ngGridEventSorted event fires many times
I would like to use serverside sorting with "useExternalSorting: true". After that i have to listen to a sortinfo change with ngGridEventSorted.
$scope.$on('ngGridEventSorted', function (sortInfo) {
console.log("fields: "+$scope.sortInfo.fields);
console.log("directions: "+$scope.sortInfo.directions);
});
I want to make ajax call inside the event but it is fired 3 times every time i click on the column i want to sort.
Is it good practice to use this event for external sorting or is there a better way ? Is this considered as a bug ?
Also this is a problem only in version 2.0.7. In 2.0.6 event is fired once.
@animator013 I'm also having this problem, any solution?
I found that I needed to update the sort info in ngGridEventSorted and then watch it to call getPagedDataAsync.
This meant that getPagedDataAsync was only called once.
$scope.$on('ngGridEventSorted', function(event, sortInfo)
{
$scope.sortInfo = sortInfo;
// don't call getPagedDataAsync here cos this function
// is called multiple times for the same update.
});
$scope.$watch('sortInfo', function ()
{
$scope.getPagedDataAsync();
}, true);
When data is set on ngGrid it calls sorting functions and it emits 'ngGridEventSorted', if we are using external data which is paged for each page we have to load data and this creates infinite loop as when we sort it gets new data and it again fires the same event.
I commented this code in angular-debug.js on line 3122 and it is working fine, I get only one event and everything works fine. I hope this helps.
/if (grid.config.sortInfo.fields.length > 0) { grid.sortColumnsInit(); $scope.$emit('ngGridEventSorted', grid.config.sortInfo); }/
Watching sortInfo works much better than listening to the event when using external sorting. Thanks @shazzababs.