如何在单击按钮时重新启动extjs中的树

时间:2017-05-28 10:17:51

标签: javascript extjs

我有一个按钮,可以计算我的TreeStore中的一些项目。当我点击它时,我想重新加载我的树。但是现在当我点击一个项目进行计算时,在所选项目下创建另一棵树,然后重新加载新树。如何为此问题编写适当的代码?

单击计算按钮时,这是我的功能:

'g-infitmformula #calculateFormula': {
            click: function (sender) {
                var selectedinfoIdList = Ext.ComponentQuery.query("g-infitmformula")[0].getSelectionModel().getSelection();
                Ext.each(selectedinfoIdList, function (item) {
                    informationItemIds.push(item.data.id);
                });
                if (selectedinfoIdList.length > 0) {
                    Dufo.Func.loadMask(Ext.getBody(), true);
                    Ext.Ajax.request({
                        url: '/sis/rest/infoit/calculateFormula',
                        jsonData: {
                            infoItemStrings: informationItemIds
                        },
                        success: function (result, request) {
                            var data = Ext.JSON.decode(result.responseText).ReturnValue;
                            Ext.Msg.alert('', data.description);
                            Dufo.Func.loadMask(Ext.getBody(), false);
                            if (data.successful === true) {
                                // Ext.ComponentQuery.query('g-infitmformula')[0].store.reload();
                                Ext.ComponentQuery.query("g-infitmformula")[0].store.reload({
                                    params: {
                                        id: "root",
                                        dtstart: dtstartVar,
                                        dtend: dtendVar,
                                        infitmcat: infitmcatVar
                                    }
                                });
                                informationItemIds = [];
                            }
                            else {
                                informationItemIds = [];
                                Ext.Msg.alert('', data.description);
                            }
                        },
                        failure: function (result, request) {
                            Dufo.Func.loadMask(Ext.getBody(), false);
                            informationItemIds = [];
                            Ext.Msg.alert('', Ext.JSON.decode(result.responseText).ReturnValue.description || Dufo.ux.fa.jsonFormatFail);
                        }
                    });
                } else {
                    Ext.Msg.alert('', Dufo.ux.fa.selectSomething);
                }
            }
        }

这是我要重新加载的商店类:

Ext.define('Dufo.store.InformationItemsFormula', {
  extend: 'Ext.data.TreeStore',
  storeId: 'infitmformula',
  model: 'Dufo.model.InformationItemsFormula',
  autoLoad: false,
  nodeParam: 'id',
  clearOnLoad: true,
  listeners: {
    append: function (parent, child, index, eOpts) {
        var me = this;
        if (!child.isRoot()) {
            child.set('leaf', child.get('isLeaf'));
            if (child.data.children) {
                var root = me.tree.treeStore.getRootNode();
                child.data.children.forEach(function (test) {
                    var n = root.createNode(test);
                    child.appendChild(n);
                });
            }
        }
    }
  },

  proxy: {
    type: 'ajax',
    url: '/sis/rest/infitmval/gtinffmlitmval',
    actionMethods: {
        read: 'POST', create: 'POST'
    },
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json; charset=utf-8'
    },
    jsonData: true,
    reader: {
        type: 'json',
        root: function (o) {


            if (o.ReturnValue) {
                if(o.ReturnValue.detail.length) {
                    Ext.ComponentQuery.query("#calculateFormula")[0].setDisabled(false);
                }else {
                    Ext.ComponentQuery.query("#calculateFormula")[0].setDisabled(true);
                }

                for (var i=0; i < o.ReturnValue.detail.length; i++) {
                    o.ReturnValue.detail[i].id = o.ReturnValue.detail[i].id + '-' +Dufo.store.TreeStoreIDGenerator.generateId();
                }

                return o.ReturnValue.detail;
            }
        }
    }
  }

});

1 个答案:

答案 0 :(得分:0)

您下面的代码正在创建问题,因为您明确地创建了相同的节点。这些节点也将由Extjs it self创建。

listeners: {
    append: function (parent, child, index, eOpts) {
        var me = this;
        if (!child.isRoot()) {
            child.set('leaf', child.get('isLeaf'));
            if (child.data.children) {
                var root = me.tree.treeStore.getRootNode();
                child.data.children.forEach(function (test) {
                    var n = root.createNode(test);
                    child.appendChild(n);
                });
            }
        }
    } 

删除此代码,因为append侦听器可用于执行某些操作而不是新创建的节点。这基本上是来自Called from a node's appendChild method