如何使用$ scope.tableParams.reload()重新加载ngtable?

时间:2014-10-18 05:51:54

标签: angularjs ngtable

我在角度js中使用ng-table。我的问题是删除我的表中的一条记录数据没有重新加载。这是我的代码

 myapp.controller('aamListAssignment', ['$scope', '$http', 'ngTableParams', function ($scope, $http, ngTableParams) {

        $scope.data = [];
        $scope.listPromise = null;
        $scope.$watch("data", function () {
            $scope.tableParams.reload();
        });    

        $scope.list = function () {
            $scope.listPromise = $http.get('./ccs/assignment/all').success(function (data) {
                $scope.data = data;
                $scope.tableParams = new ngTableParams({
                    page: 1, // show first page
                    count: 2           // count per page
                }, {
                    counts: [],
                    total: $scope.data.length, // length of data
                    getData: function ($defer, params) {
                        $defer.resolve($scope.data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
                    }
                });
            });
        };
        $scope.removeAssignment = function (id) {
            $scope.value = true;
            $scope.Url = "./ccs/assignment/removeAssignment/" + id;
            $http.get($scope.Url).success(function (data) {
                console.log('after delete: ' + $scope.id);
               // $location.path('aam/listAssignment/');
                $scope.list();
            });
        };

        $scope.list();
    }]);

执行时间

的控制台页面中发生了两种类型的错误
"Error: $scope.tableParams is undefined..............

"Error: h.$scope is null..........

请帮我解决这个问题。

我正在使用另一种方式,这次重装是有效的,但是分页不起作用。代码是

myapp.controller('aamListAssignment', ['$scope', '$http', 'ngTableParams', function ($scope, $http, ngTableParams) {

    $scope.tableParams = {};
    $scope.data = [];
    $scope.listPromise = null;
    $scope.list = function () {
        $scope.listPromise = $http.get('./ccs/assignment/all').success(function (data) {
            $scope.data = data;
            $scope.tableParams.reload();
        });

    };
    $scope.tableParams = new ngTableParams({
        page: 1, // show first page
        count: 6           // count per page
    }, {
        counts: [],
        total: $scope.data.length, // length of data
        getData: function ($defer, params) {
            $defer.resolve($scope.data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
        }
    });

    $scope.removeAssignment = function (id) {
        $scope.value = true;
        $scope.Url = "./ccs/assignment/removeAssignment/" + id;
        $http.get($scope.Url).success(function (data) {
            console.log('after delete: ' + $scope.id);
            $scope.list();
        });
    };
    $scope.list();

}]);

1 个答案:

答案 0 :(得分:4)

如果您只是请求数据,然后只是对数组进行分页,则没有问题。 但是,如果您需要对每个请求进行分页,请查看here,并且不要忘记 迭代$data

<tr ng-repeat="user in $data">

修改

在您的插件中,您应该添加total来电,类似于以下代码:

    total: data.length, // length of data
    getData: function($defer, params) {
    //needed for pagination!!
    params.total(data.length);
    $defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count()));

编辑2

你的工作plunk 上面有编辑