Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

236
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda