How to keep formatting

How to keep formatting

tyroneschifftyroneschiff Posts: 5Questions: 0Answers: 0
edited March 2011 in General
I update my DataTable with new data when a user selects a particular radio button. I'm finding that when I try to update the DataTable with new data after a user clicks the radio button, the DataTable loses its formatting. It is no longer searchable, I can no longer select the number of rows, I cannot sort the data, etc. I am trying to figure out a way to maintain the formatting when the DataTable is updated via AJAX.

Has anyone else encountered this before?

Replies

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    I presume that you are writing the HTML in the table directly ( $('table tbody').html( whatever ) or something like that)? Rather than doing that, use the API methods (fnClearTable, fnAddData etc) to manipulate the table, which will keep the DataTables internals up to date with your changes.

    Allan
  • tyroneschifftyroneschiff Posts: 5Questions: 0Answers: 0
    Hi Allan,
    Thanks so much for your response. I was writing the HTML directly into the table just as you illustrated. I think you're correct that using the API methods would be better, but am finding the documentation rather confusing to implement.

    Is there a way to use fnAddData with json formatted data from an external file?
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    There are a couple of options - you can just do a $.ajax call to get the information from the file and then pass it to fnAddData. Or perhaps if you are using Ajax sourced data the fnReloadAjax plug-in would be appropriate (even if not the source might get you started with loading and using fnAddData): http://datatables.net/plug-ins/api#fnReloadAjax

    Allan
This discussion has been closed.