Server side example without MySQL
Server side example without MySQL
Brecht2727
Posts: 28Questions: 4Answers: 0
Can someone provide me an example how to work in ajax server side without mysql?
I have a cURL in my ajax and with a foreach a loop through all records. That's working, but column sorting, pagination or search is not working.
Answers
The simple server-side example is here:
https://datatables.net/examples/server_side/simple.html
@tangerine: your link is still with MySQL database
Are you wanting to enable server side processing? You don't need to just for ajax loaded data. See this doc for the differences. If you still need server side processing for your solution then you will need to write a server side processing script that follows the SSP protocol. If you don't need SSP then the ajax loaded data will be sorted client side.
Kevin
I think server side processing will give me more possibilities? Not shure about it. Just a thought.
I have to reload the data in the table sometimes and i need also checkboxes for every row, search option, pagination and 2 datepicker fields (date from - to). I know how to work with server side script with MySQL but not without.
I will try to use the SSP protocol. Do you have an example how to use it without database? Because all examples i can find is with database
Server side processing is used for large data sets that would otherwise cause performance issues if the full set of data is loaded at the client. Server side processing requires the server script to perform paging, searching and sorting then return just the page of data to the client. SSP will actually be more complicated as many of the client side tools like
row.add()
or Search API's aren't enabled. It is expected that the SSP script perform these activities.Unless you have thousands of rows that cause performance issues you will be better served with client side processing. You can do all those thing you mentioned without the complexities of creating a SSP script. You just need to return all your data via ajax.
Kevin
I am trying to use the client side processing but have a question about sending extra params with the data. How can i extend the data?
I need to send also the 2 datepicker fields (date from - to) to the ajax.
My code, for now, looks like this:
You will want to use the
data
option. Refer to the jQuery ajax() docs for details.Kevin
@kthorngren: I was not thinking that way, but it works like you suggested.
I was thinking in my case to extend the
data: data
from the datatable. Is this also possible to do? Maybe for future projects? Just to know ;-)In DataTables terms, what you have there could be rewritten as:
i.e. no need to make your own
$.ajax()
call. Theajax.dataSrc
property is set to an empty string to tell DataTables to expect an array of data.I've also taken the opportunity to simplify your
columnDefs
array since there were repeating declarations.Allan
Thanks Allan for the tips, but i have the problem that the
data: data
is coming from theajax success
so i can not use this, i think?Is there a solution for my second question: how to extend the
data: data
from the datatable for client sideRemove
data: data
and let the Datatablesajax
option populate the table.Kevin
Thanks Kevin, but now we come to the same problem we had before. How can i send my extra data params (date from-to input fields) with this code:
Use
ajax.data
as a function. There are examples in the docs.Kevin
Oh yes, now i see... Many thanks to Allan and Kevin for the help.
All working now. I end with this code for the ajax part in the datatable:
Complete code is now:
If you use something like
ajax.relaod()
to reload the table when the datepicker values change you will need to useajax.data
as a function to get the changed values. Otherwise the values sent will not change. They will be the values at the time of initialiation.Kevin
Ok Kevin, thanks. I changed my code:
But when i use the reload function i have an error
Uncaught TypeError: Cannot read properties of undefined (reading 'reload')
ajax.reload()
is a DataTables API method, not a jQuery one.Kevin - thanks for catching my error earlier!
Allan
Thanks Allan, error is gone but in console i can see that the
setInterval
is working but i can`t see the datatable is reloading. There is no indication likeLoading
message in the table?I figured out that the option
processing: true
must be enabled to see the message! Now it is ok. But is it possible to restyle this message? I am using bootstrap 5Sure - use the
div.dataTables_processing
selector in CSS and style as you require.Allan
There is something else after ajax.reload function. Is it possible to hold the pagination position? Now after reload the paging is start from 1 again.
In server side processing we have the serverState: true but it is not working on client side.
Take a look at the
ajax.reload()
docs for controlling paging. The second example in the docs show how to stay on the same page.Kevin
Yes, everything is working now! Now i have a fully working example of client side script.
Many thanks Allan and Kevin!!!