Data Tables, ajax throws error when no records to display

Data Tables, ajax throws error when no records to display

OdynOdyn Posts: 8Questions: 2Answers: 0

Hello Data Table team

I am using Data Table inside of my application and would like to add some basic error handling for them.

I am pulling JSON object using data table ajax call like that from SQL database:

// Initialize data table table with data from JSON
$(document).ready(function() {
    $('#jobs').DataTable( {
        "dom" : "<'row'<'small-6 columns dash-toolbar'><'small-6 columns'f>r>"+
                "t"+
                "<'row'<'small-6 columns'i><'small-6 columns'p>>",
        "lengthMenu": [ 15, 25, 50, 75, 100 ],
        "lengthChange": false,
        "ajax": {
          url: '/dashboarddata?jobs=get_all',
          dataSrc: ""
        },
        "columns": [
            { "data": "ID" },
            { "data": "InvoiceNumber" },
            { "data": "PagesCount" },
            { "data": "Status" }
        ],
          // Set rows IDs to be same like from SQL database
          rowId: function(a) {
            return a.ID;
          }
    } );
} );

When there is no records to return then my Data Table return ajax error and still loading...

Is there any way to display something different when there is no records to display?

Thanks

Answers

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    Did you follow the troubleshooting steps at the link provided?
    https://datatables.net/manual/tech-notes/7

    The place to start is to determine what the server response is.

    Datatables expects an array of rows. For an empty data set the best thing to return is an empty array, ie, [].

    Kevin

  • OdynOdyn Posts: 8Questions: 2Answers: 0

    Yes I been there but found nothing useful.

    I decided to return empty JSON object in case where SQL SELECT query will not return any record set.

    So now I have empty JSON object and Data Table render without ajax object

    Just thinking if there is any option if Data Table records are empty, then display message inside like "No records to display"

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    Just thinking if there is any option if Data Table records are empty, then display message inside like "No records to display"

    That happens by default, for example:
    http://live.datatables.net/hefopofi/1/edit

    So now I have empty JSON object and Data Table render without ajax object

    Does this mean that everything is working now?

    Kevin

  • OdynOdyn Posts: 8Questions: 2Answers: 0

    Not sure, I dont think so, thats why asking

    In my case there is no default message

    IS there any option to add?

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    Not sure, I dont think so, thats why asking

    What exactly is returned from the server? Look at the browser's network inspector tool.

    Are you still getting the Ajax error?

    Do you get errors in your browser's console?

    If you don't something like the example I linked ot then there is an error somewhere. The language.emptyTable can be used to change the message but that wont't help if there is an issue.

    Can you provide a link to your page so we can take a look? If not can you post a scrreenshot of the table?

    Kevin

This discussion has been closed.