Issue when using HTML DOM to load / reload tables

Issue when using HTML DOM to load / reload tables

stocknrockstocknrock Posts: 2Questions: 1Answers: 0
edited September 2015 in Free community support

We are currently experiencing getting DataTables to work when we use HTML DOM to load/reload our tables.

Here is the sequence how we use it.

  1. On page load, we get data from a web service. We build an HTML table out of it and initialize the DataTable harnessing its data load from DOM feature.

  2. Later on a user event we retrieve new data from web service, again we build TBODY part and initialize the DataTable again on same table.

  3. Now, the DataTable is able to show the new data. However, number of records and pager are not updated accordingly. When we click on column to sort or search or click on pager, it starts to show the data we used first time.

  4. Problem is demonstrated here http://live.datatables.net/ditovopi/2/edit?html,js,output

Request your support on this whether we are missing a step or not correctly using the feature.

This question has an accepted answers - jump to answer

Answers

  • tangerinetangerine Posts: 3,350Questions: 37Answers: 394

    Your initial table is assigned to a variable.

      var table = $('#example').DataTable();
    

    Should your subsequent reference to $('#example').DataTable(); be re-using the same variable?

  • AliBahAliBah Posts: 11Questions: 0Answers: 0

    .DataTable() create JS object and also change HTML. You cannot simple add new html rows and call this method again.

    At least you need to clean htm, create new, delete datable JS object and recreate it.

    But i believe you need to change your solution and update your table data by some other way. Use server side processing, refresh data by timer or smth else.

  • allanallan Posts: 61,950Questions: 1Answers: 10,158 Site admin
    Answer ✓

    Yes, you need to use the API to change the data that the table shows and DataTables does not monitor the DOM. For example you would need to use rows().remove() to delete rows and rows.add() to add new rows.

    Allan

  • stocknrockstocknrock Posts: 2Questions: 1Answers: 0

    Thank you @allan. Your answer helped.

This discussion has been closed.