Loading JSON via AJAX with NgTable parameters

后端 未结 4 1904
情话喂你
情话喂你 2021-01-11 23:06

I\'m trying to use ngTables to sort and filter data using an AJAX call. Currently I am able to replicate the data with an ng-repeat, but none of my sorting functions apply.

4条回答
  •  长发绾君心
    2021-01-11 23:11

    $defer need to be resolved within the getData, after the ajax call is completed. As in the example You provided, the ajax call is inside the getData:

    var app = angular.module('app', ['ngTable']);
    
    app.controller('myController', function($scope, $http, $filter, ngTableParams) {
    
    $scope.tableParams = new ngTableParams({
        page: 1,            // show first page
        count: 10,          // count per page
        sorting: {
            foo: 'asc'     // initial sorting
        }
    }, {
        total: data.length, // length of data
        getData: function($defer, params) {
            $http.get('http://jsondata.com/myjson.json')
               .success(function(data, status) {
    
                   // use build-in angular filter
                   var orderedData = params.sorting() ?
                       $filter('orderBy')(data, params.orderBy()) :
                       data;
    
                   $defer.resolve(orderedData.slice((params.page() - 1) * params.count(),  params.page() * params.count()));
                });
            }
        });
    });
    

提交回复
热议问题