dgrid custom sort with secondary sort column

1k Views Asked by At

I'm currently using a custom sort function on a dgrid (pasted below). It doesn't change sorting drastically, just sorts one particular column uniquely and sorts the others case-insensitive. I'd like to add a secondary sort by a column named "scheduled" to be added to the sort when any other column is sorted. I'm just not sure how to go about it. I've seen how to override the sort to sort by two columns, but not when a custom sort is in play. The secondary sort would always be there, not matter what other column is clicked.

For reference I'm running dojo 1.10 and dgrid 1.0. Data is coming from a RequestMemory DStore and I'd really rather this sort happen on the grid rather than back at the store level. Any help would be appreciated.

currGrid.on('dgrid-sort', function (event) {
  event.preventDefault();
  var sort = event.sort[0];
  currGrid.set('sort', function (a, b) {
     if (sort.property == "thisField") {
       //special sort for thisField
       if (a[sort.property] !== 'undefined' && typeof a[sort.property] == "string") {
          var colorA =  a[sort.property].split("|");
          var aValue = colorA[0].toLowerCase();
       }
       if (b[sort.property] !== 'undefined' && typeof b[sort.property] == "string") {
          var colorB =  b[sort.property].split("|");
          var  bValue = colorB[0].toLowerCase();
       }
       if (String(aValue) == String(bValue)) {
          var result = 0;
       } else if (dojo.string.trim(aValue) == "") {
          var result = true ? 1 : -1;
       } else if (dojo.string.trim(bValue) == "") {
         var result = true ? -1 : 1;
       } else {
         var result = aValue > bValue ? 1 : -1;
       }
       return result * (sort.descending ? -1 : 1);
     } else {
       //Sort for all other fields same as always (except toLowerCase)
       if (a[sort.property] !== 'undefined' && typeof a[sort.property] == "string") {
          var aValue = a[sort.property].toLowerCase();
       } else {
          var aValue = "";
       }
       if (b[sort.property] !== 'undefined' && typeof b[sort.property] == "string") {
          var bValue = b[sort.property].toLowerCase();
       } else {
          var bValue = "";
       }
       var result = aValue > bValue ? 1 : -1;
       return result * (sort.descending ? -1 : 1);
     }
  });
  currGrid.updateSortArrow(event.sort, true);
});
currGrid.startup();

1

There are 1 best solutions below

3
On BEST ANSWER

You could do something like below.

currGrid.on('dgrid-sort', function (event) {
    event.preventDefault();
    var sortSet = [];
    sortSet.push(event.sort[0]);
    sortSet.push({property: "scheduled"});
    currGrid.set('sort', function (a, b) {
        var aValue, bValue, result = 0;
        for(var i = 0; i < sortSet.length; i++){
            var sort = sortSet[i];
            if (sort.property == "thisField") {
                //special sort for thisField
                if (a[sort.property] !== 'undefined' && typeof a[sort.property] == "string") {
                    var colorA =  a[sort.property].split("|");
                    aValue = colorA[0].toLowerCase();
                }
                if (b[sort.property] !== 'undefined' && typeof b[sort.property] == "string") {
                    var colorB =  b[sort.property].split("|");
                    bValue = colorB[0].toLowerCase();
                }
                if (String(aValue) == String(bValue)) {
                    result = 0;
                } else if (dojo.string.trim(aValue) == "") {
                    result = true ? 1 : -1;
                } else if (dojo.string.trim(bValue) == "") {
                    result = true ? -1 : 1;
                } else {
                    result = aValue > bValue ? 1 : -1;
                }
                return result * (sort.descending ? -1 : 1);
            } else {
                //Sort for all other fields same as always (except toLowerCase)
                if (a[sort.property] !== 'undefined' && typeof a[sort.property] == "string") {
                    aValue = a[sort.property].toLowerCase();
                } else {
                    aValue = "";
                }
                if (b[sort.property] !== 'undefined' && typeof b[sort.property] == "string") {
                    bValue = b[sort.property].toLowerCase();
                } else {
                    bValue = "";
                }
                //You need this check here 
                if(aValue != bValue){
                    result = aValue > bValue ? 1 : -1;
                    return result * (sort.descending ? -1 : 1);
                }
            }
        }
        return 0;
    });
    currGrid.updateSortArrow(event.sort, true);
});
currGrid.startup();  

I have some concerns about your code, the variables result, aValue and bValue are all local within the if statement and yet they are being used outside the statement. It could result in wrong results if some other variables are defined with the same name in global space. So I have modified them.

So the second section you needed to check if aValue == bValue to return 0.