1

This is my datatable code:

table = $('#datatable-buttons').DataTable({
          lengthChange: false,
          pageLength: 25,
          order: [ 0, 'desc' ],
          //buttons: ['copy', 'excel', 'pdf', 'colvis']
          columns: [
            { data: "sample_lab_ref" },
            { data: "sample_client_ref" },
            { data: "sample_client_ref2" },
            { data: "sample_client_ref3" },
            { data: "sample_date" },
            { data: "report_date" },
            { data: "test_parameter_name" },
            { data: "test_parameter_sop" },
            { data: "test_technique_name" },
            { data: "test_value_text" },
            { data: "test_units" }
          ],
          columnDefs: [
            {
              targets: [4, 5],
              visible: false,
              searchable: false
            }
          ],
          ajax: {
            url: "/assets/ajax/test_data_ajax_handler.php",
            type: "POST",
            data: {
              action: "getTestData",
              user_data: '<?=json_encode($userData)?>'
            }
          },
          buttons: {
              buttons: [
                  { extend: 'copy', className: 'btn-info' },
                  { extend: 'pdf', className: 'btn-danger' },
                  { 
                      extend: 'csv', 
                      className: 'btn-success', 
                      text: 'Export to CSV',
                      exportOptions: {
                          modifier: {
                              search: 'applied'
                          }
                      }
                  },
                  { extend: 'colvis', className: 'btn-primary' },
              ]
          },
          initComplete: function(settings, json) {
            table.buttons().container()
            .appendTo('#datatable-buttons_wrapper .col-md-6:eq(0)');
          }
      });

Works fine, but in the event the function in the ajax handler returns NULL (no rows found). How can I tell the datatable to display something like "No data found". Right now it gives me an invalid json syntax error.

1
  • 1
    By default DataTable would show No data available in table if there are zero records in the table, perhaps you need to handle your ajax result to return the table with 0 data rather than NULL Commented Mar 3, 2021 at 10:18

3 Answers 3

2

You can create empty json like this:

{
  "data": [],
  "total": 0,
  "recordsTotal": 0,
  "recordsFiltered": 0
}
Sign up to request clarification or add additional context in comments.

Comments

2

From here Display warning if records null Datatables AJAX - dataSrc You can fix it with dataSrc

ajax: {
        url: "/assets/ajax/test_data_ajax_handler.php",
        type: "POST",
        data: {
          action: "getTestData",
          user_data: '<?=json_encode($userData)?>'
        },
        dataSrc: function(data){
            if(data.data == null){
                return [];
            } else {
                return data.data;
            }
        }
      },

Comments

0

Try using dataSrc and pass a function to process your data.. Hope the attached data pre-processing callback will be called even on HTTP errors

sample code

$('#example').dataTable( {
  "ajax": {
    "url": "data.json",
    "dataSrc": function ( json ) {
      for ( var i=0, ien=json.data.length ; i<ien ; i++ ) {
        json.data[i][0] = '<a href="/message/'+json.data[i][0]+'>View message</a>';
      }
      return json.data;
    }
  }
} );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.