2013-09-21 65 views
5

我創建了一個TreePanel中,我創造storeExtjs4.2.1 - 加載JSON來TreePanel中失敗

  var store = Ext.create('Ext.data.TreeStore', { 
      autoload: false, 
      proxy: { 
       type: 'ajax', 
       url: 'data.php', 
       reader: { 
        type: 'json', 
        root: 'results'     
       } 
      } 
      }); 

我的服務器,打印JSON的樣子

{ "results": 
    [ 
    { id : '1' , text : '1', expanded: true, 
     children :[ 
      { id : '5' , text : '5', leaf:true}, 
      { id : '6' , text : '6', leaf:true} 
     ] 
    }, 
    { id : '2' , text : '2', leaf:true}, 
    { id : '3' , text : '3', leaf:true}, 
    { id : '4' , text : '4', leaf:true}, 
    { id : '7' , text : '7', leaf:true}, 
    { id : '8' , text : '8', leaf:true}, 
    { id : '9' , text : '9', leaf:true}, 
    { id : '10' , text : '10', expanded: true, 
     children :[ 
      { id : '11' , text : '11', leaf:true}, 
      { id : '12' , text : '12', leaf:true} 
     ] 
    } 
    ] 
} 

但是當我運行我的代碼,我看到螢火蟲,並永遠運行GET http://localhost/example/data.php...永不停止:(

和我的樹被添加這麼多雙

如何解決謝謝

編輯
在現實我定義我的TreePanel中使用別名,並用它作爲xtype
但我創造新的例子,並得到同樣的問題。 這裏是我的js

var store = Ext.create('Ext.data.TreeStore', { 
      autoload: false, 
      proxy: { 
       type: 'ajax', 
       url: 'data.php', 
       reader: { 
        type: 'json', 
        root: 'results'     
       } 
      } 
     }); 

     Ext.create('Ext.tree.Panel', { 
      title: 'Simple Tree', 
      width: 200, 
      height: 150, 
      store: store, 
      rootVisible: false, 
      renderTo: Ext.getBody() 
     }); 

這裏是我的data.php

echo " 
{ 
    'success': true, 
    'variable': 100, 
    'results': 
    [ 
    { id : '1' , text : '1', expanded: true, 
     children :[ 
      { id : '5' , text : '5', leaf:true}, 
      { id : '6' , text : '6', leaf:true} 
     ] 
    }, 
    { id : '2' , text : '2', leaf:true}, 
    { id : '3' , text : '3', leaf:true}, 
    { id : '4' , text : '4', leaf:true}, 
    { id : '7' , text : '7', leaf:true}, 
    { id : '8' , text : '8', leaf:true}, 
    { id : '9' , text : '9', leaf:true}, 
    { id : '10' , text : '10', expanded: true, 
     children :[ 
      { id : '11' , text : '11', leaf:true}, 
      { id : '12' , text : '12', leaf:true} 
     ] 
    } 
    ] 
}"; 

回答

5

你這裏什麼東西,我認爲是在treestore的錯誤。當您將閱讀器更改爲使用不是children的根時,還需要將數據中的每個兒童屬性名稱實例更改爲新名稱。這意味着如果你想根改爲results,你的樹數據實際上必須是這個樣子:

echo " 
{ 
    'success': true, 
    'variable': 100, 
    'results': 
    [ 
    { 'id' : '1' , 'text' : '1', 'expanded': true, 
     'results':[ 
      { 'id' : '5' , 'text' : '5', 'leaf':true}, 
      { 'id' : '6' , 'text' : '6', 'leaf':true} 
     ] 
    }, 
    { 'id' : '2' , 'text' : '2', 'leaf':true}, 
    { 'id' : '3' , 'text' : '3', 'leaf':true}, 
    { 'id' : '4' , 'text' : '4', 'leaf':true}, 
    { 'id' : '7' , 'text' : '7', 'leaf':true}, 
    { 'id' : '8' , 'text' : '8', 'leaf':true}, 
    { 'id' : '9' , 'text' : '9', 'leaf':true}, 
    { 'id' : '10' , 'text' : '10', 'expanded': true, 
     'results':[ 
      { 'id' : '11' , 'text' : '11', 'leaf':true}, 
      { 'id' : '12' , 'text' : '12', 'leaf':true} 
     ] 
    } 
    ] 
}"; 

另一種選擇是,以頂級results屬性名稱更改爲children和標記的根您的商店爲children

+1

我花了一段時間才弄清楚這個問題,因爲行爲沒有意義。在我們的應用程序中使用一噸樹。 – Reimius

+0

這就是問題,謝謝你:) – freestyle

1

檢查自動加載配置拼寫。

var store = Ext.create('Ext.data.TreeStore', { 
     // autoload: false, 
      autoLoad : false, 
      proxy: { 
       type: 'ajax', 
       url: 'data.php', 
       reader: { 
        type: 'json', 
        root: 'results'     
       } 
      } 
      }); 
+0

我只是改變它,並得到了同樣的問題:( – freestyle

+0

可以提供TreePanel中配置? – jeewiya

+0

PLZ看到我的編輯 – freestyle

0

可你只需要添加的根數據存儲喜歡跟着

var store = Ext.create('Ext.data.TreeStore', { 
      autoLoad : false, 
      root: { 
       text: 'Corporate Media', 
       id: '0', 
       expanded: true 
      } 
      proxy: { 
       type: 'ajax', 
       url: 'data.php', 
       reader: { 
        type: 'json', 
        root: 'results'     
       } 
      } 
      }); 
+0

這仍然沒有工作:( – freestyle

+0

您可以創建例如在使用你的代碼的jsfiddle。我會檢查它。 – jeewiya

+0

但是使用php和jsfiddle我認爲不行。我使用extjs 4.2.1.883,我將編輯我的標題 – freestyle