Adding options to a dependent choice list field

Dubz
Mega Sage

Hi All

I'm just wondering if anyone has any simple workarounds for adding or removing choice options to a choice list field that is dependent on another field?

g_form.addOption() and g_form.removeOption() don't work as they seem to get overridden by the choice options provided by the dependency relationship.

I don't really want to add the option permanently but i also don't want to have to completely re-configure to acquire and populate the options via glide ajax but i don't see any other ways of achieving what i need.

Any ideas?

Cheers

Dave

 

EDIT: My current workaround is to run the onChange on the field i need to add the option to but this means that users who need that choice option have to go into the field and change it to some random value first and then go back in to the field and select the correct option. It's not the most elegant solution and it doesn't add the option according to the sequence i've specified.

1 ACCEPTED SOLUTION

Dubz
Mega Sage

OK so it seems like there is no way around the field dependency. The only workaround is to run the onChange on the field you want to add the option to. Not ideal as the user has to open the field and select a value and then go back in and change it to the newly available option.

View solution in original post

2 REPLIES 2

Dubz
Mega Sage

OK so it seems like there is no way around the field dependency. The only workaround is to run the onChange on the field you want to add the option to. Not ideal as the user has to open the field and select a value and then go back in and change it to the newly available option.

hunter_phillips
Tera Contributor

Could potentially tweak whats working for us,

OnChange on the Field that you need to add options to:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading) {
		return;
	}
	if(newValue == ''){
		g_form.addOption('subcategory', 'new1', 'New 1');
		g_form.addOption('subcategory', 'new2', 'New 2');
   }
}


the if(newValue == '') is important -- it seems that Dependent fields are always cleared out when the field it's dependent on changes.

So what I've provided works granted you want those options to always be available..


However, if you only want certain options for certain situations, you could add additional logic to check the 'depending' field's value and add options accordingly

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading) {
		return;
	}
	if(newValue == ''){
                
                // ADDITIONAL LOGIC
                if(g_form.getValue('other_field') == 'abc'){
           		     g_form.addOption('this_field', 'new option', 'New Option');
                }
   }
}