mar*_*rio 9 javascript tree nested extjs
我总是很感激任何进一步的建议.
我正在尝试开发一个简单的extJS Ext 4.0.2a脚本,将一些嵌套数据显示为拖放树.为了尝试,我使用http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.reader.Reader中的一个简单示例
数据以users.json文件的形式给出:
{
"users": [
{
"id": 123,
"name": "Ed",
"orders": [
{
"id": 50,
"total": 100,
"order_items": [
{
"id" : 20,
"price" : 40,
"quantity": 2,
"product" : {
"id": 1000,
"name": "MacBook Pro"
}
},
{
"id" : 21,
"price" : 20,
"quantity": 3,
"product" : {
"id": 1001,
"name": "iPhone"
}
}
]
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
我希望将数据显示为树,其第一级节点是用户,第二级节点是订单,依此类推.
从同一个doc,我学习如何定义我的模型(我相信):
Ext.define("User", {
extend: 'Ext.data.Model',
fields: [
'id', 'name'
],
hasMany: {model: 'Order', name: 'orders'},
proxy: {
type: 'ajax', // rest
url : 'users.json',
reader: {
type: 'json',
root: 'users'
}
}
})
;
Ext.define("Order", {
extend: 'Ext.data.Model',
fields: [
'id', 'total'
],
hasMany : {model: 'OrderItem', name: 'orderItems', associationKey: 'order_items'},
belongsTo: 'User'
});
Ext.define("OrderItem", {
extend: 'Ext.data.Model',
fields: [
'id', 'price', 'quantity', 'order_id', 'product_id'
],
belongsTo: ['Order', {model: 'Product', associationKey: 'product'}]
});
Ext.define("Product", {
extend: 'Ext.data.Model',
fields: [
'id', 'name'
],
hasMany: 'OrderItem'
});
Run Code Online (Sandbox Code Playgroud)
接下来,我定义了一个树存储和一个树面板(对于某些选定的字段):
var store = Ext.create('Ext.data.TreeStore', {
model: 'User',
autoLoad: true,
autoSync: true,
root: {
name: "Root node",
id: '0',
expanded: true
},
sorters: [{
property: 'id',
direction: 'ASC' // DESC
}]
});
var tree = Ext.create('Ext.tree.Panel', {
store: store,
displayField: 'name', // what nodes display (default->text)
columns: [{
xtype: 'treecolumn',
text: 'name',
dataIndex: 'name',
width: 150,
sortable: true
}, {
text: 'total',
dataIndex: 'total',
width: 150,
flex: 1,
sortable: true
}, {
text: 'price',
dataIndex: 'price',
width: 50,
flex: 1,
sortable: true
},{
text: 'quantity',
dataIndex: 'quantity',
width: 150,
flex: 1
}, {
text: 'id',
dataIndex: 'id',
flex: 1,
width: 15,
sortable: true
}],
collapsible: true,
viewConfig: {
plugins: {
ptype: 'treeviewdragdrop', // see Ext.tree.plugin.TreeViewDragDrop
nodeHighlightColor : '7B68EE',
nodeHighlightOnDrop : true,
nodeHighlightOnRepair: true,
enableDrag: true,
enableDrop: true
}
},
renderTo: 'tree-div',
height: 300,
width: 900,
title: 'Items',
useArrows: true,
dockedItems: [{
xtype: 'toolbar',
items: [{
text: 'Expand All',
handler: function(){
tree.expandAll();
}
}, {
text: 'Collapse All',
handler: function(){
tree.collapseAll();
}
}]
}]
});
});
Run Code Online (Sandbox Code Playgroud)
我看到面板,根和第一级用户(作为根的子节点).我没有看到任何子节点(订单,order_items等).
我仔细查看了一些帖子,改进了很多东西,但仍然错过了一个有效的解决方案.
我很感激任何进一步的建议。
我取得了一些进步。看来我需要修改我的json:
{
"children": [ <<<<---- use the same string to insert nested data, see below too
{
"id": 12,
"name": "Ed2",
"children": [] <<<<---- need this line, otherwise, JS tries to load subnodes for ever
},
{
"id": 123,
"name": "Ed",
"children": [ <<<<---- as above
{
"id": 50,
"total": 100,
"info" : "hello",
"name" : "ciao",
"children": [ <<<<---- as above
{
"id" : 20,
"price" : 40,
"quantity": 2,
"children" : { <<<<---- as above
"id": 1000,
"name": "MacBook Pro",
"children": [] <<<<---- as above bis
}
},
{
"id" : 21,
"price" : 20,
"quantity": 3,
"children" : { <<<<---- as above
"id": 1001,
"name": "iPhone",
"children": [] <<<<---- as above bis
}
}
]
}
]
}
]
Run Code Online (Sandbox Code Playgroud)
}
然后模型的以下定义就起作用了:
Ext.define("User", {
extend: 'Ext.data.Model',
fields: [
'id', 'name', 'info', 'price', 'quantity', 'order_id', 'product_id'
],
proxy: {
type: 'ajax', //rest
url : 'ex1.json',
reader: {
type: 'json',
root: 'children'
}
}
Run Code Online (Sandbox Code Playgroud)
});
因此,似乎根本不需要不同级别的嵌套数据的模型之间的关联。
它有效,尽管我不确定是否有缺点。我仍在寻求你的建议,我通过尝试和错误,我还没有看到潜在的逻辑。谢谢。
我又有了新的想法。使用唯一字符串“children”的技巧似乎不太好。让我考虑一个非常简单的 json:
{
"users": [
{
"id": 12,
"name": "Ed2",
"orders": []
},
{
"id": 123,
"name": "Ed",
"orders": [
{
"id": 50,
"info" : "hello",
"name" : "MM",
"leaf" : 'true',
"some_else": []
}]
}
]
Run Code Online (Sandbox Code Playgroud)
}
我希望得到的 extJS 树是:
Root
|--ED
|--ED2
|--MM
Run Code Online (Sandbox Code Playgroud)
模型只是
Ext.define("User", {
extend: 'Ext.data.Model',
fields: [
'id', 'name'
],
hasMany: {model: 'Order', name: 'orders'},
proxy: {
type: 'ajax', //rest
url : 'my_file.json',
reader: {
type: 'json',
root: 'users'
}
}
});
Ext.define("Order", {
extend: 'Ext.data.Model',
fields: [
'id', 'name', 'info'
],
belongsTo: 'User'
});
Run Code Online (Sandbox Code Playgroud)
我得到的最终输出只是:
Root
|--ED
|--ED2
Run Code Online (Sandbox Code Playgroud)
这是我的错误,还是缺少功能?有没有办法不改变我的json?
多谢
| 归档时间: |
|
| 查看次数: |
10531 次 |
| 最近记录: |