Onchange client script to show/hide options

mballinger
Mega Guru

Hello,

I have a request to show choice options based off of a selection made. This needs to be an onChange client script. I have a category and subcategory field. Corresponding subcategories need to display for the selected category. Subcategory choices are currently set in the field.

My issue that I am facing is I can remove the choices, but they do not get readded back if a different category is selected. Also, if I use the addOption, I get a huge white space in my select box for the subcategory. Also, the clearOptions on the onChange only clears the options if an item is selected.

What is the best way to do this?

onChange Client Script: 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

        var category = g_form.getValue('u_category');
	g_form.clearOptions('u_subcategory');
	
	if(category == 'accounting') {
		g_form.removeOption('u_subcategory', 'strategy');
		g_form.removeOption('u_subcategory', 'planning');
	}
	
	if(category == 'marketing') {
		g_form.removeOption('u_subcategory', 'financial');
		g_form.removeOption('u_subcategory', 'auditing');
		g_form.removeOption('u_subcategory', 'quick_books');
	}
}
1 ACCEPTED SOLUTION

bammar
Kilo Sage
Kilo Sage

Go check out Incident and see how they made it work. But essentially all you do is go to the dictionary for Subcategory and make it dependant on Category. Then pick  a category, then jump to subcat right click and set up the choices. Then save and go back and pick the next choice. So what I am saying is this can be done with 0 coding. 

View solution in original post

4 REPLIES 4

Tiffany Royer
Giga Guru

you need to add them back in

g_form.addOption(field,choice value, choice label, order)

bammar
Kilo Sage
Kilo Sage

Go check out Incident and see how they made it work. But essentially all you do is go to the dictionary for Subcategory and make it dependant on Category. Then pick  a category, then jump to subcat right click and set up the choices. Then save and go back and pick the next choice. So what I am saying is this can be done with 0 coding. 

I like this option much better. So I've selected the dependent and set subcategory to be dependent on category. When I go into the choice, how do I set the dependent value there?

Would I do "category.accounting" as the dependent value for all the choices using the accounting category? Also which field should I reference on the incident table?

This worked! Thanks @bammar