ExtJs 3 TreePanel排序不起作用

时间:2013-06-06 06:36:31

标签: sorting extjs treegrid

我正在使用Ext.ux.tree.TreeGrid树格网面板。一切都很好,除了排序。 我有3级的层次结构,如 - 父母 - 孩子 - 大孩子。我想仅根据父文本进行排序。但我每次都得到随机结果。 :(

这是我的代码 -

var tree_grid = new Ext.ux.tree.TreeGrid({
        title : 'Requirements',
        height : 415,
        enableDD : true,
        enableHdMenu : true,
        id : 'req_tree',
        columns : [ {
            header : 'Entity',
            dataIndex : 'text',
            width : 200,
            sortable: true,
            sortType : 'asText'
        }, {
            header : 'Text',
            width : 50,
            dataIndex : 'temp',
            align : 'center',
            // sortType : 'asFloat',
            sortable: false
        }],
        dataUrl : 'my_page.php'
});

为了排序,我试过了 -

1) var myTreeSorter = new Ext.tree.TreeSorter(tree_grid, {});
   myTreeSorter.doSort(tree_grid.getRootNode());
2) new Ext.ux.tree.TreeGridSorter(tree_grid, {
     folderSort: true,
     dir: "desc",
     sortType: function(node) {
         // sort by a custom, typed attribute:
         return parseInt(node.id, 10);
     }
 });


3) Used Attributes like - sortType, sortable, sortInfo.

然而,以上都没有帮助我。请帮忙。

2 个答案:

答案 0 :(得分:2)

    var mySortStore = Ext.create("Ext.data.Store", {
        model: "MyTreeStore",
        data:  []
    });

Then when you go to add the node to the tree, use this function instead:

function addNodeSorted(node, childToBeAdded){
    //clear the store from previous additions
    mySortStore.removeAll();
    //remove old sorters if they are dynamically changing
    mySortStore.sort();
    //add the node into the tree
    node.appendChild(childToBeAdded);
    var cn = node.childNodes,
        n;

    //remove all nodes from their parent and add them to the store
    while ((n = cn[0])) {
        mySortStore.add(node.removeChild(n));
    }

    //then sort the store like you would normally do in extjs, what kind of sorting you want is up to you
    mySortStore.sort('height', 'ASC');//this is just an example

    //now add them back into the tree in correct order
    mySortStore.each(function(record){
        node.appendChild(record);
    });
}

答案 1 :(得分:0)

您是否尝试在商店中使用配置 folderSort

TreeGrid示例:sencha TreeGrid

var store = Ext.create('Ext.data.TreeStore', {
        model: 'Task',
        proxy: {
            type: 'ajax',
            //the store will get the content from the .json file
            url: 'treegrid.json'
        },
        **folderSort: true**
    });

-

其他解决方案,可以在商店使用排序过滤器:

商店排序:sencha sort

//sort by a single field
myStore.sort('myField', 'DESC');

//sorting by multiple fields
myStore.sort([
    {
        property : 'age',
        direction: 'ASC'
    },
    {
        property : 'name',
        direction: 'DESC'
    }
]);