2013-02-15 58 views
1

我想創建我的json數據的詳細視圖。我不確定如何真正接近它。我有eventlistener,但我不確定如何根據所選欄的名稱來提取數據。這裏是我的JSON拉:Titanium Mobile行數據的詳細視圖

var win =Titanium.UI.currentWindow; 

var data = []; 

var xhr = Titanium.Network.createHTTPClient(); 

var barList = Titanium.UI.createTableView({ 
    height:366, 
    width: 320, 
    top:0, 
    left:0 
}); 

win.add(barList); 

xhr.onload = function() { 
    var json = JSON.parse(this.responseText); 
    Ti.API.info(json.length); 
    for (var i = 0; i < json.length; i++) { 

     var row = Titanium.UI.createTableViewRow({ 
      hasChild: true, 
      className: 'bar-row', 
      filter: json[i].bar.name 
     }); 
     var titleLabel = Titanium.UI.createLabel({ 
      text: json[i].bar.name, 
      font: { 
       fontSize: 14, 
       fontWeight: 'bold' 
      }, 
      left: 70, 
      top: 5, 
      height: 20, 
      width: 210 
     }); 
     row.add(titleLabel); 
     var addressLabel = Titanium.UI.createLabel({ 
      text: json[i].bar.address, 
      font: { 
       fontSize: 10, 
       fontWeight: 'normal' 
      }, 
      left: 70, 
      top: 25, 
      height: 40, 
      width: 200 
     }); 
     row.add(addressLabel); 
     var iconImage = Titanium.UI.createImageView({ 
      text: json[i].bar.logo_file_name, 
      width: 50, 
      height: 50, 
      left: 10, 
      top: 10 
     }); 
     row.add(iconImage); 
     data.push(row); 

     row.addEventListener('click',function(e){ 
     var detail = Ti.UI.createWindow({ 
      title: e.rowData.title 
     }); 

    detail.open({modal: true}); 

}) 

    } 
    barList.data = data; 



}; 


xhr.open('GET', 'http://site.com/db.json'); 

xhr.send(); 

JSON數據: 我期待拉名稱,描述,mon_special,tues_special等,爲酒吧選擇

http://pastie.org/private/eyp9m5il6hrulbds76a8q

回答

1

的最簡單方法要做到這一點是數據附加到你創建的行:

var row = Titanium.UI.createTableViewRow({ 
    hasChild: true, 
    className: 'bar-row', 
    filter: json[i].bar.name, 
    barData : json[i].bar 
}); 

然後通過事件偵聽器訪問加入的TableView本身(不要使用該事件的rowData對象,如果您已經創建了該行本身)我的事件偵聽器添加到表

barList.addEventListener('click', function(e) { 
    // Get the row clicked, then get our custom attribute 
    var passedJSONBarData = e.row.barData; 
    // Now pass along through the window, or build the window here 
    var detail = Ti.UI.createWindow({ 
     title: passedJSONBarData.title, 
     barData : passedJSONBarData 
    }); 

    detail.open({modal: true}); 
}); 

所以,我們只創造,一旦你的功能使用這種方法可以節省一些性能/內存。

+0

所以我得到這個錯誤時,我所做的更改: '[錯誤]:腳本錯誤=嘗試開始從一個模式過渡而轉變已在進行中。等待viewDidAppear/viewDidDisappear以知道當前轉換已經在bars.js(第70行)完成了.' – 2013-02-16 17:22:56

+0

這是我所做的更改: 'var row = Titanium.UI.createTableViewRow {{hasChild:true, className: 'bar-row', filter:json [i] .bar.name, barData:json [i] .bar.address });' – 2013-02-16 17:24:05

+0

這不是來自於這種改變。我敢打賭,你並沒有從行中移除事件監聽器,所以現在你有兩個'click'事件觸發。從循環中刪除'addEventListener'。 – 2013-02-16 17:37:22