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

160
Visualizações
How to alter datatables ajax.data request body on button click

I would like to fetch data on page load and then have filters that can be clicked to refetch the data. The query is a post request because the query parameters are complex and should be represented as json for logical grouping.

The code is as follows:

// table initialization
table = $('my_table').DataTable({
  processing: true,
  ajax: {
    url: 'api/v1/get_documents',
    type: 'POST',
    contentType: "application/json; charset=utf-8",
    data: () => {
      return JSON.stringify({
        doc_filters: {
          archived: false,
          doc_type: 'accounting'
        },
      })
    },
  },
  columns: my_columns,
});


$('#archive_button').click(() => {
  table.ajax.data = () => {
    return JSON.stringify({
      project_filters: {
        archived: true,
      }
    });
  };
  table.ajax.reload();
})
I would like the request body to reflect the new project_filters state with archived = true, refetch the data with this new request body, and update the table to reflect the new data. This does not update the request json body.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

With the help of @andrewJames's suggestion I was able to refactor my code as follows:

// table initialization
table = $('my_table').DataTable({
  processing: true,
  ajax: {
    url: 'api/v1/get_documents',
    type: 'POST',
    contentType: "application/json; charset=utf-8",
    data: () => {
      archiveStatus = $('#archive_button').val();
      documentType = $('#document_type').val();
      return JSON.stringify({
        doc_filters: {
          archived: archiveStatus,
          doc_type: documentType
        },
      })
    },
  },
  columns: my_columns,
});


$('#archive_button, #document_type').click(() => {
  table.ajax.reload();
})

about 4 years ago · Juan Pablo Isaza 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