2016-06-08 82 views
0

我的希望是在我的視圖中獲取呈現爲樹的JSON對象。我的代碼是在一個名爲/app/view/main/Tree.js文件從JSON抓取樹

Ext.define('SGSS.view.main.Tree', {  
    extend: 'Ext.tree.Panel', 
    alias: 'viewmodel.tree', 
    xtype: 'SimpleTree', 
    width: 400, 
    height: 250, 
    store: { 
     root: { 
      expanded: true 
     }, 
     proxy: { 
      type: 'ajax', 
      url: '/cgi-bin/uiconnector', 
      reader: { 
       type: 'json', 
       rootProperty: 'assets' 
      } 
     } 
    } 
}); 

當我呼籲引用此頁面中,我得到一個屏幕,看起來比預期的稍有不同:

enter image description here 這只是空白文件夾,將無限期地繼續下去。不是我想到的JSON對象。任何想法我做錯了什麼?

我的JSON如下:

{ 
    "assets": { 
    "batteryzone": [ 
    { 
     "id": "10001", 
     "type": "batteryzone", 
     "label": "BZ1", 
     "parentid": "1001", 
     "model": "sim", 
     "alarmsev": "None", 
     "extant": "true", 
     "invertersonline": "0", 
     "hvaconline": "0", 
     "racksonline": "0", 
     "interfacerev": "0", 
     "plcrevision": "0", 
     "numbatteryracks": "20", 
     "dcvoltage": "0.0", 
     "dcpower": "0.0", 
     "averagesoc": "0.0", 
     "storedenergy": "0.0", 
     "startupsubstate": "0", 
     "startupsequence": "0", 
     "recoverymode": "false", 
     "tempmaxconnected": "-4.0", 
     "chargecapacity": "0", 
     "dischargecapacity": "0", 
     "dccurrent": "0.0", 
     "containerenabled": "false", 
     "hicell": "3.016", 
     "locell": "3.011", 
     "nameplateenergy": "1950", 
     "recoveryrunning": "false", 
     "currinputs": [] 
    }, 
    { 
     "id": "10002", 
     "type": "batteryzone", 
     "label": "BZ2", 
     "parentid": "1001", 
     "model": "sim", 
     "alarmsev": "None", 
     "extant": "true", 
     "invertersonline": "0", 
     "hvaconline": "0", 
     "racksonline": "0", 
     "interfacerev": "0", 
     "plcrevision": "0", 
     "numbatteryracks": "20", 
     "dcvoltage": "0.0", 
     "dcpower": "0.0", 
     "averagesoc": "0.0", 
     "storedenergy": "0.0", 
     "startupsubstate": "0", 
     "startupsequence": "0", 
     "recoverymode": "false", 
     "tempmaxconnected": "-4.0", 
     "chargecapacity": "0", 
     "dischargecapacity": "0", 
     "dccurrent": "0.0", 
     "containerenabled": "false", 
     "hicell": "3.016", 
     "locell": "3.011", 
     "nameplateenergy": "1950", 
     "recoveryrunning": "false", 
     "currinputs": [] 
     } 
    ] 
    } 
} 

我這麼想嗎? (我的意思是,顯然我是!)

+0

我看到的第一個問題是,您的商店似乎不是定義爲TreeStore,而是定義爲標準(平面)商店。當我忘記將商店設爲樹木商店時,我不止一次地遇到了同樣的無限期遞歸問題。 – Alexander

回答

1

您正在使用的JSON格式不是樹面板形成樹所需的正確樹格式。所需的數據格式如下所示:

 { 
      expanded: true, 
      children: [{ 
       text: 'SFO  ✈  DFW', 
       duration: '6h 55m', 
       expanded: true, 
       children: [{ 
        text: 'SFO  ✈  PHX', 
        duration: '2h 04m', 
        leaf: true 
       }, { 
        text: 'PHX layover', 
        duration: '2h 36m', 
        isLayover: true, 
        leaf: true 
       }, { 
        text: 'PHX  ✈  DFW', 
        duration: '2h 15m', 
        leaf: true 
       }] 
      }] 
     } 

這裏要指定的rootProperty是「孩子」在下面的部分中docs for the treestore如所陳述:

讀嵌套數據

爲了讀取嵌套數據,Ext.data.reader.Reader必須爲 ,並配置一個根屬性,這樣讀者才能找到每個節點的嵌套數據 (如果未指定根目錄它將默認爲 'children')。這將告訴樹使用相同的關鍵字,即'children'來查找任何嵌套樹節點 。如果在 配置中指定了根目錄,請確保任何具有子項的嵌套節點都具有相同的 名稱。

還要注意的是葉:真已被用於表示該節點是樹,即,節點不包含子節點的終端/葉節點。

有關數據格式的更多示例,可以參考API

+0

哇...所以這告訴我,ExtJS6 **不能**使用JSON嗎?我必須以某種方式將這些值添加到JSON以使其可用? –

+0

是的數據需要格式化以適應樹型數據結構。 –