stateSaveCallback etc on click

stateSaveCallback etc on click

george001george001 Posts: 14Questions: 5Answers: 3

How can I implement stateSaveCallback / "colReorder" / "buttons": ['colvis'] to be enable when the user clicks on a button?

Basically, I need the user to be able to edit the datatable only when he clicks an edit button and the changes to be saved when he press the save button... if the user doesn't press the save button no change will be saved and if he doesn't click on the edit button he won't be able to make any changes... any ideas how to do this? please

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,276Questions: 26Answers: 4,765

    Not sure I understand your question but does this example do what you want:
    https://editor.datatables.net/examples/simple/simple.html

    Kevin

  • george001george001 Posts: 14Questions: 5Answers: 3

    Hello @kthorngren

    Thanks for your answer but unfortunately is not what I am looking for. What I want is for the user to be able to save the state of the datatable onclick of a button rather than saving it automatically.

    if @allan can shed some light I will be grateful

  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin

    Good question - I don't think that's come up before. There are two options I can think of:

    1) Use stateSaveCallback to provide your own state save function. When you don't want to save the state, just ignore the function call. When you do, then save it to localStorage or similar. You'd use a flag which is set by the click listener for the logic condition.

    2) Implement your own state saving, which isn't actually all that difficult. Use page.info(), search() etc to get the information you want, and that save it somewhere. Then you can reload that information into the table whenever required. The advantage of this option is that you could allow the user to save multiple different configurations.

    Allan

  • george001george001 Posts: 14Questions: 5Answers: 3

    @allan, Thank you a lot allan! Can you provide some examples tho? (or maybe put it into the official documentation? ;) ) I need to save a state into a database by the way so each user can access 'their' state no matter pc they are using. How can ignore the function call?

  • lenamtllenamtl Posts: 265Questions: 65Answers: 1
    edited November 2017

    Hi,

    To save Datatables localstorage to DB you may check this tutorial and fork it to your needs. http://refreshmymind.com/datatables-state-save-client-server-side/

    You can add a reset button to clear the table preferences, if you want the user to get back to default table settings...

    I'm curious, why you want the user to save table preference manually (click of a button)?

    I already thought about a case for an admin to save default setting (for each tables) for all users..

  • george001george001 Posts: 14Questions: 5Answers: 3
    edited November 2017

    @lenamtl I need it so every user can make changes on their own when they really need it and not by accident...

    Thanks for the link but I used before and it is not suitable for my needs
    xx

  • george001george001 Posts: 14Questions: 5Answers: 3
    edited November 2017 Answer ✓

    Solution: B)

    var run = false;
    run = true;
    table.state.save(run); //runs the state save function 
    
    //SaveState 
                        'stateSaveCallback': function(settings, data) {
                            if (run == true) {
                                $.ajax({
                                    'url': 'hfdjs.php',
                                    'data': data ,
                                    'dataType': 'json',
                                    'method': 'POST',
                                    "success": function() {},
                                    error: function(xhr, ajaxOptions, thrownError) {
                                        console.log(thrownError);
                                    }
                                });
                            }
                            run = false;
                        }
    
  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin

    Yup - that's exactly the sort of thing I had in mind. Nice one!

    Allan

This discussion has been closed.