如何将嵌套数据显示到树中? [英] How do I show nested data into a tree?

查看:13
本文介绍了如何将嵌套数据显示到树中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

进一步的进展.请参阅 http://www.sencha.com/forum/showthread.php?153986-Empty-column-something-I-can-t-get-with-Ext.data.TreeStore-and-or-Ext.tree.Panel

Further progress. Please see at http://www.sencha.com/forum/showthread.php?153986-Empty-column-something-I-can-t-get-with-Ext.data.TreeStore-and-or-Ext.tree.Panel

我总是很感激任何进一步的建议.

I always appreciate any further advise.

我正在尝试开发一个简单的 extJS Ext 4.0.2a 脚本,以将一些嵌套数据显示为拖放树.为了尝试,我使用了一个来自 http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.reader.Reader

I am trying to develop a simple extJS Ext 4.0.2a script to display some nested data as a Drag&Drop tree. To try, I use a simple example from http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.reader.Reader

数据以 users.json 文件的形式给出:

Data are given as a users.json file:

{
"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"
                        }
                    }
                ]
            }
        ]
    }
]
}

我希望将数据显示为一棵树,其一级节点为用户,二级节点为订单,依此类推.

I wish to display data as a tree, whose first level nodes are users, second level nodes are orders, and so on.

从同一个文档中,我学习了如何定义我的模型(我相信):

From the same doc, I learn how to define my models (I believe):

    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'
});

接下来,我定义了一个树存储和一个树面板(对于一些选定的字段):

next, I define a tree store and a tree panel (for some selected fields):

    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();
                }
            }]
        }]
    });
});

我看到面板、根和第一级用户(作为根的子节点).我没有看到任何子节点(订单、订单项等).

I see the panel, the root and the first level users (as subnodes of the root). I do not see any subnodes (orders, order_items and so on).

我仔细查看了一些帖子,改进了很多,但仍然没有找到可行的解决方案.

I looked carefully at a number of posts, improved things quite a lot, but still miss to get a working solution.

推荐答案

我面临同样的任务,首先很高兴遇到您的帖子!经过 5 个小时的探索官方文档后:

I am facing the same task, and first was glad to run into your post! After 5 hours of exploring the official docs regarding to it:

我能够用比这更好的东西填充我的嵌套树存储:

I was able to populate my nested treestore with nothing better than this:

(在我的例子中,主要模型是 Camgroup,其中有许多相机作为cams":它允许对每个组使用 .cams())

(in my case the main model is Camgroup which hasMany Camera as 'cams': which allows to use .cams() for every group)

Ext.define('AV.store.sCamgroups', {
    extend: 'Ext.data.TreeStore',
    model: 'AV.model.Camgroup',
    autoLoad: true,
    root: {
        expanded: true,
        text: "Grouped Cameras"
    },
    proxy: {
        type: 'ajax',
        url: 'data/camgroups.json',
        reader: {
            type: 'json',
            root: 'camgroups',
            successProperty: 'success'
        }
    },
    listeners: {
        load: function(thisStore, rootnode, records, successful, eOpts){
            records.forEach(function(group){
                group.cams().each(function(cam) {
                    group.appendChild({
                        text: cam.get('name'),
                        leaf: true
                    });
                });
            });
        }
    }
});

camgroups.json 像这样返回:

camgroups.json returns smth like this:

{
    success: true,
    camgroups: [{
        "id"    : "group1",
        "name"  : "Exterior cams",
        "cams"  : [{
            "id"    : "cam3",
            "name"  : "Camera three",
            "url"   : "...",
            "img"   : "images/cams/3.png",
            "ratio" : 1.33
        },{
            "id"    : "cam4",
            "name"  : "Camera four",
            "url"   : "...",
            "img"   : "images/cams/4.png",
            "ratio" : 1.33
        }]
    }]
}

希望这能帮助您继续寻找更好的解决方案(可能定义一个合适的阅读器).

Hope this will help you though keep looking for a better solution (prob defining a proper reader).

同样,在Ext.data.reader.Reader"中,我可以看到 Reader 具有配置属性implicitIncludes",它应该自动解析嵌套在响应对象中其他模型中的模型",但我无法使其工作=(

Again, in "Ext.data.reader.Reader" I can see that Reader has config property 'implicitIncludes' which is supposed 'to automatically parse models nested within other models in a response object', but I cannot make it working =(

干杯,伊利亚

这篇关于如何将嵌套数据显示到树中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆