Is it possible to add a button to the StateRestore Actions?

Is it possible to add a button to the StateRestore Actions?

desperadodesperado Posts: 159Questions: 33Answers: 4

My users have requested an interesting feature. They would like to be able to make one of their saved states public so everyone who has access to the page can use the state they created.

I would ideally like to add this feature by having an extra button along with "Update, Rename, Remove" that will toggle between "Make Public" and "Make Private", separate buttons named the same could also work. I intend to mark public states by altering their name, for example adding * in front of the name to indicate it is a public state. I also intend to have the ajax save to only allow the original author to save. So if there could be a function to get the label for the given namedState it could easily return the two labels based on the name alone.

We considered just implementing that if the name starts with * then it's public but that would make it sort of a "hidden" feature so adding button(s) would be ideal.

I could just add a button on the page which says "Make current State Public" but since there can be multiple current states that is a little less elegant.

I could also have a button "Create Public State" and relabel the "Create State" to "Create Private State". So the ability to add buttons to the savedStatesCreate menu could be yet another option.

This question has an accepted answers - jump to answer

Answers

  • sandysandy Posts: 913Questions: 0Answers: 236

    Hi @desperado

    This is very much tailored functionality so it won't be something that we will look to implement ourselves. "Update, Rename and Remove" can be added in a few different places - here, here and here. You could edit the code yourself and host your own version that adds another custom button at those points. This custom button could then perform whatever functionality you wanted.

    I'm wondering if there is a way we could provide functionality to specify which buttons should be placed in the split dropdown. I'm going to talk to Allan and Colin about this today and will respond back here with our thoughts.

    Thanks,
    Sandy

  • desperadodesperado Posts: 159Questions: 33Answers: 4
    edited December 2021

    @sandy
    I wonder if you added unique classes to the split dropdown for StateRestore if I could target with some javascript and append buttons to the splits.

    A method of adding a function to be called to get additional buttons would be ideal.

  • sandysandy Posts: 913Questions: 0Answers: 236

    Hi @desperado ,

    We've been talking and I'm going to implement my suggestion above as a method to customise which buttons are present within the split dropdown. I've raised an issue internally (DD-2411 for my reference) and will report back here when there is an update - hopefully on Monday.

    Thanks,
    Sandy

  • desperadodesperado Posts: 159Questions: 33Answers: 4
    edited December 2021

    @sandy great! thanks.

    Man I am enjoying StateRestore, your doing an amazing job!

    FYI, I posted another question about adding unique class to the various dt-button-collection elements so we can target them better in our customization css. I think having a class on each key element such as dt-state-restore-{element} could really come in handy.

  • sandysandy Posts: 913Questions: 0Answers: 236
    Answer ✓

    Hi @desperado ,

    Thank you for the feedback - I'm glad it is useful for you!

    That should be the issue fixed now as you can see at this example. This will be available in the next StateRestore release which we hope will be in the next few weeks. Until then you can access the fix from the nightly builds. Note that you may also need to use the nightly version of buttons for this too.

    Thanks,
    Sandy

  • desperadodesperado Posts: 159Questions: 33Answers: 4

    @sandy I finally got around to trying this ability to add a button. But I have a few questions.

    http://live.datatables.net/nizoyeho/1/edit

    1) How do lower the dropdown when the button is selected?
    2) How do I know which State was selected ?

  • desperadodesperado Posts: 159Questions: 33Answers: 4
    edited December 2021

    @sandy Ideally if I can provide a function and passed a state I could build the command I want. I would like to be able to check if the State is Public or not and alter the button to say "Make Private" or "Make Public" based on the current state.

    I would also like to be able to alter the text of the State to indicate if the State is public or not, not sure how I can do that either.

    For example I would like to have the dropdown of states look like this.

    Where the ** indicates it is a public view. I also want to add a tool tip over it indicating which user made it public. But I can get all that data I just need to know the state info when I am building the button and to be able to apply some logic to produce the button name.
    (NOTE: My users prefer the name "View" over "State" so if I mix that up just replace "View" with "State" whenever I use the term :smile: )

  • sandysandy Posts: 913Questions: 0Answers: 236

    Hi @desperado ,

    You're making things difficult for yourself in the way you are creating that button! Take a look at this page of the documentation. The buttons.buttons.action initialisation option is going to be your focus for what happens when the button is clicked.

    You can access the states in a similar way to how StateRestore does it - see here.

    Button text can be changed via the api using button().text().

    Thanks,
    Sandy

  • desperadodesperado Posts: 159Questions: 33Answers: 4

    @sandy Thanks, that helped a lot however I can't for the life of me figure out how to drop the split popup. I know I am missing something stupid but can't figure it out so far!

    http://live.datatables.net/nizoyeho/2/edit

  • desperadodesperado Posts: 159Questions: 33Answers: 4

    @sandy Sorry but I still can't figure out how to do two things.
    1) Make my savedStates split secondary button have two labels based on the state of the public setting. When public the button should read "Make State Private", When the state is private the button should read "Make State Public".
    I see I can pass a function to the text config but how would I know in that function which state identifier I am building the button for ?
    2) How can I alter the label of the States themselves. I could add a class or change the label. I would like to add a class so I can use before to add an icon or something. Or I could just change the label. But again I would need this to be a dynamic function so I could check if the state is public or not and add the icon/text based on that state.

    For #1, I see I can create the button and pass a function to the text config. But I don't know how I can pass the state identifier into my dynamic text function.
    See this example, do I have access to the state the split is being created for when I configure the button text as a function?
    http://live.datatables.net/nizoyeho/4/edit

    For #2 I can actually solve this on sever side by changing the State Identifier in my database when I am setting the public boolean but if there is an easy way to configure Saved States collection to call a function for each state to get it's label then I could implement on client side. Or if I can conditionally add a class based on the state that count work. I don't think either of these options exist looking at the stateRestore options and api. So I can use my server side solution if that is correct.

  • sandysandy Posts: 913Questions: 0Answers: 236

    Hi @desperado,

    As I said above, you can set the text of buttons using button().text(), the button() api method gives you many ways of identifying which button you want to act on. You could also simply use the stateRestore.state().rename() api method and that will update the button text for you.

    Also as mentioned above, you can look at GitHub to see how StateRestore accesses the states within buttons. You are accessing the property correctly in your example so I'm not sure what you are struggling with there I'm afraid.

    How you track which states are public and private is going to be up to you I'm afraid - that's not something we support!

    Thanks,
    Sandy

  • desperadodesperado Posts: 159Questions: 33Answers: 4

    @sandy I apologize I worked all day on writing my example but somehow the example I linked is not the one I created. I will work on the example again after the Holiday and show what my problem is. I completely understand your statements that I can use the button().text api but I have been unable to determine how I can get access to the buttons from the saved states split menu.

    If you are reading this before Monday 12/27 then know that I will post another comment Monday to better explain my problem. Enjoy your holiday, Merry Christmas.

  • desperadodesperado Posts: 159Questions: 33Answers: 4

    @sandy Here is my example of the issues I have. I placed the issue description in the code as comments. I hope this makes what I am trying to do more clear and perhaps you can tell me if I am just doing it wrong.

    http://live.datatables.net/suxojafa/1/edit?js,output

    Two issues summary (example should make these two issues more clear)
    1) I don't know how to make the secondary split popup lower when the button is selected and my function is called.
    2) splitSecondaries button creation allows a function. Is there a way to pass the state (or state identifier) to this function ?

  • sandysandy Posts: 913Questions: 0Answers: 236

    Hi @desperado ,

    I think I'm right in saying that you have created new threads for these issues? Is that right?

    Thanks,
    Sandy

  • desperadodesperado Posts: 159Questions: 33Answers: 4
    edited January 2022

    @sandy Yes. This thread was getting too confusing. All done here! Thanks Again....and again....

Sign In or Register to comment.