Pagination on server side calls doesn't seem to be working correctly.
Pagination on server side calls doesn't seem to be working correctly.
I have built the backend processing for this in Coldfusion. When I run the call with endrow or maxrows set on the data build, the total rows and filtered rows seems to be ignored as the page only displays the default number of rows and doesn't allow paging, even though the total rows on the returned json shows several thousand records.
If I remove the maxrows and endrow attributes from the loop, the pagination looks correct but it also looks like the entire data set is being loaded at once instead of really doing server side processing.
jquery:
$.getJSON(url, function(json) {
$('#'+thisID).DataTable({
data : json.data,
columns : json.columns
});
});
ColdFusion code within a cfc function:
<cfquery datasource="datasource" name="result">
select
*
from db
</cfquery>
<!--- Total data set length --->
<cfquery datasource="datasource" name="qCount">
SELECT COUNT(id) as total
FROM db
</cfquery>
<cfif result.recordcount gt 0>
<cfset recordsTotal = qCount.total>
<cfelse>
<cfset recordsTotal = 0>
</cfif>
<cfsavecontent variable="JsonObject">
{"draw": <cfoutput>#val(arguments.draw)#</cfoutput>,
"recordsTotal": <cfoutput>#recordsTotal#</cfoutput>,
"recordsFiltered": <cfoutput>#result.recordCount#</cfoutput>,
"columns": [
{
"title" : "ID",
"data" : "id"
},
{
"title" : "Name",
"data" : "name"
},
{
"title" : "User ID",
"data" : "user_id"
}
],
"data": [
<cfif result.recordcount gt 0>
<cfoutput query="result" startrow="#val(arguments.start+1)#" endrow="#val(arguments.length)#">
<cfif result.currentRow gt (arguments.start+1)>,</cfif>
{
"id": #SerializeJSON(result.currentRow)#,
"name": #SerializeJSON(result.name)#,
"user_id": #SerializeJSON(result.user_id)#
}
</cfoutput>
<cfelse>
""
</cfif>
]}
</cfsavecontent>
This question has an accepted answers - jump to answer
Answers
For server side processing to work you need to use the Datatables
ajax
option. Like these examples. Thedata
option simply reads the data supplied. It has no concept of server side processing. Additionally for server side processing to work you need theserverSide
option enabled and a server script that follows the server side processing protocol to handle the paging, sorting and searching operations.Kevin
Is it still possible to build the columns server side or do I have to have all the columns in the html table?
The
columns.title
will build thethead
. You will need to have the columns defined before initializing Datatables, like you have in your first code snippet.This is a client side with ajax loaded data example:
http://live.datatables.net/regatofe/1/edit
This is the same example using server side processing:
http://live.datatables.net/qimukefe/1/edit
Kevin
Thank you.
Is this the format I should be using for the returned json?
I get an error:
Cannot read properties of undefined (reading '0')
On this line:
column header json
Maybe
Object.keys(data.data[0])
is not appropriate for the JSON data being returned. The example I provided derives the column names from the first object in the array. You certainly don't have to do the same. You will need to extract the column information to build the columns configuration options that Datatables supports.Kevin
Is it possible to include the desired column names, instead of just using the db field values, in the data information? I thought I had seen that somewhere in the documentation but I cannot find it now.
Sure. Your server script can respond with the column information. Since its an ajax request outside of Datatables you can do what you want. The JS Bin environment is limited on ajax resource, so the example just uses the data it can fetch
Kevin
Thank you for the help. It is working.
Okay, the page is loading and paging correctly but the sorting variables don't seem to be getting sent back to the server when a column header is clicked.
Here's my revised calling code:
This is the data getting sent back to the server during the ajax call. It doesn't change no matter which column I click on the front end.
The order is indicated with this:
See the Server Side Processing docs for details. The server script needs to look for these parameters to build the query to include column ordering.
Kevin
Right, but the data that is sent to the server when a column header is clicked never changes from null to something that the server can process.
Not sure what
null
value is being sent to the server. Looking at this example. When the table first initializes it sends the default ordering of column 0 ASC and the draw parameter is 1:I Sort another column, the draw parameter increments and it sends the new column sorting:
Is this not happening for you?
Kevin
I see some parameters like
"datamismatch":0,
which aren't sent by Datatables. Do you know where they are coming from?I noticed the
"order":null,
. AFAIK this is not something Datatables sends, even if there is no ordering to the table. I suspect these parameters are coming from one of these statements:Maybe verify what
systemVars
are.Kevin
I'll check. Thanks.
System vars is a cache clearing value for IE.
The data that is getting sent by dataTables to the server is strings of key-value pairs. I thought it was supposed to be structured in arrays. Is this correct? (Not all data included here.)
The Server Side Processing doc describes the parameters sent and the expected parameters in the response. The parameters your are showing have more than what Datatables sends like
datamismatch
. Other than the extra parameters it looks correct. You can use the browser's network inspector tool with any of these Server Side Processing examples to see what is sent and the format.Kevin
But the data does not seem to be coming to the server as arrays like the docs say. Each variable is a separate string. On the server side I have tested the order and columns variables to see if they are arrays and they are being listed as individual strings for each row.
From the docs:
The order[i] and columns[i] parameters that are sent to the server are arrays of information:
order[i] - is an array defining how many columns are being ordered upon - i.e. if the array length is 1, then a single column sort is being performed, otherwise a multi-column sort is being performed.
columns[i] - an array defining all columns in the table.
In both cases, i is an integer which will change to indicate the array value. In most modern server-side scripting environments this data will automatically be available to you as an array.
If I try to check if order is an array (isArray(arguments.order)), I get an error that "order" is not a key in the variables. I have to run a check on the entire order[][column] to be told that it is not an array.
jQuery ajax() will serialize the parameters. What you are seeing is how the arrays are serialized. Sounds like you are wanting to receive JSON data instead. See the last example in the
ajax.data
for how to send JSON data instead of the default you are seeing.Kevin
...
Look at the ajax.data example again. You're not using it correctly.
Right. I edited my comment when I noticed the problem but not quickly enough.
error: cannot read properties of undefined (reading 'aDataSort')
Lines 17-22 and 28 are inside your
ajax
option. They should be at the same level as yourajax
option. for example:Kevin
Fixed that and don't get errors but the table hangs on processing after the initial load when I try to page or sort. I did check the data being sent back to the jquery and it is the same as before which was working for paging.
Sounds like the
draw
parameter doesn't match the value sent from the client.Kevin
Draw is set to 1 in the data being sent back from the server. I also confirmed that the data being sent back is properly formatted json.
As an added oddity, the data being sent by jquery to the server no longer includes all of the column and order data like it did before.
All of this and the other similar values are missing now from what the server gets.
"columns[12][search][regex]":"false",
"columns[0][data]":"id",
"columns[7][search][value]":"",
"columns[5][searchable]":"true",
"order[0][column]":"0",
"columns[4][search][regex]":"false",
"columns[9][search][value]":"",
"columns[8][search][value]":"",
Can you link to your page so we can take a look, it would help understand the data being returned,
Colin
I wish I could but the system is on an intranet. Is there anything else I can do to provide more information? I would run this in the test environment but it's a ColdFusion application, not PHP.