How to display only few choices of two fields when another filed value is selected

Lucky18
Kilo Contributor

Lets take an example like I hav 3 fields like country,state,city

If country is selected as india,then the other two field choices should show only India states in State field and cities in City field.have done something in client script but still notihg is changing.

Please let me know where I went wrong

heres the code I have tried it for country and state fields;

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


var a=g_form.getValue('u_country');

if(a=='India')
{

g_form.addOption('u_statte','ap','Ap');
g_form.addOption('u_statte','karnataka','Karnataka');
g_form.removeOption('u_statte','california','California');
g_form.removeOption('u_statte','washington','Washington');
g_form.removeOption('u_statte','victoria','Victoria');
g_form.removeOption('u_statte','south aus','South Aus');

}
}

Thanks in Advance!

1 ACCEPTED SOLUTION

asifnoor
Kilo Patron

Hi,

Technically for such things you can use dependent field but this works only when the choices are NOT static.

Your client script looks fine. 

Also, since you have written onchange on country field, you can directly access newValue variable which will give the latest selection of the country field. 

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

//var a=g_form.getValue('u_country'); 
g_form.addInfoMessage("Value is "+newValue);

if(newValue=='India')
{
g_form.addInfoMessage("Entered into if condition");
g_form.addOption('u_statte','ap','Ap');
g_form.addOption('u_statte','karnataka','Karnataka');
g_form.removeOption('u_statte','california','California');
g_form.removeOption('u_statte','washington','Washington');
g_form.removeOption('u_statte','victoria','Victoria');
g_form.removeOption('u_statte','south aus','South Aus');

} 
}

Also verify if the field name u_statte is correct or not. 

View solution in original post

20 REPLIES 20

Is there any error in your browser console?

And are you seeing the newValue in info message on every change of country?

Lucky18
Kilo Contributor

Yeah I am getting every country with its length but main thing is not happening

Rahul Kumar17
Tera Guru

Hi

U don't write client script

set the value of Dependent Field

state field is Dependent on country field

and

city is Dependent on state field

thanq

If my response helped please mark it correct and close the thread.

Thanks,
Rahul Kumar

Hi rahul ,I think it will work only for one field value,but what i nedd is If i choose India form Country field then in state field it should show only 2 states of india and same in city it should show 4 cities in city field(like Karnataka state has two cities banglore and mysore)

Rahul Kumar17
Tera Guru

Hi

yes u r write but state is dependent only country and city is dependent on state field that means dependent only one field.

If my response helped please mark it correct and close the thread.

Thanks,
Rahul Kumar