-->

Pagination not working after loading JSON data on

2019-05-07 12:44发布

问题:

I followed the example on http://bazalt-cms.com/ for the pagination but using $http.get instead of hard-coding a variable in the controller.

  $http.get("data.json").success(function(data){
       $scope.dataset = data;
   }

I also moved the $scope.tableParams inside the $http.get

$http.get("data.json").success(function(data){
     $scope.dataset = data;

     $scope.tableParams = new ngTableParams({
      ...
    }
});

and changed the data variable to $scope.dataset

total: $scope.dataset.length, // length of data
getData: function($defer, params) {
        $defer.resolve($scope.dataset.slice((params.page() - 1) * params.count(), params.page() * params.count()));}

Everything works fine besides the pagination is now not working See Plnkr here

回答1:

I've been working with ng-table for a while. When I tried using variable names other than $data or data, it doesn't seem to work. So I suggest you should stick to using $data or data instead of any other variable names like dataset.

Here's a working Plunker.

UPDATE :

Looks like I missed to see what was really wrong in the OP's question. As Tyler Collier and yunus kala mentioned in the comments, you just have to use $data like <tr ng-repeat="user in $data"> instead of <tr ng-repeat="user in dataset">. And it doesn't matter if you use data or dataset in your controller code.



回答2:

have a look here ng-Table not rendering new data when reloading request
first should apply directive ng-repeat for $data variable,and move ngtableParams initialization outside success callback, and put inside getData all logic for data acsess