简体   繁体   English

ExtJS4:TreePanel Store通过Itemclick一次又一次地加载json文件

[英]ExtJS4: TreePanel Store is loading the json File again and again by Itemclick

I have a json which has 3 main-menupoints. 我有一个json,有3个主要的menupoints。 In the first menupoint, there is one submenu, so the menupoint 1 is no 'leaf'. 在第一个menupoint,有一个子菜单,所以menupoint 1不是'叶子'。 When I click at the folder Symbol, the whole menu (the whole json file) is again under the submenu. 当我单击文件夹符号时,整个菜单(整个json文件)再次位于子菜单下。 So its infinity. 所以它的无限。 Can someone help me with this please? 请有人帮我这个吗? THANK YOU!! 谢谢!!

Here are my files: 这是我的文件:

app/data/tree.json 应用程序/数据/ tree.json

{
'result':
    [
        {
            "text": "Point1",
            'children':[
                {
                    "text": "SubPoint1",
                    'leaf':true
                },
            ]
        },
        {   "text": "Point2",
            'leaf':true
        },
        {
             "text": "Point3",
             'leaf':true
        },

]

} }

view/TreeMenu.js 视图/ TreeMenu.js

Ext.define('App.view.TreeMenu', {
extend: 'Ext.tree.Panel',
alias: 'widget.treemenu',
title:'Title',
store: 'TreeMenu'

}); });

store/TreeMenu.js 存储/ TreeMenu.js

Ext.define('App.store.TreeMenu', {
extend: 'Ext.data.TreeStore',
requires: 'App.model.TreeMenu',
model: 'App.model.TreeMenu',
proxy: {
    type: 'ajax',
    url: 'app/data/tree.json',
    reader: {
        type: 'json',
        root: 'result'
    }
}

}); });

model/TreeMenu.js 模型/ TreeMenu.js

Ext.define('App.model.TreeMenu', {
extend: 'Ext.data.Model',
fields: [
    { name: 'name', type: 'int', leaf:false, text:'name'},
    { name: 'value', type: 'string', leaf:false, text:'value'},
]

}); });

controller/TreeMenu.js 控制器/ TreeMenu.js

Ext.define('App.controller.TreeMenu', {
extend: 'Ext.app.Controller',
views: ['TreeMenu'],
models: ['TreeMenu'],   
stores: ['TreeMenu'],
onLaunch: function() {
    var treeStore = this.getTreeMenuStore();
    treeStore.load({
        //callback: this.onStationsLoad,
        scope: this
    });
},
refs: [{
    selector: 'tree',
    ref: 'treemenu'
}],
init: function() {
    this.control({
        'treemenu': {
            itemclick : this.treeItemClick  
        }
    });

},
treeItemClick : function(view, record) {
    console.log(record);
}

}); });

It took me a while to realize but your tree is actually working as designed :) 我花了一段时间才意识到,但你的树实际上是按设计工作的:)

I created a fiddle for it to visualize: http://jsfiddle.net/dbrin/auBTH/ 我创建了一个可视化的小提琴: http//jsfiddle.net/dbrin/auBTH/

Essentially every time you expand a node in a tree the default behavior is to load that node's children. 基本上每次在tree展开节点时,默认行为是加载该节点的子节点。 That means the store will send a GET request to the server specified by the proxy url property. 这意味着store将向proxy URL属性指定的服务器发送GET请求。 It sends the id of the node that you clicked on to expand so that the server side would use that id to run the query. 它发送您单击的节点的id以进行扩展,以便服务器端使用该id运行查询。

As you said you always return the same data set back and that is exactly what you are seeing: every time you expand a tree node an identical data set is appended to the tree as the children of the expanded node. 正如您所说,您总是返回相同的数据集,这正是您所看到的:每次展开树节点时,相同的数据集将作为展开节点的子节点附加到树中。 One of the nodes you return is expandable .. and so you can repeat the process again :) 您返回的其中一个节点是可扩展的..所以您可以再次重复该过程:)

EDIT : upon further thought I found a separate issue that makes the node look for children nodes on the server instead of the already fetched sub nodes underneath. 编辑 :进一步认为我发现了一个单独的问题,使节点在服务器上寻找子节点而不是下面已经获取的子节点。 The issue is with how the reader is defined. 问题在于如何定义读者。 In your original code you set json reader to parse data returned from the server specifying the root property set to result . 在原始代码中,设置json reader以解析从服务器返回的数据,指定设置为resultroot属性。
While this works on the initial load of data it also has an impact on how the children nodes are read. 虽然这适用于初始加载数据,但它也会影响子节点的读取方式。 Essentially root config overrides the default children config. 基本上root配置会覆盖默认的配置。 I corrected my previous example changing the root property to children - http://jsfiddle.net/dbrin/auBTH/4/ now it works as expected. 我更正了我之前的示例,将root属性更改为子项 - http://jsfiddle.net/dbrin/auBTH/4/现在它按预期工作。

Do any children of Json tree has "leaf : true" ? Json树的任何孩子都有“叶子:真”吗?

Maybe it's the problem... 也许这就是问题......

-- EDIT: - 编辑:

I see that you have the leaf value. 我看到你有叶值。

Maybe can be this: 也许可以这样:

'children':[

                {
                    "text": "SubPoint1",
                    'leaf':true
                }-->, <------------------------- ERASE final comma.
            ]

I can confirm it helps. 我可以证实它有帮助。 The trick is in json format 诀窍是json格式

var data = {
"result": [{
    "text": "Point1",
        "children": [{

The problem is the array of nodes is firstly under "results" and next under "children". 问题是节点数组首先在“结果”下,然后在“子”下。 These must be the same and used in root configuration in proxy 这些必须相同,并在代理中的根配置中使用

    var data = {
"result": [{
    "text": "Point1",
        "result": [{


root: 'result'

Michal 米哈尔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM