-->

ngGrid sort entire result set

2020-03-24 11:08发布

问题:

I would like on the ngGridSorted event to sort the entire result set (not just the page) by the sort function that was clicked on. Is there an easy way to do this? I haven't seen a good example of how to sort anything but the page that the user is currently on.

回答1:

In fact you just can use the server-side paging option without actually having serversided code.

The example on the ng-grid demos also just loads a big json file.

The function you need to rewrite is:

 $scope.getPagedDataAsync = function (pageSize, page, searchText)

Instead of loading a file you can put logic in here that has the ability to search and splice an array.

No need to do async stuff just because the function has async in its name.

A library with array manipulation functions like Lo-Dash or Underscore is of great help here.

Update:

Since I found out that the demo doesn't include sorting (Doh!) I made you this Plunker which is an extended version of the official server sided Pagination demo that also features sorting and a non server array.

The code is a bit complex. See if you understand it, or come back with questions.

The sorting Algorithm is not very optimized. In fact I took it from here (Thanks again, David!)