H2O
H2O

Reputation: 313

Exporting Datatable data in Excel without Pagination

Currently I'm using Datatables with pagination. Now I recently added the Datatable.buttons library to use Export to Excel option. Now this functionality works fine but it only exports the first 10 rows of the page I'm currently in due to pagination. Now if I remove the limit in my model class, it will export all the entries which is what I want but it also gets rid of the pagination which decreases my site performance. The following is my current AJAX call code for getting Datatable data:

$('#datatable.table').DataTable({
  searching: true,
  paging: true,
  processing: true,
  serverSide: true,
  bFilter: true,
  dom: 'Bfrtip',
  async: false,
  'columnDefs': [ {
  'targets':[0,1,31,32,33,34,35,36,37], 
  'orderable': false, 
  }],
  buttons: [
          {
            text:"Export to Excel",
              extend: 'excelHtml5',
              exportOptions: {
                  columns: ':visible',
                  modifier:{
                    page:'all'
                  }
              }
          },           
    ],
  ajax: {
    "url": '<?php echo site_url('listings/dtlists'); ?>',
    "type": "POST"
  },
  columns: [{
      data: "title",
      className: "column5"
    },
  ...

Now I've added page:'all' in my modifier, but it still only prints the 1st pagination page. So is there any way that I can use a different model function just for the export button where I can customize the limit manually like 1000 or 2000 records?

Basically I want to redirect the buttons functionality to a different controller instead of "url": '<?php echo site_url('listings/dtlists'); ?>', controller which it is currently going to.

Upvotes: 0

Views: 3690

Answers (1)

rf1234
rf1234

Reputation: 1605

The reason for this behavior is serverSide processing. With serverSide processing only those records are loaded by the client that need to be shown. If you set pagination to 10 only 10 records get loaded - and can be exported.

If you get rid of serverSide processing everything will work because then all relevant records are downloaded to the client regardless of your pagination settings. serverSide processing is only appropriate if you have extremely large tables with very many records qualifying to be downloaded. Otherwise it has very many downsides e.g. search doesn't work if you have rendered values that don't exactly match your database content etc..

I only use server side when I absolutely cannot avoid it.

Upvotes: 1

Related Questions