Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to disable multi-column sort in ui-grid

I have a client who specifically does not like the numbers next in the headers of the columns when doing a sort. This is rooted in UI-Grid's multi-sort, which gives each column a numbered priority. Is there a way to disable the multi-sort in order to remove those numbers? I still want to keep sorting activated, but only on one column at a time. Thanks.

like image 299
Michael Davidson Avatar asked Nov 17 '15 16:11

Michael Davidson


1 Answers

I've had this problem myself. If you look carefully in the ui0grid.js code you'll see that there is (at this time) no option to diable it. The writers of ui-grid state that they would welcome a request for such a function in this thread

However, you want a fix, not a promise ;-)

You can spot how many sortColumns have been chosen in the sortChanged method.

Try something like this:

$scope.gridOptions.onRegisterApi = function(gridApi) {
    $scope.gridApi = gridApi;

    // Register a handler that is fired everytime someone changd the sort params.
    $scope.gridApi.core.on.sortChanged($scope, function(grid, sortColumns) {
        if (sortColumns.length > 1) {
            // We have more than one sort. Kill the first one.
            // If this works we'll only ever have 0, 1 or 2 sortColumns,
            // and only ever 2 for the lifetime of this method.
            var column = null;
            for (var j = 0; j < grid.columns.length; j++) {
                if (grid.columns[j].name === sortColumns[0].field) {
                    column = grid.columns[j];
                    break;
                }
             }
             if (column) {
                 sortColumns[1].sort.priority = 1; // have to do this otherwise the priority keeps going up.                           
                 column.unsort();
             }
         }
    });
};

This is against the 3.0.0 release of ui-grid.

HTH

like image 61
Jim Grimmett Avatar answered Nov 01 '22 10:11

Jim Grimmett