ExtJS 4:TreeStore同时具有静态和动态加载的数据?
我正在做一个树面板,看起来像这样:ExtJS 4:TreeStore同时具有静态和动态加载的数据?,extjs,extjs4,Extjs,Extjs4,我正在做一个树面板,看起来像这样: Ext.define('demo.UserModel', { extend: 'Ext.data.Model', fields: ['id', 'name', 'profile_image_url'] }); var userTreeStore = Ext.create('Ext.data.TreeStore', { model: 'demo.UserModel', proxy: { type: 'jso
Ext.define('demo.UserModel', {
extend: 'Ext.data.Model',
fields: ['id', 'name', 'profile_image_url']
});
var userTreeStore = Ext.create('Ext.data.TreeStore', {
model: 'demo.UserModel',
proxy: {
type: 'jsonp',
url : 'https://myserver/getusers',
reader: {
type: 'json',
root: 'users'
}
},
listeners: {
// Each demo.UserModel instance will be automatically
// decorated with methods/properties of Ext.data.NodeInterface
// (i.e., a "node"). Whenever a UserModel node is appended
// to the tree, this TreeStore will fire an "append" event.
append: function( thisNode, newChildNode, index, eOpts ) {
// If the node that's being appended isn't a root node, then we can
// assume it's one of our UserModel instances that's been "dressed
// up" as a node
if( !newChildNode.isRoot() ) {
newChildNode.set('leaf', true);
newChildNode.set('text', newChildNode.get('name'));
newChildNode.set('icon', newChildNode.get('profile_image_url'));
}
}
}
});
userTreeStore.setRootNode({
text: 'Users',
leaf: false,
expanded: false // If this were true, the store would load itself
// immediately; we do NOT want that to happen
});
var settingsTreeStore = Ext.create('Ext.data.TreeStore', {
root: {
expanded: true,
children: [
{
text: 'Settings',
leaf: false,
expanded: true,
children: [
{
text: 'System Settings',
leaf: true
},
{
text: 'Appearance',
leaf: true
}
]
}
]
}
});
// Graft our userTreeStore into the settingsTreeStore. Note that the call
// to .expand() is what triggers the userTreeStore to load its data.
settingsTreeStore.getRootNode().appendChild(userTreeStore.getRootNode()).expand();
Ext.create('Ext.tree.Panel', {
title: 'Admin Control Panel',
store: settingsTreeStore,
});
目前,我用以下代码对其进行了“模拟”:
treePanel.setRootNode({
text: 'Root',
expanded: true,
children: [
{
text: 'General Settings',
icon: kpc.cfg.baseUrl.img+'/icon_gears-bluegreen.gif',
leaf: true
},
{
text: 'Users',
icon: kpc.cfg.baseUrl.img+'/icon_users-16x16.gif',
expanded: true,
children: [
{
text: 'Dummy User 1',
icon: kpc.cfg.baseUrl.img+'/icon_user-suit.gif',
leaf: true
},
{
text: 'Dummy User 2',
icon: kpc.cfg.baseUrl.img+'/icon_user-suit.gif',
leaf: true
},
{
text: 'Dummy User 3',
icon: kpc.cfg.baseUrl.img+'/icon_user-suit.gif',
leaf: true
},
{
text: 'Dummy User 4',
icon: kpc.cfg.baseUrl.img+'/icon_user-suit.gif',
leaf: true
}
]
}
]
});
我如何动态加载单个用户(即通过商店)?换句话说,我如何制作一个静态和动态加载项混合的树存储
谢谢 我相信
节点
参数将帮助您。设置autoLoad:false
,然后利用实际树面板的beforerender
事件。在事件内部调用存储的load函数,并向其传递一个节点
。文档声明,如果这是从load()
调用中输入的,则默认为根节点。看起来,您可以将设置保留在根节点中,然后通过调用load并将其传递给子节点,您可以只更新用户
请参阅:供参考。请注意,此加载函数与Ext.data.Store
中的加载函数不同(Ext.data.TreeStore
不继承自Ext.data.Store
)
我还没有机会测试它,但它似乎很有希望。我有一个非常类似的问题,虽然我没有让它完全像我希望的那样工作,但它基本上是工作的。我已
自动加载:false
,并添加了此事件处理程序:
beforerender: function(comp, opts) {
var node = this.getRootNode();
node.appendChild({test: 'Recent', id: 'recent', expandable: true, expanded: false});
node.appendChild({text: 'Current', id: 'current', expandable: true, expanded: false});
node.appendChild({text: 'All', id: 'all', expandable: true, expanded: false});
}
根目录的3个直接子目录是静态的,然后代理在我展开它们时发出填充它们的请求(传递适当的id)
我还必须禁止根节点与存储上的侦听器一起加载:
listeners: {
beforeload: function(store, operation, opts) {
if (operation.node.data.id == 'root') {
return false;
}
}
},
希望这有帮助。看来应该有更好的办法 最适合我的解决方案是:
Ext.define('demo.UserModel', {
extend: 'Ext.data.Model',
fields: ['id', 'name', 'profile_image_url']
});
var userTreeStore = Ext.create('Ext.data.TreeStore', {
model: 'demo.UserModel',
proxy: {
type: 'jsonp',
url : 'https://myserver/getusers',
reader: {
type: 'json',
root: 'users'
}
},
listeners: {
// Each demo.UserModel instance will be automatically
// decorated with methods/properties of Ext.data.NodeInterface
// (i.e., a "node"). Whenever a UserModel node is appended
// to the tree, this TreeStore will fire an "append" event.
append: function( thisNode, newChildNode, index, eOpts ) {
// If the node that's being appended isn't a root node, then we can
// assume it's one of our UserModel instances that's been "dressed
// up" as a node
if( !newChildNode.isRoot() ) {
newChildNode.set('leaf', true);
newChildNode.set('text', newChildNode.get('name'));
newChildNode.set('icon', newChildNode.get('profile_image_url'));
}
}
}
});
userTreeStore.setRootNode({
text: 'Users',
leaf: false,
expanded: false // If this were true, the store would load itself
// immediately; we do NOT want that to happen
});
var settingsTreeStore = Ext.create('Ext.data.TreeStore', {
root: {
expanded: true,
children: [
{
text: 'Settings',
leaf: false,
expanded: true,
children: [
{
text: 'System Settings',
leaf: true
},
{
text: 'Appearance',
leaf: true
}
]
}
]
}
});
// Graft our userTreeStore into the settingsTreeStore. Note that the call
// to .expand() is what triggers the userTreeStore to load its data.
settingsTreeStore.getRootNode().appendChild(userTreeStore.getRootNode()).expand();
Ext.create('Ext.tree.Panel', {
title: 'Admin Control Panel',
store: settingsTreeStore,
});
这是我将普通数据转换为treeStore数据的函数,您可以使用它。这样,您就不再需要treeStore了: 记录:记录的数组。 Text:项目名称(从记录中获取) 子项:子项的名称(默认为“子项”)
我用一个阅读器来达到这种效果,我觉得这是一种优雅的时尚。请注意,这是一个平面商店,可能看起来有点不同与树商店。这一概念应该得到翻译
Ext.define('Ext.data.reader.a8PolicyReader', {
extend: 'Ext.data.reader.Json',
read: function(response) {
var staticStuff,
responseArr;
// Static stuff
staticStuff = [{name: 'some static user', id:1}, {name: 'another user', id:2}];
// extract response
responseArr = Ext.decode(response.responseText);
// shove them together
responseArr.concat(staticStuff);
// read
this.readRecords(responseArr);
}
})
什么部分保持静止?一般设置?如果没有动态加载的数据,树是否会以某种方式工作?如果答案是否定的,那么我可能会建议加载所有内容(前提是树真的这么简单并且开销很低)。@LittleTreeX:“设置”节点是静态的。我希望所有的“用户”节点都能从服务器动态加载(即从RESTful API加载)。+1。。。这是非常翔实的,并且能够阅读一些内容。尽管如此,这个页面中的某些内容似乎没有响应,并在上面提供的链接上导致Google Chrome崩溃。