Leave a fixed option on a dynamic table - JS

Asked

Viewed 82 times

1

I have a dynamic table, which changes according to the user’s select done in HTML. However, every time the page updates the selected option ends up changing too.

I thought of two possibilities that might work out for what I want:

  • Create a variable and pass the current choice to that variable. Create a button called "Load" and every time this button is triggered, it will press whatever is inside that variable;
  • Best way: create a default option and go updating this default option according to the user’s choice.

The first option I even managed to do, but it keeps "updating" the page and losing value.

Here is the example of the code: http://jsfiddle.net/kBQdS/247/

Thank you,

1 answer

1

store the value of the select in the localStorage, when loading the page, update the value of select and call the event change.

var dynamicTable = (function() {
    ...
})();

$(document).ready(function(e) {        
    var data1 = [...];        
    var data2 = [...];        
    var dt = dynamicTable.config(...);        

    $('.montadoras').change(function() {
        localStorage.setItem('montadoras_value', this.value);
        ...
    });

    var montadoraAtual = localStorage.getItem('montadoras_value');
    $('.montadoras').val(montadoraAtual).trigger("change");
});
  • Thanks Tobias, the idea worked. The problem is that as there are several people filling out the same form, if one updates, it ends up updating everyone. Can you do something like this where each form is unique?

  • @user2287892 the localStorage exists only in the client (Browser) and cannot be accessed on the server, so if people are using different computers, then they are isolated. If more than one person uses the same computer and browser, you will have to add a unique key to the registry name in localStorage, such as an email... eg: localStorage("[email protected]", value).

  • The form is accessed via server, IE, it ends up taking the localStorage from the same place. But you could not create an array as a single key or is not allowed?

  • Just to complement, I have an ID that is unique to each form. How could I use it to my advantage, i.e., store the localStorage within that ID, as a single key?

  • I gave an evolved in my question, I am able to bring the number automatically generated by the form as "key" (I can visualize through the console), the problem that every time I update the page, it loses the information. This was not happening before. var nsit = Document.getElementById("nsit"). value; localStorage.setItem(nsit, this.value); var montadoraAtual = localStorage.getItem('nsit'); $('.assemblers'). val(montadoraAtual). Trigger("change"); What may be wrong?

  • @user2287892 you are going to string 'nsit' and not the variavel nsit to the localStorage.getItem(nsit).

  • Now it’s all worked out. In addition to what you said, I had to put var nsit = Document.getElementById("nsit"). value; out of $('. assemblers'). change(Function() Thanks for the help

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.