1

I am trying to add a reset/clear button to a selection list. I have tried but can't figure out how to clear the user selections.

VF Code:

 <div id="nav">
            <apex:pageblock >
                <apex:pageBlockSection >
                    <apex:outputLabel style="font-weight:bold;">States:</apex:outputLabel>                
                </apex:pageBlockSection>
                <apex:pageBlockSection >
                    <apex:selectList id="statelist" value="{!States}" multiselect="true" size="10" >
                        <apex:selectOptions value="{!items}"/>
                    </apex:selectList>
                </apex:pageBlockSection>
                <apex:pageBlockSection >
                    <apex:commandButton action="{!stateFilter}" rerender="thePage" title="Filter" value="Filter">
                        <apex:param name="AccRendered" assignTo="{!AccRendered}" value="FALSE"/>
                    </apex:commandButton>
                    <apex:commandButton rerender="statelist" title="Reset" value="Reset">
                        <apex:param name="AccRendered" assignTo="{!AccRendered}" value="FALSE"/>
                    </apex:commandButton>
                </apex:pageBlockSection>
            </apex:pageblock>
        </div>

Controller Code:

public class LicensingGridController {

    public Id selectedHubId {get; set;}
    public Boolean AccRendered {get; set;}
    public String stFilter {get; set;}
    public String bcsFilter {get; set;}
    list<BCS__c>hubs = new list<BCS__c>();
    String[] states = new String[]{};

        public LicensingGridController(){
            AccRendered     = FALSE;
        }

    public List<BCS__c> getTheHubs() {
        system.debug('stfilter = ' + stFilter);
        system.debug('states = '+ states);

        if(states.isEmpty()){
            hubs = [Select Id, Name, User__r.Name,User__r.SmallPhotoUrl,
                    (Select Id,Account__c,Account__r.Name,Hub_Name__r.Name
                     FROM Account_Counselors__r
                    ORDER BY Account__r.Name ASC),
                    (Select Name,Status__c,Status_Icon__c 
                     FROM Licenses__r
                    ORDER BY NAME ASC
                    )
                    FROM BCS__c
                    WHERE Inactive__C = FALSE
                    ORDER BY Name ASC];
            return hubs;
        } else {            
            hubs = [Select Id, Name, User__r.Name,User__r.SmallPhotoUrl,
                    (Select Id,Account__c,Account__r.Name,Hub_Name__r.Name
                     FROM Account_Counselors__r
                    ORDER BY Account__r.Name ASC),
                    (Select Name,Status__c,Status_Icon__c 
                     FROM Licenses__r
                     ORDER BY NAME ASC
                    )
                    FROM BCS__c
                    WHERE Inactive__C = FALSE AND 
                    id in (select Hub_Name__c from LIcense__c where Name IN :states)
                    ORDER BY Name ASC];
            return hubs;
        }

    }


public list<OE_Rollover_Spans__c> getOpenEnrollments(){

    List<string> whereList = new List<String>();

    system.debug('HubId = ' + selectedHubId);

    List<Account_Counselor__c> cAccounts = new list<Account_Counselor__c>(
        [Select Id,Account__c FROM Account_Counselor__c WHERE Hub_Name__c = :selectedHubId]);

    system.debug('cAccounts = ' + cAccounts);

    for(Account_Counselor__c cc:cAccounts ){
        whereList.add(cc.Account__c );
    }

    system.debug('wherelist = '+ whereList);

    List<OE_Rollover_Spans__c> OEs = new list<OE_Rollover_Spans__c>(
        [Select Account__c,Account__r.Name,Name,Type__c,OE_Type__c,Start_Date__c,End_Date__c
         FROM OE_Rollover_Spans__c
         WHERE Account__c IN :whereList
        ORDER BY Account__c ASC]
    );

    return OEs;
}

public PageReference fetchOEs(){
    AccRendered = TRUE;
    return null;
}

public PageReference stateFilter(){
    return null;
}

public PageReference bcsFilter(){
    return null;
}

public List<SelectOption> getItems() {

    List<SelectOption> options = new List<SelectOption>();
    for(AggregateResult l : [SELECT Name FROM License__c GROUP BY Name]){ 
        options.add(new SelectOption(String.valueOf(l.get('Name')),String.valueOf(l.get('Name'))));
    }
    return options;


}

public String[] getStates() {
    return states;
}

public void setStates(String[] states) {
    this.states = states;
}

}
  • For the most accurate assistance, you should post the relevant controller code as well – Antonio Manente Jun 9 '16 at 21:11
  • 1
    Controller code added. – Matt M Jun 9 '16 at 21:18
1

The values for your selections are held in this variable:

String[] states = new String[]{};

First, you need to declare states as public.

Then, you can create a method that resets or clears the string states simply be creating a button that calls the method that would look something like this:

public string[] setstates(){

    states.clear();
    set states; 

} 

If you make the method void and use a {set,get} property on states, you don't really need to return anything and could name the method anything you wanted to. You also wouldn't need to use the setter, making it optional.

  • I already have that method at the bottom of my controller code. I am not sure how to integrate what you are showing me with that. – Matt M Jun 10 '16 at 16:13
1

You will use command button action method which will call actionFunction and in tern it will call controller method. In the controller method you can reset the fields. you could reRender properly to see the reset data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.