如何在数据更新后调用角度数据表分页刷新回调?

时间:2015-11-04 02:53:38

标签: javascript angularjs pagination angular-datatables

我有角度数据表配置,工作正常。我的配置看起来像

  vm.dtOptions = DTOptionsBuilder.newOptions().
            withPaginationType('full_numbers').
            //withOption('ajax', {
            //    url: 'rest/get/'+entityName,
            //    type: 'GET'
            //}).
            withOption('serverSide', true).
            withOption('ajax', function(data, callback, settings) {
                    EntityManager.get({entity:entityName,action:'get',start:data.start,length:data.length}).$promise.then(function(response) {
                    console.log('response');
                    console.log(response);
                    vm.objectList = response.data;

                    callback({
                        recordsTotal:    response.recordsTotal,
                        recordsFiltered: response.recordsFiltered,
                        data: response.data
                    });

                });
            }).
            withDataProp('data').
            withOption('processing', true).
            withOption('bFilter', false).
            withOption('bSort', false).
            withOption("aaSorting", []).
            withDisplayLength(10);

但我也有更新数据和recordsTotal的过滤功能,因此应该重新分页 - 必须修改最后一个按钮编号。但它不会发生。有没有办法打电话

 callback({
      recordsTotal:    response.recordsTotal,
      recordsFiltered: response.recordsFiltered,
      data: response.data
 });

来自控制器?什么对象和什么方法更新分页?

1 个答案:

答案 0 :(得分:2)

好的,最后我找到了解决方案。首先,在标记中添加dt-instance:

 <div ng-controller="DataTableController as listTable" ng-init="init('informsystem')">
   <table datatable="" dt-options="listTable.dtOptions" dt-instance="listTable.dtInstance" class="row-border hover">

在控制器中声明dtInstance变量并初始化。另外,获取所有ajax回调逻辑以分离函数并将其传递到dtoptions和filter:

 var vm = this;
 vm.dtInstance = {}; //MUST BE INITIALIZED! DON'T FORGET vm.(this) before varName

        var ajaxCallback = function(data, callback, settings) {
            $scope.filter.start = data.start;
            $scope.filter.length = data.length;
            console.log($scope.filter);
            EntityManager.get($scope.filter).$promise.then(function(response) {
                console.log('response');
                console.log(response);
                vm.objectList = response.data;

                callback({
                    recordsTotal:    response.recordsTotal,
                    recordsFiltered: response.recordsFiltered,
                    data: response.data
                });

            });
        };

在config中使用ajaxCallback:

   ....withOption('ajax', ajaxCallback ).....
在doFilter / doSearch中

  $scope.doFilter = function () {
            console.log(vm.dtInstance);
            vm.dtInstance.changeData(ajaxCallback);
        };

$ scope.filter在init中填充了$ resource的常见参数,并在回调中添加了start和length(offset)。此外,过滤器包含来自ng-model绑定的过滤器html输入的值。