Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Extjs 如何在没有子项或叶信息的情况下填充Ext.data.TreeStore?_Extjs_Extjs4_Gridpanel_Treepanel - Fatal编程技术网

Extjs 如何在没有子项或叶信息的情况下填充Ext.data.TreeStore?

Extjs 如何在没有子项或叶信息的情况下填充Ext.data.TreeStore?,extjs,extjs4,gridpanel,treepanel,Extjs,Extjs4,Gridpanel,Treepanel,我有一个返回一系列内容的服务。一个事物只有一个id和一个名称 我想将它们加载到Ext.tree.Panel中。现在,我已经定义了一个数据存储,它模仿服务的典型响应。您可以使用演示JSFIDLE进行游戏 代码也包括在下面: Ext.define('Thing', { extend: 'Ext.data.Model', fields: ['id', 'name'], }); var store = Ext.create('Ext.data.TreeStore', { mod

我有一个返回一系列内容的服务。一个事物只有一个
id
和一个
名称

我想将它们加载到
Ext.tree.Panel
中。现在,我已经定义了一个数据存储,它模仿服务的典型响应。您可以使用演示JSFIDLE进行游戏

代码也包括在下面:

Ext.define('Thing', {
    extend: 'Ext.data.Model',
    fields: ['id', 'name'],
});

var store = Ext.create('Ext.data.TreeStore', {
    model: 'Thing',

    // example service response (in reality, this would be JSON)
    root: {
        children: [
            {
                id: 1,
                name: 'Thing 1',
            },
            {
                id: 2,
                name: 'Thing 2',
            },
            {
                id: 3,
                name: 'Thing 3',
            },
        ],
    },

    listeners: {
        append: function(thisNode, newChildNode, index, eOpts) {
            if( !newChildNode.isRoot() ) {
                newChildNode.set('text', newChildNode.get('name'));
            }
        },
    },
});

Ext.create('Ext.tree.Panel', {
    renderTo: Ext.getBody(),
    store: store,
    rootVisible: false,
});
如您所见,该服务只返回对象的
id
name
,而不返回
children
leaf
等树节点信息,这正是
Ext.data.TreeStore
通常所期望的

我无法更改服务作为响应返回的内容。我喜欢调用服务并获得一个平面数组,而不需要额外的树节点信息。有许多与这项服务对话的应用程序根本不需要这些数据,我也没有权力允许我更改这些数据

不幸的是,如果没有为响应中的每件事定义
子元素
,当我尝试扩展节点时,Ext会引发以下错误(您可以自己在JSFIDLE中生成此错误):


因此,我的问题是:我如何避免在回复中发回
子项
(或
),并解决此错误?我只希望我的东西能被加载到树中并可扩展(是的,它们不能是树叶)。

自己添加这些信息

小示例(您可以通过浏览器在控制台中轻松试用):

例如,在商店的afterRequest方法中,您可以执行以下操作:

proxy: {
    type: 'ajax',

    url: 'xxx',

    listeners: {
        exception: function(proxy, response, options) {
            //error case
        }
    },

    afterRequest: function(request, success) {
        var resText = request.operation.response.responseText,
            allThings = Ext.decode(resText),  //Decodes (parses) a JSON string to an object
            things = allThins.children,       //Now we have an object an we can do what we want...
            length = things.length;

        for (var i = 0; i < length; i++) {
            things[i].leaf = true;
            things[i].expanded = true;
        }

        this.setRootNode(allThings); 
    }
}
代理:{
键入:“ajax”,
url:'xxx',
听众:{
例外:函数(代理、响应、选项){
//错误案例
}
},
后请求:功能(请求,成功){
var resText=request.operation.response.responseText,
allThings=Ext.decode(resText),//将JSON字符串解码(解析)为对象
things=allThins.children,//现在我们有了一个对象,我们可以做我们想做的事情。。。
长度=事物的长度;
对于(变量i=0;i
这只是一些伪代码,但它应该可以工作! 设置一个调试器语句(),看看你真正得到了什么


我希望这有帮助

您可以尝试迭代响应存储并创建子节点。 然后将子节点附加到根节点

store.each(function(rec) {  
  var childNode ;  
  //Create a new object and set it as a leaf node (leaf: true)  
  childNode = Ext.create('Model_Name', {  
       id: rec.data.id,  
       name: rec.data.name,
       text : rec.data.name,  
       leaf : true,  
  });  
  // add/append this object to your root node  
  treepanel.getRootNode().appendChild(childNode );  
 }); 
查看更多详细信息

proxy: {
    type: 'ajax',

    url: 'xxx',

    listeners: {
        exception: function(proxy, response, options) {
            //error case
        }
    },

    afterRequest: function(request, success) {
        var resText = request.operation.response.responseText,
            allThings = Ext.decode(resText),  //Decodes (parses) a JSON string to an object
            things = allThins.children,       //Now we have an object an we can do what we want...
            length = things.length;

        for (var i = 0; i < length; i++) {
            things[i].leaf = true;
            things[i].expanded = true;
        }

        this.setRootNode(allThings); 
    }
}
store.each(function(rec) {  
  var childNode ;  
  //Create a new object and set it as a leaf node (leaf: true)  
  childNode = Ext.create('Model_Name', {  
       id: rec.data.id,  
       name: rec.data.name,
       text : rec.data.name,  
       leaf : true,  
  });  
  // add/append this object to your root node  
  treepanel.getRootNode().appendChild(childNode );  
 });