11var app = angular . module ( 'persons' , [ 'ngGrid' , 'ui.bootstrap' ] ) ;
22
33app . controller ( 'personsList' , function ( $scope , $http ) {
4+ // Makes the REST request to get the data to populate the grid.
45 $scope . refreshGrid = function ( page ) {
56 $http ( {
67 url : 'resources/persons' ,
@@ -15,22 +16,26 @@ app.controller('personsList', function ($scope, $http) {
1516 } ) ;
1617 } ;
1718
19+ // Do something when the grid is sorted.
20+ // The grid throws the ngGridEventSorted that gets picked up here and assigns the sortInfo to the scope.
21+ // This will allow to watch the sortInfo in the scope for changed and refresh the grid.
1822 $scope . $on ( 'ngGridEventSorted' , function ( event , sortInfo ) {
1923 $scope . sortInfo = sortInfo ;
20-
2124 } ) ;
2225
26+ // Watch the sortInfo variable. If changes are detected than we need to refresh the grid.
27+ // This also works for the first page access, since we assign the initial sorting in the initialize section.
2328 $scope . $watch ( 'sortInfo' , function ( ) {
2429 $scope . refreshGrid ( $scope . persons . currentPage ) ;
2530 } , true ) ;
2631
32+ // Initialize required information: sorting, the first page to show and the grid options.
2733 $scope . sortInfo = { fields : [ 'id' ] , directions : [ 'asc' ] } ;
2834 $scope . persons = { currentPage : 1 } ;
29-
3035 $scope . gridOptions = {
3136 data : 'persons.list' ,
3237 useExternalSorting : true ,
33- sortinfo : $scope . sortInfo
38+ sortInfo : $scope . sortInfo
3439 } ;
3540} ) ;
3641
0 commit comments