vitmalina / w2ui

UI widgets for modern apps. Data table, forms, toolbars, sidebar, tabs, tooltips, popups. All under 120kb (gzipped).
http://w2ui.com
MIT License
2.65k stars 730 forks source link

w2ui 2.0.dear sir,how to change combo items at runtime."can't refresh" #2548

Closed introspection3 closed 3 weeks ago

introspection3 commented 1 month ago

Short description dear sir,how to change combo items at runtime."can't refresh"

What is current behavior ...

What is desired behavior ...

Link to jsfidle/codepan with sample code ...

   var field = w2ui.tunnelForm.get("targetClientId");  
                    if (field) {
                        w2ui.tunnelForm.record['targetClientId'] = "";
                        let dropdownlistUrl='/user/dropdownlist/'+selectedClientId;
                        let dropdownlist = await fetch(dropdownlistUrl);
                        let data=await dropdownlist.json();

                        field.options.items =data;

                            w2ui.tunnelForm.refresh();

                    } else {
                      console.log('not found targetClientId');
                    }

              }

Steps to reproduce or sample ...

WillTheFarmer commented 1 month ago

change field.options.items = data; to field.w2field.options.items = data;

This will update and populate list at runtime.

There is no need to refresh form. You will not need to execute - w2ui.tunnelForm.refresh();

Hope this helps!

ope-nz commented 4 weeks ago

@WillTheFarmer thanks for that, lifesaver! I had been banging my head trying to update a list on a form on a popup.

BTW I have found that depending on whether the form has been previously rendered that you have to do either of the methods above. That is, if the form has not been rendered then field.w2field is undefined and will error.

`let field = w2ui.form_source.get(FieldName);

if (field.w2field){ //Form has been rendered before field.w2field.options.items = services; } else { //Form has not been rendered before field.options.items = services; }`

WillTheFarmer commented 4 weeks ago

@ope-nz you're welcome! Yes, depends if form has been rendered. I banged my head for a while as well and spent many hours going through w2ui class code and using Chrome DevTools. First time through onRender works without w2field. I add form properties to track methods that fire often. Example:

    onRender: function (event) {
        console.log('onRender', event);
        if (this.lonRender) {
            console.log('onRender - THIS IS THE ONLY TIME THIS WILL EXECUTE.');
            this.lonRender = false;
            this.requeryDropdowns();
        };
    },
vitmalina commented 3 weeks ago

You should also be able to do this

w2ui.tunnelForm.set("targetClientId", { options: { items: data }})

which regenerates the field too

ope-nz commented 3 weeks ago

@vitmalina sorry to flog a dead horse here but the approach you posted does not work for a form in a popup. I dont know if this is because it is in a popup but the behaviour is different.

This does not work for a form in a popup;

w2ui.tunnelForm.set("targetClientId", { options: { items: data }})

This does work for a form in a popup;

let field = w2ui.form_source.get(FieldName);

if (field.w2field){
//Form has been rendered before
field.w2field.options.items = services;
}
else
{
//Form has not been rendered before
field.options.items = services;
}