Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

234
Views
Datatables.js selected rows not staying selected after ajax.reload()

according to what it says on the tin, Datatables should be able to "retain row selection over a data reload". But I can't get it to work when using data from ajax.

Here's how I make my table:

  oTable = $('#fileList').DataTable({
    select: true,
    "ajax": {
      "url": "./data.php",
      "type": "GET"
    },
    rowId: 'Index',
    "createdRow": function (row, data, dataIndex) {
      $(row).addClass(data[2]); //set formatting from data[2]
    }
  });

Following the instructions on the reference above I initially used this to refresh the table every five seconds:

setInterval(function () {
    oTable.ajax.reload();
}

But any selected row would unselect every five seconds. So I tried being more explicit: loading the selected rows into an array and re-selecting them inside the setInterval function:

setInterval(function () {
    var selectedRows = [];
    //put all the selected rows into an array:
    sel = oTable.rows('.selected').every(function (rowIdx) {
      selectedRows.push(rowIdx);
    });
    oTable.ajax.reload();
    console.log(selectedRows);
    //select all the rows from the array
    for (var i = 0; i < selectedRows.length; i++) {
      console.log("selecting ",selectedRows[i]);
      oTable.rows(selectedRows[i]).select();
    }
  }, 5000)
});

in the Console if I have a selected row (in this example the third row) I see:

Array [ 2 ]
selecting  2    

Which is as expected, but the rows aren't re-selected. If I type oTable.rows(2).select(); into the console it selects the third row as it should, but it doesn't work from the setInterval block.

I'm guessing that it may have something to do with the rowID property. I've defined the table in HTML like this:

      <table id="fileList" class="display">
        <thead>
          <tr>
            <th>Index</th>
            <th>Status</th>
            <th>Owner</th>
          </tr>
        </thead>
      </table>

and the data comes from a php script which returns an array like

{"data":[["1", "foo", "bar"], ["2", "fuz", "baz"]}

where the first item is the index.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

It seems that the issue may be occurring due to the lack of keys in the returned data. Have you tried returning the data as an associative array similar to:

"data": [
    {
        "index": 1,
        "status": "Foo",
        "owner": "Bar"
    }
]

You can then try replacing:

rowId: 'Index',

with:

rowId: 'index',
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!