Skip to main content
karimulla syed (Insight Global) ha fatto una domanda in #Apex
how to work with , remove and remove all functionality in the salesforce list collection with below code.

 

public class selectoptiontest {

public set<string>citylist {get;set;}

public set<selectoption>NotselectedOptions{get;set;}

public list<string>selectedvallist{get;set;}

public selectoptiontest(){

NotselectedOptions= new set<selectoption>();

selectedvallist= new list<string>();

citylist= new set<string>();

citylist.add('mumbai');

citylist.add('maharastra');

citylist.add('uttarpradesh');

citylist.add('mumbai');

getdata();

}

public void getdata(){

for(string s:citylist){

NotselectedOptions.add(new SelectOption(s,s));

}

}

public void removeselected(){

citylist.removeall(selectedvallist);

}

public void removeall(){

selectedvallist.clear();

for(string s:citylist){

selectedvallist.add(s);

}

citylist.removeall(selectedvallist);

}

}

v.f page:

 

<apex:page controller="selectoptiontest" >

<apex:form >

<apex:outputPanel id="t">

<apex:selectList id="Avilblecities" multiselect="true" tabindex="1" value="{!selectedvallist}" >

<apex:selectOptions value="{!NotselectedOptions}"/>

</apex:selectList>

</apex:outputPanel>

<apex:commandButton action="{!removeselected}" rerender="t" value="removeselected"/>

<apex:commandButton action="{!removeall}" rerender="t" value="removeAll"/>

</apex:form>

</apex:page>

can any one help it with it please.

thanks and regards

franklin.
1 risposta
  1. 22 dic 2020, 13:28
    Hi Franklin,

    >>  https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_list.htm

    In the documentation you can use the below functions for your purpose remove to delete a element at a particular index and clear function to remove all the elements in the list.

    remove(index)

    Removes the list element stored at the specified index, returning the element that was removed.

    Signature

    public Object remove(Integer index)

    Parameters

    index

    Type: Integer

    Return Value

    Type: Object

    Example

    List<String> colors = new String[3];

    colors[0] = 'Red';

    colors[1] = 'Blue';

    colors[2] = 'Green';

    String s1 = colors.remove(2);

    system.assertEquals('Green', s1);

    clear():

    Removes all elements from a list, consequently setting the list's length to zero.

    Signature

    public Void clear()

    Return Value

    Type: Void

    EDIT: changed the clear description.

    Additionally, you can also try checking https://salesforce.stackexchange.com/questions/65670/best-way-to-remove-element-from-list-or-why-they-kept-iterator-without-this-opti

    Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

    Thanks.
0/9000