ExtJS 4: TreeStore with both static and dynamically-loaded data?

前端 未结 5 1321
慢半拍i
慢半拍i 2021-02-04 19:18

I\'m making a TreePanel that looks like this:

\"enter

At the moment I have it \"mo

5条回答
  •  甜味超标
    2021-02-04 19:45

    Here is my function to convert normal data to treeStore data, you can use that. By that way, you don't need treeStore anymore:

    Records: array of record. Text: name of item (get from record) Children: name of children (default is 'children')

    dynamicReportsStore.load({
        scope: this,
        callback: function (records, operation) {
            if (operation.isComplete()) {
                var tree = this.buildTreeByRecords(records, 'name');
                treePanel.getRootNode().removeAll();
                treePanel.getRootNode().appendChild(tree);
            }
        }
    });
    
    
    
    buildTreeByRecords: function (records, text, children) {
    var childs = [],
        results = [],
        tree = [];
    records = Ext.Array.map(records, function (record) {
        return {
            text: record.get(text) || record.get('id'),
            leaf: record.get('leaf'),
            expanded: true,
            parentId: record.get('parentId'),
            id: record.get('id')
        };
    }, this);
    Ext.each(records, function (record) {
        if (Ext.isEmpty(childs[record.parentId])) {
            childs[record.parentId] = [];
        }
        childs[record.parentId].push(record);
    }, this);
    Ext.each(records, function (record) {
        if (!Ext.isEmpty(childs[record.id])) {
            record[children || 'children'] = childs[record.id];
            results.push(record);
        }
    }, this);
    Ext.each(results, function (result) {
        if (result.parentId === 0) {
            tree.push(result);
        }
    }, this);
    return tree;}
    

提交回复
热议问题