2011-07-27 108 views
4

我有一個Ext.tree.Panel它有一個TreeStore。這棵樹在一個標籤中。問題在於,當我的應用程序加載應用程序中使用的所有樹時,即使該商店位於autoLoad:false時,也會加載它們的數據。我怎樣才能防止樹上的自動加載?ExtJS 4 TreePanel autoload

Ext.define('...', { 
extend:'Ext.container.Container', 
alias:'widget.listcontainer', 
layout: { 
    type: 'vbox', 
    align: 'stretch'   
}, 
items: [ 
    { 
     xtype: 'container', 
     html: "...", 
     border: 0 
    }, 
    { 
     xtype: '...', 
     flex: 1, 
     bodyPadding: 5, 
     margin: '9 0 0 0' 
    } 
] 
}); 

Ext.define('...', { 
extend:'Ext.data.TreeStore', 
model: '...', 
proxy: { 
    type: 'ajax', 
    reader: { 
     type: 'json', 
     root: 'data' 
    }, 
    api: { 
     read : 'some url' 
    } 
} 
}); 

Ext.define('...', { 
    extend: 'Ext.tree.Panel', 
    alias: 'widget....', 
    id: '...', 
    title: '...', 

    height: 400, 
    collapsible: true, 
    useArrows: true, 
    rootVisible: false, 
    multiSelect: true, 
    singleExpand: true, 
    autoScroll: true, 
    store: '...', 

columns: [...] 
}); 

P.S.我發現如果我在樹上把rootVisible改成true,這個問題不會發生,但是它會顯示給根節點(我不想)。

+0

請添加您的代碼。 –

+0

如果不是自動加載並且root不可見,你想在treepanel中看到什麼?我認爲你必須在創建商店的地方添加代碼。你想要AJAX可加載的樹或其他東西? –

+0

當然,我會在時間到的時候做一個tree.store.load(),但我不希望加載發生在應用程序加載時使用的每一棵樹上。 –

回答

1

如果root不可見,那麼AJAX樹會自動加載fisrt級別的層次結構(正如您自己已經證明的那樣)。

我認爲最好的辦法是讓根目錄可見或者在某些操作後創建樹。我寫了防止AJAX請求加載數據的代碼:

var preventTreeLoad = true;  

store.on('beforeexpand', function(node) { 
     if (node == this.getRootNode() && preventTreeLoad) { 
      Ext.Ajax.abort(this.proxy.activeRequest); 
      delete this.proxy.activeRequest; 
     } 
}, store); 

var b = Ext.create('Ext.Button', { 
     text: 'Click me', 
     renderTo: 'btn',      
    }); 

b.on ('click', function() { 
     preventTreeLoad = false; 
     this.load();   
},store); 

但我不建議使用這種方法。例如,如果javascript不那麼快 - 可能發送Ajax請求(響應不會被讀取,但服務器將執行操作)。

+0

我在php代碼中添加了寫入文件,該代碼返回樹結構並確保請求根本不發送。但是,這種方法並不好。 –

2

您可以在定義樹時放置一個虛擬代理,然後在想要開始使用樹/存儲時設置實際代理。例如:

var store = Ext.define('Ext.data.TreeStore', { 
    ... 
    // dummy proxy to avoid autoLoad on tree store construction 
    proxy: { 
     type: 'ajax', 
     url: '' 
    }, 
    ... 
); 

然後,當你想使用它的第一次,

store.setProxy({ 
    type: 'ajax', 
    url: 'http://some/real/url', 
    ... 
}); 
store.load(); 
7

我打的是同樣的問題,以避免隱含的要求,我公司指定的root內嵌在TreeStore的配置,如:

Ext.create('Ext.data.TreeStore', { 
    model: '...', 
    proxy: { 
     type: 'ajax', 
     reader: { 
      type: 'json', 
      root: 'data' 
    }, 
    api: { 
     read : 'some url' 
    } 
    folderSort: true, 
    rootVisible: false, 
    root: {expanded: true, text: "", "data": []} // <- Inline root 
}); 

明確.load內嵌根之後將被覆蓋。

+4

我不明白爲什麼這是倒投了。在這裏的所有解決方案中,這個是最靈活和最少量的黑客攻擊。只需定義一個最初沒有孩子的空擴展根,然後您可以隨時加載商店。即使與無形的根。很棒! +1 –

2

你可以用一個小的覆蓋解決這個問題:

Ext.override(Ext.tree.View, 
{ 
    setRootNode: function(node) 
    { 
     var me = this;   
     me.store.setNode(node); 
     me.node = node; 
     if (!me.rootVisible && me.store.autoLoad) 
     { 
      node.expand(); 
     } 
    } 
}); 

afterlayout你需要一個負載()

-1

嘗試用childrenautoLoad : false

root: { 
    children : [] 
} 
0

簡單的方法是設置商店的根物業

Ext.create('Ext.data.TreeStore', { 
    .... 
    autoLoad:false, 
    root: {   
     expanded: false 
    } 
});