我调用store.load()
,但是treepanel只显示第一级(模型ProcessVedDoc)。如果我向load方法添加一个回调,我会在记录中看到空的childNodes。虽然数据中有子节点(但是对象没有映射到模型VedDoc)。可能有什么不对?
screenshot from firefox dev tools
模型
Ext.define('Proj.model.ProcessVedDoc', {
extend: 'Ext.data.Model',
fields: [
{name: "text", mapping: 'name'},
{name: "expanded", type: "boolean", defaultValue: true},
{name: "loaded", type: "boolean", defaultValue: true}
}],
requires : ['Proj.model.VedDoc'],
hasMany: [{
model: 'Proj.model.VedDoc',
associationKey : 'children',
name: 'children'
}]});
Ext.define('Proj.model.VedDoc', {
extend: 'Ext.data.Model',
fields: ['id',
{name: 'text', mapping: 'name'},
{name: 'leaf', type: 'boolean', defaultValue: true, persist: false}],
idProperty: 'id'});
存储
Ext.define('Proj.store.VedDocsTreeStore', {
extend: 'Ext.data.TreeStore',
model: 'Proj.model.ProcessVedDoc',
autoLoad: false,
autoSync: false,
root: {
expanded: true
},
proxy: {
type: 'ajax',
url: 'portfolios/veddocs',
getParams: Ext.emptyFn,
timeout: 300000,
reader: {type: 'json', root: 'docs'}
}});
查看
{
xtype: 'treepanel',
title: 'Documents',
name: 'vedDocs',
margin: '15 0 0 0',
width: 650,
height: 350,
rootVisible: false,
store: vdocs
}
的Json
{
"docs": [{
"name": "15a1dc1515aa151eea556",
"children": [
{"name": "Doc1", "id": "bf4e2776-1089-445f-98dd-e5e8fe5c798f"},
{"name": "Doc2", "id": "1312bba6-9d05-4aa5-b069-b2958043c2a5"},
{"name": "Doc3, "id": "49a045cf-b5cf-4478-b886-f078e6a48753"}]
},
{
"name": "15a1dc18515bb515aa151556",
"children": [
{"name": "Doc1", "id": "3cef1250-fd35-4ff6-bbbf-33e4c59d4767"},
{"name": "Doc2", "id": "a43ffa24-ed25-4fc7-80a5-f4b1a78b666e"}]
}],
"success": true}
答案 0 :(得分:1)
尝试以该格式进行回复,因为 treepanel root了解子对象。
JSON格式
{
"success": true,
"children": [{
"text": "15a1dc1515aa151eea556",
"expanded": true,
"children": [{
"text": "Doc1",
"leaf": true,
"id": "bf4e2776-1089-445f-98dd-e5e8fe5c798f"
}, {
"text": "Doc2",
"leaf": true,
"id": "1312bba6-9d05-4aa5-b069-b2958043c2a5"
}, {
"text": "Doc3",
"leaf": true,
"id": "49a045cf-b5cf-4478-b886-f078e6a48753"
}]
}, {
"text": "15a1dc18515bb515aa151556",
"expanded": true,
"children": [{
"text": "Doc1",
"leaf": true,
"id": "3cef1250-fd35-4ff6-bbbf-33e4c59d4767"
}, {
"text": "Doc2",
"leaf": true,
"id": "a43ffa24-ed25-4fc7-80a5-f4b1a78b666e"
}]
}]
}
ExtJs Treepanel和商店代码
我创建了一个演示。你在这里查看Sencha Fiddle
var store = Ext.create('Ext.data.TreeStore', {
autoLoad: true,
autoSync: false,
root: {
expanded: true
},
proxy: {
type: 'ajax',
url: 'veddocs.json',
timeout: 300000,
reader: {
type: 'json',
root: 'children'
}
}
});
Ext.create('Ext.tree.Panel', {
title: 'Simple Tree',
width: 200,
height: 150,
store: store,
rootVisible: false,
renderTo: Ext.getBody()
});
注意*您可以在前端制作treedata格式,并使用 setRoot({您的根对象})在商店中加载数据。有关详情Extjs Store Docs
答案 1 :(得分:0)
您需要从Ext.data.TreeModel
扩展模型以在商店中构建树。