dgrid custom sort issue

2019-09-07 20:12发布

问题:

I'm trying to override the sort logic in dgrid as suggested by kfranqueiro in this link - https://github.com/SitePen/dgrid/issues/276.

I get the data from the server in sorted order and just want to update the UI of column header. I'm doing this -

On(mygrid, 'dgrid-sort', lang.hitch( this,function(event){
    var sort = event.sort[0];
    var order = this.sort.descending ? "descending" : "ascending";
    console.log("Sort "+ this.sort.property + " in " +order+" order.");
    event.preventDefault();
    mygrid.updateSortArrow(event.sort, true);
    myFunctionToRefreshGrid();
}));
...
myFunctionToRefreshGrid: function() {
   ...//get data from server in sorted order
   var mystore = new Memory({data: sortedDataFromServer, idProperty: 'id'});
   mygrid.set("collection", mystore);
   ...
}

Memory here is "dstore/Memory". I'm using dgrid 0.4, dstore 1.1 and dojo 1.10.4

Before calling set('collection',...) I see that sortedDataFromServer is in the desired sorted order. But for some reason, the order in the grid is different. For example, when sorted in descending order, I see that the values starting with lower case appear first in descending order and then the values starting with upper case appear in sorted order. It looks like dstore is doing something more.

What could be going on? Am I doing something wrong? Is there a different/better way to do custom sorting?

Thanks,

回答1:

This is how I ended up addressing the situation - As suspected, the collection/store was further sorting my data and hence the inconsistency. I customized the store (Memory) as shown below and use the custom store when setting data to my grid.

var CustomGridStore = declare([Memory],{
    sort: function (sorted) {
        sorted = [];//Prevent the collection from sorting the data
        return this.inherited(arguments);
    }
});


回答2:

I think you are doing the right thing, only problem here is that you are not resetting sort property of grid, one you re-initiate memory with sorted order, it get's sorted automatically

after you are calling

event.preventDefault();

call this

  mygrid.set("sort", null);

I am doing custom sorting in my one of grid as following

                self.xrefGrid.on("dgrid-sort", function (event) {
                var sort = event.sort[0];
                    event.preventDefault();
                    self.xrefGrid.set('sort', function (a, b) {
                        var aValue,bValue;
                        if (a[sort.attribute] && typeof a[sort.attribute] == "string")
                             aValue = a[sort.attribute].toLowerCase();
                        if (b[sort.attribute] && typeof b[sort.attribute] == "string")
                             bValue = b[sort.attribute].toLowerCase();
                        var result = aValue > bValue ? 1 : -1;
                        return result * (sort.descending ? -1 : 1);
                    });
                    self.xrefGrid.updateSortArrow(event.sort, true);
            });