2013-03-20 147 views
1

您好新Sencha Touch2,解析嵌套的Json數據並在列表中顯示時遇到問題。 這裏是我的Json樣子:Sencha Touch2:解析嵌套的Json數據

[{"task": 
{"agent":{"activeFlag":"false", 
    "shiftId":"0","id":"0","deleteFlag":"false"}, 
     "id":"124","status":"Unassigned", 
    "assignment":{"pnr": 
    {"locator":"ABCDEF","connectTime":"0","id":"0"}, 
     "id":"123", 
     "alerts":"Delay" 
     "customers":[ 
      {"frequentNumber":"12345", 
       "doNotMissFlag":"false", 
       "customerScore":"0", 
       "lastName":"XYZ", 
       "firstName":"ABC", 
       "primaryFlag":"true", 
       "customerId":"56789"}, 
      {"frequentNumber":"987654", 
       "doNotMissFlag":"false", 
       "customerScore":"0", 
       "lastName":"PQR", 
       "firstName":"STU ", 
       "cartNumber":"0", 
       "primaryFlag":"false", 
       "customerId":"54321"}]}, 
    }}, 
    {"task":{"agent":{...... 

時能得到「代理人」的價值觀,「分配」值,但沒能得到「客戶」。 這裏是模型。 ModelList.js:

Ext.define('CustomList.model.ModelList', { 
    extend: 'Ext.data.Model', 
    xtype:'modelList', 
    requires:['CustomList.model.Customers'], 
    config: { 
      fields:['task'], 
     proxy:{ 
      type:'ajax', 
      url:'http://localhost:9091/CustomListJson/app/store/sample.json', 
      reader:{ 
      type:'json' 

     } 
    }, 
    hasMany:{model:'CustomList.model.Customers', 
      name:'customers'} 
    } 

}); 

Customers.js:

Ext.define('CustomList.model.Customers', { 
    extend: 'Ext.data.Model', 
    config: { 
     fields: [ 
      'firstName','lastName' 
     ], 
     belongsTo: "CustomList.model.ModelList" 
    } 

}); 

這裏是我的ShowList.js:

Ext.define('CustomList.view.ShowList',{ 
    extend:'Ext.Panel', 
    xtype:'showList', 
    config:{ 
     layout:'fit', 
     styleHtmlContent:'true', 
     styleHtmlCls:'showListCls', 
     items:[ 
      { 
       xtype:'list', 
       id: 'listitems', 
       store:'StoreList', 
       itemTpl:['{task.assignment.alerts}', '<div>', 
        '<h2><b>FirstName: </b></h2>', 
        '<tpl for="Customers">',  // could not able to get and show in list 
        '<div> - {firstName}</div>', // could not able to get and show in list 
        '</tpl>', 
        '</div>'] 
// am able get the below values in list 
//    itemTpl:'{task.assignment.alerts}' 
//    itemTpl:'{task.assignment.pnr.locator}' 
//     itemTpl:'{task.agent.activeFlag}' 
//    itemTpl: '<b>{firstName} {lastName}  </b><br>'+'pnr: '+ '{locator} <br>' + 
//     'Alerts: <font color="#990000">'+'{alerts} </font>' +'status: '+'{status} ' 
       }] 


    } 
}); 

這裏是我的StoreList.js:

Ext.define('CustomList.store.StoreList', { 
    extend:'Ext.data.Store', 
    requires:['Ext.data.reader.Json'], 
    config:{ 
     model:'CustomList.model.ModelList', 
     autoLoad:'true' 

    } 
}); 

我遵循這個:http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.reader.Reader 但無法顯示在列表中。誰能幫幫我嗎。謝謝。

+0

而不是列表,嘗試嵌套列表 – 1Mayur 2013-03-20 07:34:15

+0

你能否建議我在我的代碼上面的變化? – chipmunk 2013-03-20 09:08:16

+0

@chipmunk你可以請一些想法如何從json獲得所有數據我也陷入了同樣類型的問題 – surhidamatya 2013-07-12 03:48:16

回答

0

我得到了我的問題的解決方案。使用以上我可以顯示的「客戶」的項目

itemTpl:['{task.assignment.alerts} <div>', 
        '<tpl for="task.assignment.customers">', 
        'firstName: {firstName}<br>', 
        '</tpl> </div>' 
       ].join('') 

: 我ShowList.js所做的更改: 用這個。謝謝。

+0

你能告訴我你是如何從你的json獲得代理和賦值的嗎? – Tejas 2013-03-21 09:50:14