Skip to Content
Menu
This question has been flagged

I have a custom widget in a list view to show a selection field. I am using ListRenderer.include from javascript side to add my widget. Now from Listcontroller.include, my  custom java script function is doing some logic in same list view, I am calling this.reload(); to reload the view at the end of the function, my problem is I don't want to change selection of above-mentioned field but when this.reload(); is triggering, it is resetting the value of the above selection field.


Here is my field.

select class="form-control my_field">
t-foreach="widget.my_widget" t-as="dict_a">



In JavaScript I am storing this field's selection ID in local storage when user changes this field. But when this.reload(); happens, it is resetting the value of this field in widget view but not in local storage. Now view has something else, but value being used in functions is something else. How can I avoid it?

Here is my function responsible to handle field value in ListRenderer.include

_on_change_my_field: async function(ev) {
ev.stopPropagation();
var selection = $(ev.currentTarget).val();
localStorage.setItem('localstorage_field_name', selection);
},


here is a function from ListController.include having this.reload();

_myfunction: async function(myinput){
var data = localStorage.getItem('localstorage_field_name') || '';
console.log(data);
// console: 8
//do the logic
this
.reload();
var test_data = localStorage.getItem('localstorage_find_name') || "";
console.log(data, test_data)
// console: 8, 8
}



Avatar
Discard
Best Answer

If you want to avoid resetting the value of the selection field when calling this.reload(), you can try setting the value of the field directly in the widget before calling this.reload(). This way, when the view is reloaded, the value of the field will not be reset. Here is an example of how you can do this:

In your ListRenderer.include, add the following code to your _on_change_my_field function:

// Store the selection in local storage localStorage.setItem('localstorage_field_name', selection);

// Set the value of the selection field in the widget this.widget.my_field = selection;

Then, in your ListController.include, you can call this.reload() as usual:

_myfunction: async function(myinput){ var data = localStorage.getItem('localstorage_field_name') || ''; console.log(data); // do the logic this.reload(); var test_data = localStorage.getItem('localstorage_find_name') || ""; console.log(data, test_data) }

This should ensure that the value of the selection field in the widget is not reset when calling this.reload().

Avatar
Discard
Related Posts Replies Views Activity
0
Feb 23
1723
0
Jan 21
2311
2
Jan 22
6714
0
Feb 16
4783
1
Feb 24
380