ng grid pagination
Hi, Trying to implement pagination in ng grid Reference plunker: http://plnkr.co/edit/TGIpIY?p=preview
Getting the grid values using "$resource" and values are displayed properly in the ng-grid in the result.html without using pagination.
On using pagination the grid is not displayed.
mycontroller.js $scope.result = myservices.getResult(); //this method contains lists in it The search function returns 2 lists - list1 and list2
created grid as below $scope.mygrid = { data : "result.list2", enablePaging : true, pagingOptions : $scope.pagingOptions, showFooter : true, ..... }; As per the reference site,in the function "$scope.setPagingData(data,page,pageSize);", how to set the value for "data" ? how to get the list value here? (values not in .json file, it is from backend sources using restful services.Using .json file for "data" as in the site, it is working fine)
Please suggest how to solve this.
Not sure if I understand the problem; is this what you need to do?
$scope.setPagingData($scope.result.list2, ...)
Yes, this is what i need,but i tried this way and pagination does not work on load. only on selecting the page number from the page options it works.
the search result from the restful services returns an object. it has 2 list in it. each list is set of objects.
Example - $scope.result = myservices.getResult(); - this is PersonDetails object.
PersonDetails has 2 list - List
Code: mycontroller.js
$scope.result = myservices.getResult();
$scope.getPagedDataAsync = function(pageSize, page) { $scope.setPagingData($scope.result.personList, page, pageSize); }; $scope.setPagingData = function(data, page, pageSize) { var pagedData = data.slice((page - 1) * pageSize, page * pageSize); $scope.myData = pagedData; if (!$scope.$$phase) { $scope.$apply(); } }; $scope.$watch('pagingOptions', function() { console.log("watch changed pagingOptions"); $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage); }, true);
//person grid $scope.myGrid = { data : "myData", multiSelect : false, enableRowSelection : true, showColumnMenu : true, enablePaging : true, pagingOptions : $scope.pagingOptions, showFooter : true ........ }
the grid loads even before the result from backend comes and it shows empty grid. The pagination option watch function is called for empty grid and the values are undefined for it. So the page shows empty grid on load of the result.html only on selecting the page numbers from the page option dropdown the pagination works with values shown in the grid.
Please suggest me how to solve this. I need the grid with values on load itself. i am getting empty grid on page load.
Can someone help me with this issue?
found the issue,but still not able to solve it due to other errors.
The method "$scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);" should be called before the pagingOptions watch function.
Modified code: $scope.myData = [];
$scope.getPagedDataAsync = function(pageSize, page) { console.log("in pageddatasync method"); $scope.result = myservices.getResult(); $scope.setPagingData($scope.result.personList, page, pageSize); };
$scope.setPagingData = function(data, page, pageSize) { var pagedData = data.slice((page - 1) * pageSize, page * pageSize); $scope.myData = pagedData; if (!$scope.$$phase) { $scope.$apply(); } };
//added code $scope.getPagedDataAsync($scope.pagingOptions.pageSize,$scope.pagingOptions.currentPage);
$scope.$watch('pagingOptions', function() { console.log("watch changed pagingOptions"); $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage); }, true);
Now the method "getPagedDataAsync" is called first but still the values are not displayed and it throws error as personlist is undefined. the call to the backend takes few seconds to return the value and so the value is undefined initially and it does not display the grid.
Error:
TypeError: Cannot call method 'slice' of undefined at Object.MyController.$scope.setPagingData (http://localhost:9080/sample/js/ui/controllers/mycontroller.js:14:25) at Object.MyController.$scope.getPagedDataAsync (http://localhost:9080/sample/js/ui/controllers/mycontroller.js:30:12)
please someone help me with the issue. How to do i need to make the grid not display until the value from the backend is received , do i need to check for undefined?
Found a workaround using settimeout.
How can I hide/show paging(enablePaging) in angular ng-grid? Thanks