2011-08-05 62 views
2

我想使用ExtJS4創建TreePanel。所以,我要送JSON到代理讀者已經格式如下ExtJS4如何從JSON設置TreePanels根節點

{"text":"en","children": 
    {"text":"/","children":[ 
     {"text":"/page","children":[ 
      {"text":"/page/new","children":[],"leaf":true,"expanded":false}, 
      {"text":"/page/remove","children":[],"leaf":true,"expanded":false} 
     ],"leaf":false,"expanded":false}, 
     {"text":"/home","children":[],"leaf":true,"expanded":false} 
    ],"leaf":false,"expanded":true} 
} 

如何做我必須配置我的店,如果我想"en"節點成爲我的根節點。

Ext.define('Example.store.WebItems', { 
    extend: 'Ext.data.TreeStore', 

    model: 'Example.model.Item', 

    proxy: { 
     type: 'ajax', 
     api: { 
      read : 'some/url', 
     }, 
     reader: { 
      type: 'json', 
      root: 'children' // Is this correct? 
     } 
    }, 
    root: // What should I write here? 
}); 

當我定義TreeStore的根爲root: 'My Root'這將增加新的根,但我想使用JSON定義的根。所以我的問題是:

  1. 如何從JSON使用根節點,而不是定義它manualy?
  2. 我是否還必須在代理讀取器和TreeStore中定義根節點?

回答

5

響應必須有root。例如:

{ MyRoot: 
    {"text":"en","children": 
    {"text":"/","children":[ 
     {"text":"/page","children":[ 
      {"text":"/page/new","children":[],"leaf":true,"expanded":false}, 
      {"text":"/page/remove","children":[],"leaf":true,"expanded":false} 
     ],"leaf":false,"expanded":false}, 
     {"text":"/home","children":[],"leaf":true,"expanded":false} 
    ],"leaf":false,"expanded":true} 
    } 
} 

在這個例子中是root 'MyRoot'。現在,你必須「告訴」的讀者,你root是「MyRoot」:

 // ... 
     reader: { 
      type: 'json', 
      root: 'MyRoot' 
     } 
    }, 
    //root: this config is not needed now 
}); 
+4

感謝您的回答。我只想補充一點,Ext.tree.Panel的屬性'rootVisible'必須設置爲'false'。否則,不會加載樹。 – kurochenko

+0

將rootVisible設置爲false是我正在尋找的祕密......謝謝! – prototype

1

按照我知道,你需要指定根如下:

root : { 
text : 'en', 
id : 'src', 
expanded : true' 
} 

試試上面的代碼片斷它會幫助你。

+0

感謝您的回答!不幸的是,它只是手動添加了根目錄,這是我不想要的。 – kurochenko

0

可能會遲到,但對於其他人,包裹JSON內部數組[]就像這樣:
[{"text":"en","children": {"text":"/","children":[ {"text":"/page","children":[ {"text":"/page/new","children":[],"leaf":true,"expanded":false}, {"text":"/page/remove","children":[],"leaf":true,"expanded":false} ],"leaf":false,"expanded":false}, {"text":"/home","children":[],"leaf":true,"expanded":false} ],"leaf":false,"expanded":true} }]
刪除讀者塊
reader: { type: 'json', root: 'MyRoot' }

附加根塊:

root : { text : 'en', id : 'src', expanded : true' }

在treepanel中添加rootVisible: false