Ajax call, data + server side data
Ajax call, data + server side data
Hello DataTables community.
I'm stuck with some issue here. Probably an easy one to fix, but I can't figure it out.
I have a datatables, with some columns that I can sort. I also have the search capability and pagination. On top of that, I have a form that enables more complex filtering.
Now whenever the user change the filters in the form, or search, change sorting or the pagination, I want to send all of that information to the server that will process these parameters.
Here is my code:
`
$(document).ready( function () {
var table=$("#tbl").DataTable({
"order": [[ 5, "desc" ]],
"processing": true,
"bProcessing": true,
"serverSide": true,
"ajax":{"type":"GET","url":"/index.php",
"data":function ( d ) {return $('#form_opts').serialize();}
},
"fixedHeader": true,
"columns": [
{ "name":"structure","orderable": false},
{ "orderable": true},
{ "orderable": true},
{"orderable": true},
{ "orderable": false },
{ "orderable": true},
{ "orderable": true},
],
});
} );`
My issue is that when I remove the "data" line, I got all the server side parameters as such:
draw 1
columns[0][data] 0
columns[0][name] structure
columns[0][searchable] true
columns[0][orderable] false
columns[0][search][value]
columns[0][search][regex] false
columns[1][data] 1
columns[1][name] 377677
columns[1][searchable] true
columns[1][orderable] true
columns[1][search][value]
columns[1][search][regex] false
columns[2][data] 2
columns[2][name]
columns[2][searchable] true
columns[2][orderable] true
columns[2][search][value]
columns[2][search][regex] false
columns[3][data] 3
columns[3][name]
columns[3][searchable] true
columns[3][orderable] true
columns[3][search][value]
columns[3][search][regex] false
columns[4][data] 4
columns[4][name]
columns[4][searchable] true
columns[4][orderable] false
columns[4][search][value]
columns[4][search][regex] false
columns[5][data] 5
columns[5][name]
columns[5][searchable] true
columns[5][orderable] true
columns[5][search][value]
columns[5][search][regex] false
columns[6][data] 6
columns[6][name]
columns[6][searchable] true
columns[6][orderable] true
columns[6][search][value]
columns[6][search][regex] false
order[0][column] 5
order[0][dir] desc
start 0
length 10
search[value]
search[regex] false
But when I add the data line, I only get this:
opt1 on
opt2 on
opt3 on
_ 1550083659641
Now I would like to have both.
Any ideas?
Thanks for your help.
This question has an accepted answers - jump to answer
Answers
You don't want to return anything in your data function. Take a look at the second example here:
https://datatables.net/reference/option/ajax.data#Examples
Kevin
Problem solved. I know it was something silly.
This is the correct data line:
"data":function ( d ) { d.form_data=$('#form_opts').serialize();}
Thanks Kevin