How to set up and configure sorting in a datatable

How to set up and configure sorting in a datatable

justin1291justin1291 Posts: 1Questions: 1Answers: 0

Whenever I click any of the header rows in my datatable it removes all the data and the message "No data available in table".
See attached images:

Before click:

After click:

I've attached my PHP code for the page in question, I'd appreciate any help with my code.

Thank you.

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    There's going to be an event listener that's removing the data, it'll be outside of DataTables control. We're happy to take a look if you can link to a test that reproduce the issue. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Colin

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Just looked over your PHP.

    The HTML for the table is empty, and DataTables is initialised without telling it what the data will be. Instead, you are writing the data directly into the HTML around line 281-295:

    $('#customer-results tbody').append('<tr>' +
    

    That won't work since DataTables doesn't know anything about the data in the table. Hence, why when it redraws for the sort, it shows no data in the table.

    See this FAQ which also explains that.

    In short, use the API to populate the table. In this case you could use rows.add() and just do:

    table.clear().rows.add(data.items).draw();
    

    Making sure you set the columns.data option for each column correctly for the data point you want to show. Also use columns.render to create and display the link.

    Allan

Sign In or Register to comment.