So, I have a dojo script wherein I'm pushing 4 rows into a data grid. Something like this,
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'});
Then, I have a functionality where I use
dojo.forEach(grid.store._arrayOfAllItems, function (item) {
//do something
}
But before the forEach loop is called, I need to sort the items with respect to col3
Now, to sort the items based on col3, I've tried
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);
But, even then,
for(i = 0; i < grid.store._arrayOfAllItems.length; i++)
{
var items = grid.store._arrayOfAllItems;
document.write(grid.store.getValue(items[i], "col3")+"<br>");
}
Prints the unsorted arrangement of items. I've also tried using
grid.store.fetch({onComplete: displaySortItems , sort: [{attribute: "col3"}] });
But turns out fetch() does not return anything fetch() FAQ and even when I tried sorting the items in the displaySortItems() it's not being reflected beyond the scope of the function.
So, is there any way I can get a sorted array(based on col3) of items when grid.store._arrayOfAllItems
is used.
or
Can I sort the items array and make the changes to hold even when grid.store._arrayOfAllItems
is used. Please help!