对道场商店物品进行排序

问题描述 投票:0回答:2

我有一个 Dojo 脚本,其中我将 4 行推入数据网格中。像这样的东西,

var data = {
            identifier: "id",
            items: []
            };

var data_list = [
        { col1: "normal", col2: false, col3: 'Cut are not followed by two hexadecimal', col4: 29.91},
        { col1: "important", col2: false, col3: 'Decause a % sign always indicates', col4: 9.33},
        { col1: "important", col2: false, col3: 'Aigns can be selectively', col4: 19.34},
        { col1: "normal", col2: false, col3: 'Begin the action', col4: 5.6}
        ];

var rows = 4;

for(var i = 0, l = data_list.length; i < rows; i++)
{
    data.items.push(lang.mixin({ id: i+1 }, data_list[i%l]));
}

var store = new ItemFileWriteStore({data: data});
        
/*set up layout*/
var layout = [[
        {name: 'Column 1', field: 'id', width: '100px'},
        {name: 'Column 2', field: 'col2', width: '100px'},
        {name: 'Column 3', field: 'col3', width: '200px'},
        {name: 'Column 4', field: 'col4', width: '150px'}
        ]];
    
/*create a new grid*/
var grid = new DataGrid({
            id: 'grid',
            store: store,
            structure: layout,
            rowSelector: '20px'});

然后,我就有了我使用的功能

dojo.forEach(grid.store._arrayOfAllItems, function (item) {
//do something
}

但是在调用 forEach 循环之前,我需要根据 col3

对项目进行排序

现在,要根据 col3 对项目进行排序,我已经尝试过

            var noOfItems = store._arrayOfAllItems.length;
            var items = new Array(noOfItems);
            for(i = 0; i < noOfItems; i++)
            {
                items[i] = grid.getItem(i);
            }

            for(i = 0; i < noOfItems; i++)
            {
                for(j = 0; j < noOfItems - 1; j++)
                {
                    var a = store.getValue(items[j], "col3");
                    var b = store.getValue(items[j+1], "col3");
                    
                    if(a.localeCompare(b) == 1)
                    {
                        [items[j], items[j+1]] = [items[j+1], items[j]];
                    }
                }
            }

            grid.setItems(items);

但是,即便如此,

 for(i = 0; i < grid.store._arrayOfAllItems.length; i++)
 {
    var items = grid.store._arrayOfAllItems;
    document.write(grid.store.getValue(items[i], "col3")+"<br>");
 }

打印未排序的项目排列。 我也尝试过使用

grid.store.fetch({onComplete: displaySortItems , sort: [{attribute: "col3"}] });

但事实证明 fetch() 不返回任何内容 fetch() FAQ 即使当我尝试对 displaySortItems() 中的项目进行排序时,它也没有反映到函数的范围之外。

那么,当使用grid.store._arrayOfAllItems时,有什么方法可以得到项目的排序数组(基于

col3
)?

或者我可以对项目数组进行排序并使更改即使在使用

grid.store._arrayOfAllItems
时也能保留吗?

javascript dojo dojox.grid.datagrid dojox.grid
2个回答
0
投票

您需要编写回调函数才能使用 fetch()。 ItemFileReadStore 的文档提供了一个自定义排序的示例。 至少您需要一个 onComplete 和 onError 函数。

var gotItems = function(items, request) {
    // Process your items here
    for(i = 0; i < items.length; i++) {
    console.log(items[i]);
    }
};

var fetchFailed = function(error, request) {
    alert("Fetch failed");
};

var sortAttributes = [{attribute: "col3", descending: false}];

store.fetch({query: {}, onComplete: gotItems, onError: fetchFailed, sort: sortAttributes});

更新

fetch()
在交付物品时进行排序,而不是在内部进行排序。如果你想再次排序,你可以再次调用
fetch()
,为
onComplete
提供另一个函数。

在 dojo 中,以“_”开头的属性(如 _arrayOfAllItems)被视为“私有”。您仍然可以访问它们,但修改它们不是一个好主意。 ItemFileWriteStore 将在内部管理它。如果您想要一个排序数组,您可以在传递给

fetch()
的函数中构建自己的副本。


0
投票

您可以在初始化网格时声明排序顺序。

var grid = new Grid({
    sortInitialOrder: [{colId: "Genre", descending: true},{colId: "Artist", descending: true}]
});
© www.soinside.com 2019 - 2024. All rights reserved.