How can I do server-side pagination with ng-table?

Viewed 3308

My code is

$scope.loadQuestions = function() {
  $scope.questionCount = 0;
  $scope.questionTable = new NgTableParams({
    count: []
  }, {
    total: 19387,
    getData: function($defer, params) {
      $scope.filter.sort = params.orderBy();
      $scope.filter.page = params.page();
      return $http.get("/api/questions", {
        params: $scope.filter
      }).then(function(response) {
        $scope.questionCount = response.data.count;
        return response.data.questions;
      });
    }
  });
};

If I do this, it's fine. But that's because I hardcoded the total, which doesn't make sense obviously. If I do

  return $http.get("/api/questions", {
    params: $scope.filter
  }).then(function(response) {
    params.total(response.data.count);
    $scope.questionCount = response.data.count;
    return response.data.questions;
  });

then it ng-table fires the http request twice for some reason. So what's the right way to do it?

3 Answers

For this you need to create function , inside that function calling the api.

For second call you need to changes ngtableparam name means like if earlier was tableparam now you need to create tableparam1 or something.

please see the below code

on ng click

    $scope.NextPrev=function(param){
        //code here
        //call another function  
        $scope.tablerefresh($scope.Inedx)
    }
    
     
    $scope.tablerefresh=function(param){
        $scope['tableparater1']={
           reload: function () {},
           settings: function () {return {} }
         }
         $timeout($scope.getdatafromApi(number) ,100); //call those function that gets the data from api
         $scope.tableparater1.reload();
    };
    $scope.getdatafromApi=function(number){
        $.ajax({
           //ajax code here
        }).then (function (response)) {
            data=response;
            $scope.tablaparm.reload(); // reloadfirst call data
        }
       //assigning the data into $scope.tableparater1
       $scope.tableparater1=new ngTableParams({})
    } 
Related