This is how I solved this problem. The basic idea is to manually sort the array containing all your data and paginate again.
define initial sort values
$scope.sortInfo = {fields: ['id'], directions: ['asc']};
Define a function to sort your data in a specific field
// sort over all data function sortData (field, direction) { if (!$scope.allData) return; $scope.allData.sort(function (a, b) { if (direction == "asc") { return a[field] > b[field] ? 1 : -1; } else { return a[field] > b[field] ? -1 : 1; } }) }
watch sortInfo and react to changes
// sort over all data, not only the data on current page $scope.$watch('sortInfo', function (newVal, oldVal) { sortData(newVal.fields[0], newVal.directions[0]); $scope.pagingOptions.currentPage = 1; setPagingData($scope.pagingOptions.currentPage, $scope.pagingOptions.pageSize) }, true);
where setPagingData is
// pick the slice we currently want to see function setPagingData (page, pageSize){ if (!$scope.allData) return; $scope.totalServerItems = $scope.allData.length; $scope.myData = $scope.allData.slice((page - 1) * pageSize, page * pageSize);; };
set sortInfo in your gridOptions
$scope.gridOptions = { sortInfo: $scope.sortInfo, useExternalSorting: true, }
dedan
source share