1

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!

HeXMaN
  • 113
  • 1
  • 11

2 Answers2

0

You need to write callback functions to use fetch(). The documentation for ItemFileReadStore provides an example for custom sorting. At a minimum you need a function for onComplete and 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});

UPDATE: fetch() sorts while it delivers the items and not internally. If you want to sort again you can call fetch() again, providing another function for onComplete.

In dojo properties that start with '_', (like _arrayOfAllItems) are considered 'private'. You can still access them but it is not a good idea to modify them. ItemFileWriteStore will manage it internally. If you want a sorted array you can built your own copy in a function you pass to fetch().

pgianna
  • 715
  • 1
  • 7
  • 16
  • As I have mentioned, the `fetch()` and `onComplete` along with `sort: sortAttributes` sorts the items[] just fine. But my problem is that the sorted items[] array does not remain sorted after `store.fetch()` call. Try printing all the item values as `grid.store.getValue(items[i], "col3")` after the `store.fetch();`. They are unsorted again! – HeXMaN Mar 07 '17 at 06:01
  • `fetch()` stores while it delivers the items and not internally. If you want to sort again you can call `fetch()` again providing another function for `onComplete`. – pgianna Mar 07 '17 at 07:01
  • So, is there no other way I will be able to sort the items[] such that `grid.store._arrayOfAllItems` returns a sorted array ? :( – HeXMaN Mar 07 '17 at 07:09
  • I updated the answer. If you want a sorted array the best option is to create a separate copy. – pgianna Mar 07 '17 at 07:51
  • I have already tried what you said. Copied the `items[]` into `newItems[]`. Sorted the `newItems[]` and then `grid.setItems(newItems);`. But in vain. The grid still prints out the unsorted items[] data. Please please help! – HeXMaN Mar 07 '17 at 08:42
  • Grids have their own sorting. In DataGrid you can set the sortInfo property. Or you can call `grid.setSortIndex(3,true); grid.sort();` to sort the third colcumn. – pgianna Mar 07 '17 at 12:20
0

You can declare the sort order when you initialize the grid.

var grid = new Grid({
    sortInitialOrder: [{colId: "Genre", descending: true},{colId: "Artist", descending: true}]
});
Stefano
  • 293
  • 1
  • 4
  • 22
  • But I don't want to sort the grid initially, I only want the sorted grid(w.r.t **col3**) before calling `dojo.forEach(grid.store._arrayOfAllItems, function (item) {}` – HeXMaN Apr 12 '17 at 05:13
  • Try using a function for sorting arrays of object. – Stefano Apr 12 '17 at 07:39
  • As mentioned in the question, I have already done that. But the grid does not stay sorted after the scope of the function. – HeXMaN Apr 12 '17 at 07:44