Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

235
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda