Control ajax pagination using query string

Control ajax pagination using query string

robin-nrobin-n Posts: 2Questions: 1Answers: 0
edited December 2016 in Free community support

I'm fetching a JSON file where I need to be able to control pagination using query strings.

The output looks like this.

{  
   "PageIndex":1,
   "PageSize":50,
   "TotalFoundReleases":433,
   "Releases":[  
      "item",
      "item",
      "item"
   ]
}

So, the URL looks something like this http://example.com/NewsFeed/A275CXXXXX where I get page 1. Now, to get page 2 I append an ?pageIndex=2

How can I implement this in Datatables?

$('#example').dataTable({
      "ajax": {
        "url": "http://example.com/NewsFeed/A275CXXXXX`",
        "dataSrc": "Releases",
      },
      "columns": [
        {"data": "Title"},
        {"data": "Intro"},
      ]
    });
  });

Fiddle here: https://jsfiddle.net/503yrwr8/

Answers

  • robin-nrobin-n Posts: 2Questions: 1Answers: 0

    Solved!

    https://jsfiddle.net/503yrwr8/5/

        var table = $('#cision-datatable').DataTable({
          ordering: false,
          info: true,
          bFilter: false,
          processing: true,
          serverSide: true,
          ajax: function(data, callback) {
            // make a regular ajax request using data.start and data.length
            $.get('https://publish.ne.cision.com/papi/NewsFeed/A275C0BF733048FFAE9126ACA64DD08F', {
              pageSize: data.length,
              format: 'json',
              pageIndex: (data.length + data.start) / data.length,
            }, function(res) {
              callback({
                recordsTotal: res.TotalFoundReleases,
                recordsFiltered: res.TotalFoundReleases,
                data: res.Releases,
              });
            });
          },
          "columns": [
            {"data": "Title"},
            {"data": "PublishDate", "type": "date"},
          ],
          "columnDefs": [
            {
              "render": function (data) {
                var date = new Date(data);
    
                return date.getFullYear() + '-' +
                    ('0' + (date.getMonth()+1)).slice(-2) + '-' +
                    ('0' + date.getDate()).slice(-2) + ' ' +
                    ('0' + date.getHours()).slice(-2) + ':' +
                    ('0' + date.getMinutes()).slice(-2);
              },
              "targets": 1
            },
            {"visible": true, "targets": [1]}
          ]
        });
    

    Searching and filtering wont work though.

This discussion has been closed.