You need to sign in to do that
Don't have an account?

Apex : how to remove selected items from selectlist ?
Hi,
For adding the item in a selectlist we do the following
for(String f : FieldLabels)
{
selectedFieldsList.add(new selectOption(f,f));
}
and this works perfect.
Same way how to remove from the select list ? I dtried with the following things and got the errors
E.g : 1 : for(String f : selectedFields)
{
selectedFieldsList.remove(f);
}
ERROR : Method does not exist or incorrect signature: [LIST<System.SelectOption>].remove(String)
for(integer i=0;i<selectedFields.Size();i++)
{
selectedFieldsList.remove(selectedFields.Size());
}
ERROR : LIST INDEX OUT OF BONDS
for(String f : selectedFields)
{
selectedFieldsList.remove(0);
}
here the items are deleted but , if i select 5 random items to delete, it is removing first 5 items not the selected one.
For adding the item in a selectlist we do the following
for(String f : FieldLabels)
{
selectedFieldsList.add(new selectOption(f,f));
}
and this works perfect.
Same way how to remove from the select list ? I dtried with the following things and got the errors
E.g : 1 : for(String f : selectedFields)
{
selectedFieldsList.remove(f);
}
ERROR : Method does not exist or incorrect signature: [LIST<System.SelectOption>].remove(String)
for(integer i=0;i<selectedFields.Size();i++)
{
selectedFieldsList.remove(selectedFields.Size());
}
ERROR : LIST INDEX OUT OF BONDS
for(String f : selectedFields)
{
selectedFieldsList.remove(0);
}
here the items are deleted but , if i select 5 random items to delete, it is removing first 5 items not the selected one.
If you know the index of the value, you like to delete, then the below one works
for(integer i=0;i<selectedFields.Size();i++)
{
if(i == indexToBeremoved)
selectedFieldsList.remove(i);
}
If you just know the value to be removed and not sure about it's index, then you can try the below.
Suppose, you have the element to be removed from the Select Options in a string variable named "selectedValue"
for(integer i=0;i<selectedFields.Size();i++)
{
if(selectedFields[i].getValue() == selectedValue)
selectedFieldsList.remove(i);
}
Hope this helps..!
Regards,
- Harsha
Plz provide me full code of controller..
where i need to add these lines of codes..
Regards,
Nansi