2

Once you define custom sorting for a column like in Github and UI-Grid

How can you access the column from inside the algorithm?

var myAwesomeSortFn = function(a,b, rowA, rowB, direction){

       // "Need to access the name (field) of column being sorted here";
        var column = "No Idea"

       console.log("sorting by column " + column );

        if (a == b) return 0;
        if (a < b) return -1;
        if (a > b) return 1;


    };  
MishaT
  • 35
  • 8

1 Answers1

1

You could try the following...

{ field: 'lastName', displayName: 'Last Name', sortingAlgorithm: MyService.getSortingAlgorithm('lastName') },

Then define in a service (or in your scope if you prefer)

getSortingAlgorithm: function (columnName) {
    return function(a, b, rowA, rowB, direction) {
        console.log("sorting by column " + columnName);

        if (a == b) return 0;
        if (a < b) return -1;
        if (a > b) return 1;
    };  
}
S. Baggy
  • 950
  • 10
  • 21
  • This is what I'm currently doing, it works, but also means lots of time when you have over 40 columns, plus the maintainability cost when fields change. – MishaT Sep 29 '16 at 14:52
  • I'll mark this as accepted since it seems to be the only way to do it without modifying ui-grid, which we ended up doing to expose the column. – MishaT Oct 12 '16 at 22:40