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();
})
reload()function re-uses theajaxdefinition as defined in the DataTable configuration. Although you are using a function for yourdata, the actualarchivedvalue in the function is hard-coded tofalse. My approach in similar situations is to make that a variable (initially set tofalse), and then have theclick()event update the value of the variable totrue, prior to callingreload(). There may be other/better approaches, also.