2

我正在尝试在具有两个主要组件的 EXTJ 中创建一个网页:

  1. 表单(Ext.form.Panel)
  2. 面板(Ext.tree.Panel)

该表单应该获取一些值,这些值应该填充第二个面板中的树。在第一个面板的按钮处理程序中,我可以访问更新的 JSON 对象,但我无法找到刷新 TreeStore 的方法,该方法将更新 tree.Panel 中的显示。

这是我到目前为止所拥有的:

Ext.define('TreeServiceData',{
    config:{
    data : ''
    },print : function() {
    console.log("Printing data: ")
    console.log(this.data.children[0])
    }
});
var dataObject = Ext.create('TreeServiceData');
dataObject.setData({'expanded':false,'children':[{'text':'Master','expanded':true,'leaf':false,'children':[{'text':'Child 1','expanded':true,'leaf':false,'children':[{'text':'Child 2','expanded':true,'leaf':false,'children':[{'text':'Child 3','expanded':false,'leaf':false}]}]}]}]})


Ext.define('TreeStoreData',{
    extend: 'Ext.data.TreeStore',
    model: 'TaskModel',
    autoLoad: true,
    autoSync: true,
    proxy: {
        type:'memory',
        reader: {
            type:'json'
        }
    },
    root:dataObject.getData()
});

var treeStore = Ext.create('TreeStoreData');

现在我正在尝试在按钮调用上更新和显示这个树存储的值,如下所示:

buttons:[
{
    text:'Get CCP/Product',
    handler:function (btn, evt) {
        dataObject.print();
        treeStore.removeAll();           
        dataObject.setData({'expanded':false,'children':[{'text':'Master11','expanded':true,'leaf':false,'children':[{'text':'Child 12','expanded':true,'leaf':false,'children':[{'text':'Child 23','expanded':true,'leaf':false,'children':[{'text':'Child 34','expanded':false,'leaf':false}]}]}]}]})
        dataObject.print();

    }
}
]

但是在这个按钮处理程序上,我总是在 treeStore.removeAll() 方法上得到一个“Uncaught TypeError: Cannot call method 'indexOf' of undefined”,在这个上下文中明确定义了 treestore。

问题 1) 刷新 TreeStore 的正确方法是什么?

4

2 回答 2

2

答案 1)

代替:

treeStore.removeAll();
dataObject.setData( ... );

你应该做:

dataObject.setData( ... ); // This won't affect the store
treeStore.setRootNode(dataObject.getData()); // Actually update the store

请注意,更改 dataObject 的数据不会像您想象的那样自动影响存储...

于 2013-05-07T19:04:45.553 回答
0

此代码适用于我(ExtJS 4.2.1)总树面板节点刷新示例:

    var responseDictObjects = $.ajax({
        data: { Id: this.idDictionary },
        dataType: "json",
        type: "POST",
        cache: false,
        url: 'http://' + config.domain + '/' + 'api/Dictionaries/GetDictTreeData',
        async: false
    }).responseText;

    responseDictObjects = jQuery.parseJSON(responseDictObjects);


    while (this.storeDict.getRootNode().firstChild) {
        this.storeDict.getRootNode().removeChild(this.storeDict.getRootNode().firstChild);
    }
    this.storeDict.getRootNode().appendChild(responseDictObjects.Data);

将 this.storeDict 替换为您的商店参考。

就我而言:

        JSON.stringify(responseDictObjects.Data)

返回

"[{"id":8,"text":"kkk","leaf":false,"expanded":true,"children":null},{"id":17,"text":"ttttt" ,"叶子":false,"扩展":true,"children":null},{"id":22,"text":"gggg","叶子":false,"expanded":true,"children" :null},{"id":23,"text":"qqq","leaf":false,"expanded":true,"children":null},{"id":24,"text":" fff","leaf":false,"expanded":true,"children":null},{"id":27,"text":"fff","leaf":false,"expanded":true,"孩子":null},{"id":28,"text":"gggggggggggggggggggg","叶”:假,“扩展”:真,“孩子”:null},{“id”:31,“文本”:“ttttttttttt666666666”,“叶”:假,“扩展”:真,“孩子”:空},{"id":32,"text":"ffffffffffffffffffff","leaf":false,"expanded":true,"children":null},{"id":33,"text":"kkkkkkkkkkkkk" “叶子”:假,“扩展”:真,“孩子”:null},{“id”:35,“文本”:“7777777777”,“叶子”:假,“扩展”:真,“孩子” :null},{"id":36,"text":"999999999999","leaf":false,"expanded":true,"children":null},{"id":37,"text":" iii","叶子":false,"展开":true,"儿童":null}]"


我发现 TreePanel 的另一个错误,执行 appendChild 后出现先前删除的节点。所以我开始使用 jquery 树插件(dynatree)。您只需要创建空面板。在渲染面板后,嵌入树,在我的例子中:

创建空面板:

    that.treePanel = Ext.create('Ext.panel.Panel', {
        title: 'Records',
        width: 350,
        height: 400
    });

渲染面板后,您可以随时刷新节点:

    var that = this;

    var responseDictObjects = $.ajax({
        data: { Id: this.idDictionary },
        dataType: "json",
        type: "POST",
        cache: false,
        url: 'http://' + config.domain + '/' + 'api/Dictionaries/GetDictTreeData',
        async: false
    }).responseText;

    responseDictObjects = jQuery.parseJSON(responseDictObjects);

    var el = this.treePanel.getId();

    if (this.treeDict == null) {
        this.treeDict = $('#' + el).dynatree({
            onActivate: function (node) {
                that.treeDict.lastSelectedId = node.data.index;
            },
            children: []
        });
    }
    this.treeDict.dynatree("getRoot").removeChildren(true);
    this.treeDict.dynatree("getRoot").addChild(responseDictObjects.Data);
于 2013-08-18T15:59:42.510 回答