parliament
parliament

Reputation: 22904

ng-grid Access sorted array data

I need to access the array of sorted data after sorting a table since the original is unchanged.

There is an gridOptions.ngGrid.filteredData but no gridOptions.ngGrid.sortedData

How can I access the sorted array?

Upvotes: 4

Views: 870

Answers (1)

Kabb5
Kabb5

Reputation: 3870

Rather than just changing the appearance of the data in the grid, you could resort the data itself…if you use Underscore.js, you can add a function and modify the headerCellTemplate, as shown below.

Modified headerCellTemplate:

<div class="ngHeaderSortColumn {{col.headerClass}}" ng-style="{'cursor': col.cursor}" ng-class="{ 'ngSorted': !noSortVisible }">
  <div ng-click="mySort(col.displayName)" ng-class="'colt' + col.index" class="ngHeaderText">{{col.displayName}}</div>
  <div class="ngSortButtonDown" ng-show="col.showSortButtonDown()"></div>
  <div class="ngSortButtonUp" ng-show="col.showSortButtonUp()"></div>
  <div class="ngSortPriority">{{col.sortPriority}}</div>
  <div ng-class="{ ngPinnedIcon: col.pinned, ngUnPinnedIcon: !col.pinned }" ng-click="togglePin(col)" ng-show="col.pinnable"></div>
</div>
<div ng-show="col.resizable" class="ngHeaderGrip" ng-click="col.gripClick($event)" ng-mousedown="col.gripOnMouseDown($event)"></div>

New function:

$scope.mySort = function (field) {
  $scope.gridOptions.data = _.sortBy($scope.gridOptions.data, function (item) {return item.field});
};

The only change I made to the default template is on the second line where I changed the value of ng-click from col.sort($event) to mySort(col.displayName)

Upvotes: 1

Related Questions