如何通过成功发布重新加载ngTable

时间:2014-11-21 15:33:16

标签: angularjs ngtable

我在这个主题上发现了类似的帖子,但它们包括ngTable分页。我已禁用该功能,并且在成功发布后我无法刷新表。 tableParams.reload没有做任何事情。我也不明白为什么数据没有在其上更新,是不是Angular应该在模型改变时更新视图?

// Table
$scope.tableParams = new ngTableParams({
    page: 1,            // show first page
    count: 100,          // count per page
    sorting: {
        foo: 'asc'     // initial sorting
    }
}, {
    counts: [], // length of data
    getData: function ($defer, params) {
        $http.get('/api/apiMasterItem/')
           .success(function (data, status) {
               var orderedData = params.sorting() ? $filter('orderBy')(data, params.orderBy()) : data;
               $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));

           });
    }
});
$scope.editId = -1;

$scope.setEditId = function (pid) {
    $scope.editId = pid;
}
//Post MasterItem
$scope.addMasterItem = function () {
    var newMasterItem = $scope.newMasterItem
    MasterItemPost.post(newMasterItem)
        .success(function () {
            notification.success();
        }).error(function () {
            notification.error();
        })
    console.log(newMasterItem);
    $scope.tableParams.reload();
    $scope.newMasterItem.MLItemCode = '',
    $scope.newMasterItem.MLItemDescription = '',
    $scope.newMasterItem.MLItemUOM = '',
    $scope.newMasterItem.MLItemPrice = ''
};

1 个答案:

答案 0 :(得分:2)

您也应该在收到数据后更新total属性,无论是否使用分页

控制器

...
getData: function ($defer, params) {
        $http.get('/api/apiMasterItem/')
           .success(function (data, status) {
               var orderedData = params.sorting() ? $filter('orderBy')(data, params.orderBy()) : data;
               // update table params
               params.total(data.length);
$defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));                    
           });