4

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?

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks this should work but it would break multisort. Thanks for pointing me in the right direction as it caused me to discover a better way. I modified ng-grid's sortService there is a function called sortActual where it copies the data into tempData variable like so: var tempData = self.data.slice(0); By simply removing the splice(0) it will sort the data in place aka the original array and no further work is neccesary. Thanks !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.