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

154
Views
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 answers
Answer question

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 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!