Reload on change function
Reload on change function
I've been struggling with a problem for a number of days now. Read all the similar posts and can't seem to get this to work.
On change of form, DataTable is reloaded
I checked the console and it appears all good. Data is returned properly
However I get an error: DataTables warning: table id=example - Requested unknown parameter 'id' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4
I checked the link, and made some changes, to no avail. Could someone please point me in the right direction?
$(document).ready(function(){
$('#142_get').change(function () {
var typefilter = $("#typefilter").val();
var datefilter_enddate = $("#datefilter_enddate").val();
var datefilter_startdate = $("#datefilter_startdate").val();
$.ajax({
type: 'POST',
url: '142_get.php',
data: "typefilter="+typefilter+"&datefilter_enddate="+datefilter_enddate+"&datefilter_startdate="+datefilter_startdate,
success: function (data) {
$('#example').DataTable({
destroy: true,
data: data,
columns: [
{ data: "id" },
{ data: "eui" },
{ data: "callerid" },
{ data: "date_called" },
{ data: "type" },
{ data: "billing_item_id" },
{ data: "action" },
],
});
}
})
});
});
This question has an accepted answers - jump to answer
Answers
console log shows data returned as follows:
Your JSON looks like this:
You are loading the Datatable data with
data: data,
. Looks ike you should be usingdata: aaData,
since there is not adata
object.Kevin
I tried that, seems to make no difference. I tried changing that, but same result.
Sorry, use
data: data.aaData,
to point to the row data.Kevin
So now the error is gone. However the table does not update with data.
Just as an update, here is what I have now
Use the browser's network inspector to verify the JSON response is what you are expecting when the change event executes.
If the first data set and the second contain some of the same data its possible, due to Datatables sorting, the first page shows the same rows.
If you still need help then please post a link to your page or test case replicating the issue so we can help debug.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
"If the first data set and the second contain some of the same data its possible, due to Datatables sorting, the first page shows the same rows."
That is exactly what is happening .
If you aren't expecting the two datasets to have some of the same data then you will need to debug your server script queries.
Kevin
I think there is something wrong with this part:
Reason I think that is onchange, the table does properly update result quantities, pagination is updated etc. Just the table fields are simply empty.
Its hard to say without seeing the problem. Using the browser's network inspector compare the differences between the JSON responses. Do you see anything different? You can post the working and non-working responses here.
If your
columns.data
definition is wrong then you would get errors, something likeDataTables warning: table id={id} - Requested unknown parameter...
.I built this example loosely based on what you are doing:
https://live.datatables.net/havavemu/1/edit
The environment doesn't have code to supply ajax data parameters as filters so the fetched data is the same each time. There is a flag that splits the data and alternates between the first half and second half. Its simply to show the technique you are using works.
Kevin
How could I have missed this? I knew something was missing. All I needed to add was
Thank you very much! Grateful for your help.
Seems I might have a new issue. So when the table reloads, which now works perfectly, only one page with 10 results are displayed. Pagination only shows 1 page. Results only say 10. However I know there should be hundreds if not thousands.
The server side processing page returns the results as follows:
What could be wrong?
here is the console log
Further update on this.
If I change this to
and I remove the following line completely
I get pagination info, correct qty etc properly. But no actual data. Table is empty.
If I do
and add back the
Then table populates first page, but pagination is broken. Meaning only 1st page with 10 results.
Your above code is not using server side processing however the response does provide the server side response. You are using jQuery ajax() to fetch the data. If you want server side processing then you will need to use
ajax
andserverSide
.This example shows how to send
ajax.data
parameters using server side processing.Your server script is returning legacy server side processing parameters:
You may need to use the current parameter names for DT 2.0 as described in the Server Side Processing protocol. For example:
Kevin