Saving datatables state into $_Session[]
Saving datatables state into $_Session[]
thefireescaper
Posts: 11Questions: 3Answers: 0
I am using server side processing. would like to save the current datatables state into PHP $_Session[] vars - capturing the offset, rows, SQL search etc into separate session vars so that I can use these same params elsewhere on the site. Is this possible? Thanks
Answers
Sure thing - use our
stateSave
option along with thestateSaveCallback
andstateLoadCallback
options to define the Ajax calls that you will need to sent to your server-side script that will save / load the state.Allan
Thanks @allan . Am really struggling to get this to work.
So I added a stateSaveCallback to my table which I presume is called on every draw.
The table is defined as below:
The file I am using for the callback is below. All I am doing in there is trying to test it by setting the session var with a text value/string. The lock.php file included at the top contains the session_start()
```
<?php
require_once("../resources/templates/functions.php");
require_once("../resources/templates/lock.php");
// load up your config file
require_once("../resources/config.php");
$_SESSION["Admin_users_table_search"] = "this is where I will put the session data";
<?php > ``` What am I doing wrong? When I display the table and then output the session var it says undefined. This is driving me mad. Do you have an example of this working ie. extracting the state? Thank you ?>cc @colin
Add
print_r($_POST);
and you'll see the data being sent to the server. I'd probably do:in your
stateSaveCallback
then you can save$_POST['state']
into your PHP session variable.We don't have a working example of that. I'm a little confused why you would want to do this to be honest - if you are just saving to session, why do it on the server-side rather than using the client-side where you can use
sessionStorage
?Allan
Thanks @allan - within my application I have two tabs. The first tab is the datatable listing a number of items. The second tab is the charts associated with each entry of the table. All of this is driven by mySQL database on the server side. I want to ensure that any action - sorting, search, filtering, etc.. in the table is reflected in the charts view as well (which is just a straight forward call back to the database). I do intend to store the state within the database. Just trying to get it working.
Does that make sense?
Ajax is new to me so struggling a bit.
Thanks
Okay I think I understand. So your chart is being built on the server-side and you need to access the state when you are building it in order to have it reflect your DataTable state.
In that case, yes I think your approach makes sense since you need that information at the server-side.
I like the sound of this - sounds like nice UX to me!
Regards,
Allan