ui-grid icon indicating copy to clipboard operation
ui-grid copied to clipboard

ngGridEventSorted event fires many times

Open animator013 opened this issue 13 years ago • 4 comments

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 avatar Sep 12 '13 08:09 animator013

@animator013 I'm also having this problem, any solution?

AlJohri avatar Nov 10 '13 07:11 AlJohri

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);

shazzababs avatar Dec 04 '13 14:12 shazzababs

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); }/

hemanshu-shah avatar Dec 25 '13 01:12 hemanshu-shah

Watching sortInfo works much better than listening to the event when using external sorting. Thanks @shazzababs.

tamlyn avatar Mar 10 '14 17:03 tamlyn