Saving/renaming states for stateRestore in database

Saving/renaming states for stateRestore in database

trongarttrongart Posts: 222Questions: 51Answers: 0
edited February 2022 in StateRestore

I am exploring how to load, save, and rename states for stateRestore from a SQL database with Ajax. I understand that it is possible to save and load states from stateRestore using Ajax as per this example. To load the table data and stateRestore states, the Ajax data must come in this format.

I'm struggling to find examples how to write the relevant Ajax data to save/rename a state in a database for stateRestore as this is a relatively new feature. Ideally I am looking to save/rename states by modifying/adding elements to the same format and to write them into the database. I also cannot find any similar Ajax writing code for datatables as I believe this is only used in stateRestore.

Are there maybe any examples I could learn from or refer to for this? Appreciate your input!

Answers

  • colincolin Posts: 15,177Questions: 1Answers: 2,590

    If you look at the Network tab in the developer's tools on your console in the example you linked to, you can see what's being sent to the server.

    The script I pointed to you before, shows how you can then save it on the server. You just really need to treat it as a blob,

    Colin

  • trongarttrongart Posts: 222Questions: 51Answers: 0
    edited February 2022

    Thank you @colin- I managed to run the same test case as here on my server using your txt file in stateRestoreLoad.php for the data. The data is loaded correctly and stateRestore loads and saves as expected. However, the states are saved in the local session instead of in the txt file even though stateRestoreSave.php is used. What needs to be done to add/remove/rename stateRestore items within the txt file (as I can later adjust this to work with the database)? Is there maybe an example where this is saved server side/txt file?

  • trongarttrongart Posts: 222Questions: 51Answers: 0
    edited February 2022

    Just to rephrase- it would be incredibly helpful to have an example for stateRestoreSave.php with an SQL server (or txt file).

  • colincolin Posts: 15,177Questions: 1Answers: 2,590

    The example that we have just shows how the browser communicates with the back-end. There are many ways of then dealing with that data, from a DB to a file, so we leave that to the developers to decide how they wish to implement it.

    Colin

  • AKASecretAKASecret Posts: 9Questions: 2Answers: 0

    Sorry to bring this up again more than a month later...I'm just looking for some guidance.
    I am also hoping for an example of how to save/load states from a database. What I'm trying to do is save "user states" to my SQL Server database (not using PHP) so they don't get lost when localstorage is cleared. I don't even know what steps I'm supposed to take to begin to accomplish this task. I have so many questions about the configuration, and, like @colin said, the documentation seems to stop after the data is written. I can't figure out how to incorporate the database with the given documentation.
    1. What columns are supposed to be in my database to save the state? I'm imagining a table called UserStates with three columns: Key (varchar(250)), Value (varchar(250)), UserId (varchar(250)). I still don't understand what "Key" is supposed to represent, but "Value" is the AJAX string. These two I can see from Local Storage. UserId will be added from...I don't know yet...I guess a jQuery variable somehow...
    2. Once the state is saved in my database, do I retrieve it by the following code?
    { extend: 'savedStates', config: { ajax: '../StatesController/GetStateData', type: 'POST' } }
    3. How do I only show the current user's saved states? I assume that comes from the GetStateData method, and pull all States.Where(i => i.UserId == userId)?
    4. How do I tell Datatables to use the database to save/load states?

  • sandysandy Posts: 913Questions: 0Answers: 236

    Hi @AKASecret ,

    You're right, we don't have any documentation beyond that point because we don't provide any code to deal with the wide variety of options for developers when it comes to storing the states.

    1. Yes that could work. The key could be the state name which is the unique identifier for that state. For what you have described above I would suggest a compound key of "key" and "userid" so that different users can store states with the same id. As for storing the user id, that is down to you and how your site works I'm afraid.
    2. That depends how your backend is set up. You can read the documentation for what StateRestore includes in it's ajax requests at stateRestore.ajax.
    3. That's something you are going to have to work out in your backend. You can set the stateRestore.ajax property to be a function, then you can alter the data in whatever way you want. This could mean adding a user id to the ajax request if that is helpful. Again though, this is on you to implement.
    4. StateRestore should work out that you have set an ajax url or function using stateRestore.ajax and will perform all save/load operations through that url, no need to do anything else.

    Hope this helps,
    Sandy

Sign In or Register to comment.