javascript Angularjs Datatable 服务器端分页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33475840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:31:36  来源:igfitidea点击:

Angularjs Datatable server side pagination

javascriptjqueryangularjsdatatablesangular-datatables

提问by Ali-Alrabi

I'm trying to make Angularjs Datatable server side pagination in this link https://l-lin.github.io/angular-datatables/#/serverSideProcessing

我正在尝试在此链接https://l-lin.github.io/angular-datatables/#/serverSideProcessing 中进行 Angularjs Datatable 服务器端分页

So I use this code

所以我用这个代码

    $scope.dtOptions = DTOptionsBuilder.newOptions()
       .withOption('ajax', {
                  dataSrc: function(json) {
                    conole.log(json)
                    json['recordsTotal'] =json.length
                    json['recordsFiltered'] = json.length
                    json['draw']=1
                    conole.log(json)
                    return json;
                  },
              url: 'api/footestrecords',
              type: 'GET'
           })
       .withOption('processing', true)
       .withOption('serverSide', true)
       .withPaginationType('full_numbers');

I added recordsTotal, recordsFiltered and row manually in dataSrc parameter

我在dataSrc参数中手动添加了recordsTotal、recordsFiltered和row

and this is json data before and after add recordsTotal, recordsFiltered and row

这是添加recordsTotal,recordsFiltered和row之前和之后的json数据

json data before add

添加前的json数据

[Object, Object, Object, Object, Object, Object, Object, Object,
Object,Object, Object, Object, Object, Object, Object, Object, Object,
Object, Object, Object, Object, Object, Object, Object, Object, Object,
Object, Object]

json data after add

添加后的json数据

 [Object, Object, Object, Object, Object, Object, Object, Object,
  Object, Object, Object, Object, Object, Object, Object, Object, Object,
  Object, Object, Object, Object, Object, Object, Object, Object, 
  Object,Object, Object, recordsTotal: 28, recordsFiltered: 28, draw: 1]

the probelm is pagination don't work ,data table shows all data in one page,and when I click on paging button did no action.

问题是分页不起作用,数据表显示一页中的所有数据,当我单击分页按钮时没有任何操作。

回答by Sourav Mondal

Returned JSON format should be:

返回的 JSON 格式应为:

{
    data: [{Object},{Object},{Object},{Object}…]
    draw: "1"
    recordsFiltered: 91
    recordsTotal: 91
}

You can get a complete tutorial from here about Datatables server-side paging, sorting and filtering in angularjs

您可以从此处获得有关angularjs 中的 Datatables 服务器端分页、排序和过滤的完整教程

回答by zygimantus

Remove .dataSrcand use this option:

删除.dataSrc并使用此选项:

.withDataProp(function(json) {
  console.log(json);
  json.recordsTotal = json.response.total;
  json.recordsFiltered = json.response.total;
  json.draw = 1;
  return json.response.data;
})

Change json.response according your object.

根据您的对象更改 json.response。

回答by sam ruben

The return data must be list of object ["data"]=[{name:john,id:1},{name:jason,id:2}].

Try
  .withOption('ajax', {
                  dataSrc: function(data) {                 
                   return data.data;
                     }
                  })
Else,
       .withOption('ajax', {
                  "dataSrc": "data"
                  }