Spinner not hiding and table not updating on first page of new results when using ajax pipeline
Spinner not hiding and table not updating on first page of new results when using ajax pipeline
Link to test case: https://datatables.net/examples/server_side/pipeline.html
Description of problem: Hi, I'm using the ajax pipeline example shown at https://datatables.net/examples/server_side/pipeline.html and I've encountered an issue whereby I can page through the results, but the first time I hit a new page of results (ie. each 5 pages, by default) the spinner comes up and the ajax request is made and the data loaded, but the data table is not updated and the spinner stays there. Paging to the next page refreshes the table and hides the spinner. Has anybody seen this issue before?
The dataset has ~80,000 records, and I'm using all the default Datatables examples - so 50 records per page, and doing an XHTML request for another 5 pages of json data each 5 pages (which is working - it's just not updating the table, and it's leaving the spinner showing).
Here is my table html:
<table id="dataTelemetry" class="table table-striped table-bordered dt-responsive nowrap">
<thead>
<tr>
<th class="p-3">Col 1</th>
<th class="p-3">Col 2</th>
<th class="p-3">Col 3</th>
<th class="p-3">Col 4</th>
<th class="p-3">Col 5</th>
<th class="p-3">Col 6</th>
<th class="p-3">Col 7</th>
<th class="p-3">Col 8</th>
<th class="p-3">Col 9</th>
<th class="p-3">Col 10</th>
</tr>
</thead>
</table>
Here is my table javascript:
<script>
$(document).ready(function () {
let objTable = $('#dataTelemetry').DataTable({
processing: true,
serverSide: true,
ajax: $.fn.dataTable.pipeline({
url: 'ajax',
pages: 5, // number of pages to cache
}),
});
});
</script>
Here is an example of the initial JSON XHTML request, which runs successfully at page load/table init and gives me 5 pages with a brief showing of the spinner (ie. the expected behaviour):
{
"draw": 108,
"recordsTotal": 86300,
"recordsFiltered": 86300,
"data": [
["10000", "OFF", "OFF", "S3", "None", "None", "2134.533936", "2821.500000", "2.750000", "<a href=\"https://maps.google.com\" target=\"_blank\"></a>"],
<snip - removed 49 records formatted as above, but with different data>
]
}
Here is an example of the JSON which is returned when reaching page 6, which causes the spinner to stay visible and the table not to update (ie. the unexpected behaviour):
{
"draw": 109,
"recordsTotal": 86300,
"recordsFiltered": 86300,
"data": [
["510000", "OFF", "OFF", "S3", "None", "None", "2207.336670", "2896.500000", "140.250000", "<a href=\"\" target=\"_blank\"></a>"],
<snip - removed 49 records formatted as above, but with different data>
]
}
When the issue occurs and the spinner shows up, all I need to do is to navigate to the next page and it refreshes the table with the new data (but the next page's data obviously) and hides the spinner.
Apologies if the format of this question is wrong. Any insight would be much appreciated!
Replies
There's nothing obvious there in the code or the responses, it looks like it's behaving as it should. Are you using the most recent DataTables sources?
As it works in the example you referenced, it must be something in your implementation. Are you able to link to your page, please, so we can debug it?
Colin
Thank you @colin for your fast response, and sorry for my slow reply.
This is the first time I have attempted to use datatables in a python project, django at that, and I have had to spend some time extracting the minimum viable code.
I'm developing on a local PC and I can't easily get it up and hosted somewhere sorry.
Here is the full code for my minimum viable test:
Source of initial page (url: /table_test), rendering as expected:
Python source of the ajax call (url: /ajax_data_test):
The URL which I am receiving for each of the ajax_data_test requests looks like:
If I break the above into new lines by the '&' character I get:
URL decoded version of the above:
Apologies that I don't have a publicly-accessible webserver that can run this at the moment; if it is too much of a problem I can probably organise something temporarily.
Any help is appreciated. I don't understand why it is not loading the results when I hit those multiples of 5 (or click on a page > 5 pages ahead or behind the current page).
Well I'll be.
It appears that it was the 'draw' variable not being incremented correctly. Makes sense I guess, the js state machine probably uses this to help identify a dataset in memory and I guess it was trying to render a dataset that wasn't available? My best guess at what was going on.
Anyway, I seem to have solved my problem by making a small change in the ajax routine:
I just added a way for the incoming draw variable to inform the value of the draw variable in the server state.
Sorry for the noise everyone, and thank you @colin for taking a look over my mess
Excellent, glad all sorted! Thanks for reporting back,
Colin