2016-02-24 51 views
1

美好的一天。ExtJs樹裝載序列

我正在編寫一個樹控件Ext.tree.Panel,它從外部.json文件加載其配置,而數據從服務器加載爲.json。

所以,我有這樣的觀點:

Ext.define('App.tree.TreeView', { 
    extend: 'Ext.tree.Panel', 
    ... 
    initFilters: function(cfg) { 
     /*several this.store.addFilter() based on cfg*/ 
    }, 
    initComponent: function() { 
     var me = this; 
     me.cfg = getSomeCfgFromSomewhere(); 
     me.initFilters(me.cfg); 
    }  
}); 

商店:

Ext.define('Site.widgets.tree.TreeStore', { 
    extend: 'Ext.data.TreeStore', 
    ... 
    proxy: { 
     type: App.cfg.proxy, 
     reader: { type: 'json', rootProperty: 'data.children' }, 
     format: App.cfg.proxy.urlEnd, 
     url: App.cfg.treeRoot, 
     noCache: false 
    } 
    lazyFill: true, 

    rootVisible: true, 
    root: { 
     full_path: '/', /*set as idProperty in model*/ 
     'display-name': 'root', 
     expanded: true 
    }, 
}); 

的問題是:我的根在商店,它加載它的內容,因爲明確設置expanded:true。但是隻有這些內容顯示在樹中,沒有根本身。刪除過濾時,tre會加載OK。

可能的原因:從EXT-ALL-debug.js的煎茶代碼的調試:視圖的initComponent商店開始加載數據之前被調用,因此它增加了過濾器之前的數據加載,當根仍空,然後調用

onNodeFilter: function(root, childNodes) { 
    var me = this, 
     data = me.getData(), 
     toAdd = []; 
    if (me.getRootVisible()) { 
     if (childNodes.length) { 
      toAdd.push(root); 
     } else { 
      root.set('visible', false, me._silentOptions); 
     } 
    } 
    ... 
} 

,即根被設置爲不可見,因爲它仍然是空的,只有root的子項被加載。

問題是:我的過濾器初始化過程中是否存在初始設計錯誤,我該如何解決?

回答

1

考慮的問題是,這家店開始加載數據前視圖的initComponent叫,我會嘗試:

存儲加載事件中主叫initFilters,就像這樣:

Ext.define('Site.widgets.tree.TreeStore', { 
    extend: 'Ext.data.TreeStore', 
    ... 
    proxy: { 
     type: App.cfg.proxy, 
     reader: { type: 'json', rootProperty: 'data.children' }, 
     format: App.cfg.proxy.urlEnd, 
     url: App.cfg.treeRoot, 
     noCache: false 
    } 
    lazyFill: true, 

    rootVisible: true, 
    root: { 
     full_path: '/', /*set as idProperty in model*/ 
     'display-name': 'root', 
     expanded: true 
    }, 
    listeners : { 
     load : function() { 
      //Call initFilters. 
      //initFilters should call addFilter just once, 
      //with array of filters as first parameter, 
      //to avoid filtering the hole tree more than once! 
     } 
    } 
}); 

這方法有一個問題,因爲你必須處理initFilters cfg參數的作用域。

+0

後端返回由節點的** full_path **成員中包含的路徑定義的節點,因此該樹被動態加載。根必須描述我想加載的子樹的'上'節點,後來我想能夠從外部改變它。我的代理實際上是這樣的:'proxy:{type:App.cfg.proxy,reader:{type:'json',rootProperty:'data.children'},格式:App.cfg.proxy.urlEnd,url:Site .cfg.url.treeRoot,noCache:false}' – AntalOvc

+0

謝謝你試圖提供幫助。我自己嘗試的第一件事,但它並沒有改變行爲。第二個可能工作,但在任何負載事件初始化過濾器似乎是一個相當昂貴的解決方案,因爲他們每個人都調用整個樹的過濾,並且通常有大約6個過濾器。 – AntalOvc

+0

不客氣! addFilter方法接受一個過濾器數組作爲第一個參數,所以你可以只調用它一次! –

1

好的,結果是部分答案只是在問題的內部:我能夠通過在店內簡單地重載onNodeFilter()方法來解決這個問題,因爲我知道我總是要顯示根。如果有人能提供更好的建議 - 請作爲我的客人。

+0

不錯,至少你找到了解決方案! =) –