Extjs如何将新节点添加到选定节点

时间:2019-05-13 15:05:36

标签: extjs

我不知道这是否是尝试按需填充树的正确方法,但这是我所知道的方法,我不能说官方文档会有所帮助。 我有一棵树,到目前为止,第一个节点正在从数据库中加载。然后,如果我双击一个节点,则在控制器中执行以下操作:

function(item, record, index) {
        var selectedId = record.data.idelement;
        var newStore = Ext.create('mycomponent.mystore', {
            autoDestroy: true,
            idparent: selectedId
        });
        newStore.proxy.extraParams = {idparent: selectedId};
        newStore.removeAll(true);
        newStore.load();
}

我可以知道load函数正在从浏览器的“网络”标签中的数据库中获取正确的数据。视图中什么也没有发生,树保持不变。这是我的商店:

Ext.define('mycomponent.mystore', {
    extend: 'Ext.data.TreeStore',
    alias: 'store.datatree',
    storeId: 'datatree',
    model: Ext.define('TreeNode', {
        extend: 'Ext.data.Model',
        fields: [{
            name: 'text',
            convert: function(v, r) {
                return r.data.text;
            }
        }]
    }),
    restful: true,
    autoLoad: false,
    // autoDestroy: true,
    root: {
        text: 'Loading...',
        id: 'NULL',
        expanded: true
    },
    listeners: {
        'load': function(store, records, successful, operation, eOpts) {
            var children = [];
            for (var i = 0, l = records.length; i < l; i++) {
                var x = records[i].data;
                var child = {
                    idelement: x.idelement,
                    name: x.name,
                    descript: x.descript,
                    active: x.activeo,
                    idparent: x.idparent,
                    text: x.name,
                    leaf: x.isleaf,
                    children: []
                };
                children.push(child);
            }

            if (!store.idpadre) {
                store.loadData(children, false);
            } else {
                var tree = Ext.getCmp('mytree');
                var oldStore = tree.store;
                var idparent = store.idparent;

                var node = oldStore.getRootNode().findChild('idelement', idparent, true);

                if (node && !node.leaf) {
                    node.expand();
                    node.appendChild(
                        {
                            idelement: x.idelement,
                            name: x.name,
                            descript: x.descript,
                            active: x.activeo,
                            idparent: x.idparent,
                            text: x.name,
                            leaf: x.isleaf,
                            children: []
                        }
                    );
                }
            }
        }
    },

    proxy: {
        type: 'ajax',
        headers: {
           'Accept': '*/*',
           'Cache-Control': 'no-cache',
           'Content-Type': 'application/json',
           'Authorization': localStorage.token
        },
        extraParams: {
           'filter[active]': true,
           'filter[idparent][null]': 0
        },

        reader: {
            type: 'json',
            rootProperty: 'data',
            successProperty: 'success'
        },

        api: {
           read: 'myurl',
           create: 'myurl',
           update: 'myurl',
           destroy: 'myurl'
        },

        autoSave: true
    },
    constructor: function (config) {
        config = Ext.apply({
            rootProperty: Ext.clone(this.rootData)
        }, config);

        this.callParent([config]);
    }
});

这也给我这个错误:

Uncaught TypeError: Cannot read property 'isVisible' of null
    at constructor.onChildNodesAvailable (ext-all-debug.js:109350)
    at Object.callback (ext-all-debug.js:8705)
    at constructor.onChildNodesAvailable (ext-all-debug.js:110968)
    at Object.callback (ext-all-debug.js:8705)
    at constructor.onProxyLoad (ext-all-debug.js:111930)
    at constructor.triggerCallbacks (ext-all-debug.js:80621)
    at constructor.setCompleted (ext-all-debug.js:80589)
    at constructor.setSuccessful (ext-all-debug.js:80602)
    at constructor.process (ext-all-debug.js:80501)
    at constructor.processResponse (ext-all-debug.js:89534)

我要在存储load事件中执行的操作是将一个静态节点添加为所选节点的子节点,以进行测试,并在该位置将restapi的真实数据放入其中,但是我做不到根据我的逻辑,这应该有效,但事实并非如此,那么我在做什么错呢?我在这里想念什么?

0 个答案:

没有答案