Angularjs Datatable server side pagination
Asked Answered
T

3

5

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

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

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

json data before add

[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

 [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.

Tao answered 2/11, 2015 at 10:49 Comment(1)
You made error here: conole.logAndersen
S
10

Returned JSON format should be:

{
    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

Scincoid answered 18/1, 2016 at 17:49 Comment(0)
A
1

Remove .dataSrc and use this option:

.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.

Andersen answered 7/5, 2017 at 8:21 Comment(0)
M
0
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"
                  }
Myrick answered 5/4, 2019 at 5:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.