Prevent unsorted but only allow one column to be sorted at a time
I see no way to allow this functionality. The only way I see to prevent unsorted columns is 'suppressRemoveSort', but if I set that then if I select another column the sort on the original column doesn't go away. This kind of makes sense, based on "preventing" unsorting of the column, but it blocks what I'm actually trying to do. Even a workaround of manually being able to change the sort programmatically would be ok for now, as I could just call that again from the sort handler manually. Alas, from the documentation I see no way to programmatically set the sort like you can with pagination.
Basically, we always want a single column sorted at all times, in all cases, but only ever want one column at a time to have sorting. Help?
You can set the sort on the columnDef, there is also an event that is fired of sortChanged. http://ui-grid.info/docs/#/api/ui.grid.class:GridOptions.columnDef
Right, but setting in the columnDef doesn't allow skipping unsorted that I could see. I can set a default sort and set suppressRemoveSort, but then if I select a different column, the original column stays as part of the sort. I found a "workaround" after digging through the JavaScript of the grid object itself. What is giving me the desired behavior I'm looking for, is setting an initial sort on a column (i.e. sort: { direction: uiGridConstants.ASC }), then in sortChanged (as you had suggested), I have to manually do:
if (sortColumns.length == 0) { // Prevent unsorted
for (i = 0; i <= grid.columns.length; i++) {
if (grid.columns[i].name == $scope.clientModel.GridSort) {
grid.sortColumn(grid.columns[i]);
break;
}
}
}
else {
$scope.clientModel.GridSort = sortColumns[0].name;
$scope.clientModel.GridDir = sortColumns[0].sort.direction;
$scope.loadGridData();
}
What I really wanted, was only those last 3 lines in the "else". We're maintaining paging and sorting together, so I was separately maintaining the sort name and direction already, anyway. I had to do everything inside the "if" to check and prevent the unsorted from happening by manually sorting again.
Oh! And I forgot to mention - this is being done using both useExternalSorting and useExternalPaging. Maybe that was part of the confusion as to why I had to jump through all these hoops. If there's a more "baked in" solution please let me know, but even from digging through the JavaScript I saw no better way to block unsorted while maintaining single-column sorting+paging externally.
Other than this snafu, really love v3!
Yeah, if you want to force only one column to be sorted, you need to set suppressRemoveSort, and in the sortChangedEvent, you need to check if there are now 2 columns sorted, and if so remove the old sorted column. Yes, a bit convoluted, but also a quite specific use case.
We'd be happy to accept a PR that provides a "sortOneColumnOnly" option that put this same logic into the base application.
Thanks for the confirmation! Yeah, I did have my first "success" with using suppressRemoveSort and finding 2 columns to reduce to 1, but found it being a bit less code as shown above with simply not setting suppressRemoveSort and checking for 0 columns to re-sort the previously sorted column.
This is a useful request, but not really in the current roadmap. We have far too many good ideas in the repository already, giving the impression that we have a project riven with defects.
Accordingly, I am leaving the enhancement tag on this, but closing it. If someone would like this functionality, we'd welcome a pull request that adds it.
Hello, Related to this issue, could you please explain to me how can i remove the sorting from one column even if the suppressRemoveSort = true for each of my columns. I am trying to achieve the save thing but without success so far.
This is my code supporting a custom suppressMultiColumnSorting flag (default true). This is added to onRegisterApi:
if(gridOptions.suppressMultiColumnSorting !== false) {
gridApi.core.on.sortChanged($scope, (grid, sortColumns) => {
if(sortColumns.length > 1) {
// the newly sorted column is the one with the highest defined priority.
const newCol = sortColumns.reduce((col, newCol) => {
const p = col.sort && col.sort.priority;
return p >= 0 && (!newCol || newCol.sort.priority < p) ? col : newCol;
});
// reset all other columns regardless of whether they set suppressRemoveSort.
grid.columns.forEach(col => {
if(col !== newCol) col.sort = {};
});
// sort by the new column only.
grid.sortColumn(newCol, newCol.sort.direction, false);
}
});
}
And here's an alternative implementation as a decorator to avoid the need to define it in every onRegisteredApi:
angular.module(name)
.config($provider => {
$provide.decorator('uiGridDirective', $delegate => {
const {compile} = $delegate[0];
$delegate[0].compile = () => {
const compilation = compile(), {post} = compilation;
compilation.post = ($scope, $elm, $attrs, uiGridCtrl) => {
const result = post($scope, $elm, $attrs, uiGridCtrl);
const {grid} = uiGridCtrl;
if(grid.options.suppressMultiColumnSorting !== false) {
grid.api.core.on.sortChanged($scope, (grid, sortColumns) => {
if(sortColumns.length > 1) {
// the newly sorted column is the one with the highest defined priority.
const newCol = sortColumns.reduce((col, newCol) => {
const p = col.sort && col.sort.priority;
return p >= 0 && (!newCol || newCol.sort.priority < p) ? col : newCol;
});
// reset all other columns regardless of whether they set suppressRemoveSort.
grid.columns.forEach(col => {
if(col !== newCol) col.sort = {};
});
// sort by the new column only.
grid.sortColumn(newCol, newCol.sort.direction, false);
}
});
}
return result;
};
return compilation;
}
return $delegate;
});
});
I was looking for this functionality and notice that is already there. columnDefs
sortDirectionCycle: [uiGridConstants.DESC, uiGridConstants.ASC],
To prevent sorting on multiple columns, I added these two lines in the Grid.prototype.sortColumn function, ui-grid.js file.
self.resetColumnSorting(column);
column.sort.priority = undefined;
works for me..