これが私がこの問題を解決した方法です。基本的な考え方は、すべてのデータを含む配列を手動でソートし、ページネーションを再度行うことです。
並べ替えの初期値を定義する
$scope.sortInfo = {fields: ['id'], directions: ['asc']};
特定のフィールドでデータをソートする関数を定義する
// 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;
}
})
}
を監視し、sortInfo
変化に対応する
// 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);
どこsetPagingData
ですか
// 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);;
};
gridOptions で sortInfo を設定します
$scope.gridOptions = {
sortInfo: $scope.sortInfo,
useExternalSorting: true,
}