Saving datatables state into $_Session[]

Saving datatables state into $_Session[]

thefireescaperthefireescaper Posts: 11Questions: 3Answers: 0
edited August 2022 in Free community support

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

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    Sure thing - use our stateSave option along with the stateSaveCallback and stateLoadCallback options to define the Ajax calls that you will need to sent to your server-side script that will save / load the state.

    Allan

  • thefireescaperthefireescaper Posts: 11Questions: 3Answers: 0
    edited August 2022

    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:

    <script>
    var editor2; 
    
    $(document).ready(function() {
        editor2 = new $.fn.dataTable.Editor( {
            "ajax": "admin_users_table.php",
            "table": "#admin_users_table",
            "fields": [ {
                    "label": "ID:",
                    "name": "id"
                }, {
                    "label": "Name:",
                    "name": "name"
                },{
                    "label": "Email:",
                    "name": "email"
                }, {
                    "label": "Role:",
                    "name": "role"
                }
            ]
        } );
    
        $('#admin_users_table').DataTable( {
            dom: "Bfrtip",
            ajax: {
                url: "admin_users_table.php",
                type: "POST"
            },
            columns: [
                { data: "id" },
                { data: "name" },
                { data: "email" },
                { data: "role" }
            ],
        stateSave: true,
        "stateSaveCallback": function (settings, data) {
           $.ajax( {
             "url": "admin_users_state_save.php",
             "data": data,
             "dataType": "json",
             "type": "POST"
          } );
        },
        serverSide: true,
        paging:true
        } );
    } );
    </script>
    
    
    
    

    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

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    Add print_r($_POST); and you'll see the data being sent to the server. I'd probably do:

    data: {
      state: data
    }
    

    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

  • thefireescaperthefireescaper Posts: 11Questions: 3Answers: 0
    edited August 2022

    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

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    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

Sign In or Register to comment.