0

I'm trying to get a selectList to reset to None after clicking a CommandButton. Right now after I save the button, it just stays on the last value picked. I want to keep the values that were generated from the class, just reset it to none. I've tried using rerender on as many places around the selectList that I can think of but it's not working.

VF Page:

<apex:page controller="wrapperClassController" showHeader="false" lightningStylesheets="true" standardStylesheets="true">
<script>
    function changeValue(input, textid) {
        document.getElementById(textid).value = input.value;
    }   

    function show_alert()
    {
    var msg = "Your survey has been saved.";
    alert(msg);
    }
</script>
<apex:form >
    <apex:outputPanel id="MBTRList">
        <apex:pageBlock rendered="{!hideStuff}">
            <apex:pageBlockButtons >
                <apex:commandButton value="Build Form" action="{!callTwoMethods}" rerender="MBTRList,Form,formButton" rendered="{!hideStuff}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!contacts}" var="c" id="table">
                <apex:column >
                    <apex:inputCheckbox value="{!c.selected}"/>
                </apex:column>
                <apex:column value="{!c.con.Name}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:outputPanel>
    <apex:outputPanel id="formButton">
        <apex:pageBlock rendered="{!showStuff}">   
            <apex:commandButton value="Save Form" action="{!saveList}" onclick="show_alert()" rerender="mainForm,selectList,Form,selectListBlock"/>
        </apex:pageBlock>
    </apex:outputPanel>
    <apex:outputPanel id="Form">
        <apex:pageBlock rendered="{!showStuff}">  
           <apex:pageBlockSection id="selectListBlock">
               <apex:selectList label="Please select your name: " value="{!inputText3}" size="1" multiselect="false" required="true">
                   <apex:selectOptions id="selectList" value="{!List}"></apex:selectOptions>
                   <apex:actionSupport event="onchange" action="{!showMainPanel}" reRender="mainForm,Form"/>
                </apex:selectList>
            </apex:pageBlockSection>
            </apex:pageBlock>
    </apex:outputPanel>
    <apex:outputPanel id="mainForm">
        <apex:outputPanel rendered="{!showMain}">
    <table>
        <tr>
    <td style="font-weight: bold;" colspan="10">
    * 1. Pain Scale
    </td>
</tr>
<tr>
    <td></td>
    <td style="text-align:center; vertical-align:bottom; width:9%;">No Pain</td>
    <td style="text-align:center; vertical-align:bottom; width:9%;">1</td>
    <td style="text-align:center; vertical-align:bottom; width:9%;">2</td>
    <td style="text-align:center; vertical-align:bottom; width:9%;">3</td>
    <td style="text-align:center; vertical-align:bottom; width:9%;">4</td>
    <td style="text-align:center; vertical-align:bottom; width:9%;">    Interrupts some<br/>activities  </td>
    <td style="text-align:center; vertical-align:bottom; width:9%;">6</td>
    <td style="text-align:center; vertical-align:bottom; width:9%;">7</td>
    <td style="text-align:center; vertical-align:bottom; width:9%;">8</td>
    <td style="text-align:center; vertical-align:bottom; width:9%;">9</td>
    <td style="text-align:center; vertical-align:bottom; width:9%;">As bad as it could be</td>
</tr>
<tr  style="background-color:#eeeeee">
    <td style="text-align:center;">Before session
    <apex:inputHidden value="{!painBefore}" id="painBefore" /></td>

    <td style="text-align:center;"><input type="radio" onclick="changeValue(this,'{!$Component.painBefore}');" name="Pain Scale Before" id="Pain Scale Before" value="No Pain"/></td>
    <td style="text-align:center;"><input type="radio" onclick="changeValue(this,'{!$Component.painBefore}');" name="Pain Scale Before" id="Pain Scale1 Before" value="1"/></td>
    <td style="text-align:center;"><input type="radio" onclick="changeValue(this,'{!$Component.painBefore}');" name="Pain Scale Before" id="Pain Scale2 Before" value="2"/></td>
    <td style="text-align:center;"><input type="radio" onclick="changeValue(this,'{!$Component.painBefore}');" name="Pain Scale Before" id="Pain Scale3 Before" value="3"/></td>
    <td style="text-align:center;"><input type="radio" onclick="changeValue(this,'{!$Component.painBefore}');" name="Pain Scale Before" id="Pain Scale4 Before" value="4"/></td>
    <td style="text-align:center;"><input type="radio" onclick="changeValue(this,'{!$Component.painBefore}');" name="Pain Scale Before" id="Pain Scale5 Before" value="Interrupts some activities"/></td>
    <td style="text-align:center;"><input type="radio" onclick="changeValue(this,'{!$Component.painBefore}');" name="Pain Scale Before" id="Pain Scale6 Before" value="6"/></td>
    <td style="text-align:center;"><input type="radio" onclick="changeValue(this,'{!$Component.painBefore}');" name="Pain Scale Before" id="Pain Scale7 Before" value="7"/></td>
    <td style="text-align:center;"><input type="radio" onclick="changeValue(this,'{!$Component.painBefore}');" name="Pain Scale Before" id="Pain Scale8 Before" value="8"/></td>
    <td style="text-align:center;"><input type="radio" onclick="changeValue(this,'{!$Component.painBefore}');" name="Pain Scale Before" id="Pain Scale9 Before" value="9"/></td>
    <td style="text-align:center;"><input type="radio" onclick="changeValue(this,'{!$Component.painBefore}');" name="Pain Scale Before" id="Pain Scale10 Before" value="As bad as it could be"/></td>
</tr>
</table>
        </apex:outputPanel>
        </apex:outputPanel>

    </apex:form>

Controller:

public class wrapperClassController {

public List<cContact> contactList {get; set;}
public Boolean showStuff { get; set; }
public Boolean showMain { get; set; }
public Boolean hideStuff { get; set; }
public String inputText1{get;set;}
public String painBefore{get;set;}
public Id inputText3{get;set;}

public wrapperClassController(){
    showStuff = false;
    showMain = false;
    hideStuff = true;
}

public PageReference callTwoMethods() {

      toggleContent();
      processSelected();

      return null;

}

public PageReference toggleContent() {
        showStuff = true;
        hideStuff = false;
    return null;
}

public PageReference showMainPanel() {
        showMain = true;
    return null;
}

public List<cContact> getContacts(){
    if(contactList == null){
        contactList = new List<cContact>();
        for(Contact c:[select Id, Name from Contact 
                       WHERE RecordTypeId = '012f10000000LFN']){
                           contactList.add(new cContact(c));
                       }
    }
    return contactList;
}

public PageReference processSelected(){
    List<Contact> selectedContacts = new List<Contact>();
    for(cContact cCon:getContacts()){
        if(cCon.selected == true){
            selectedContacts.add(cCon.con);
        }
    }
    return null;
}

public List<selectOption> getList(){
    List<selectOption> selectedContacts = new List<selectOption>();
    selectedContacts.add(new selectOption('', '--None--'));
    for(cContact cCon:getContacts()){
        if(cCon.selected == true){
            selectedContacts.add(new selectOption(cCon.con.Id,cCon.con.Name));
        }
    }
    return selectedContacts;
}

public PageReference saveList(){
    MBTR_Forms_Docs_Evals__c MBTR = new MBTR_Forms_Docs_Evals__c(); //  Either salesObj is standard or custom object

    MBTR.Participant_s_initials__c = inputText1;  // Field Assignment
    MBTR.Pain_Scale_Before__c = painBefore;
    MBTR.Contact__c = inputText3;
    MBTR.RecordTypeId = '012f10000000L9K';
    insert MBTR; // DML Operations
    showMain = false;
    return null;

} 

}
1

If you want to rest the value of SelectList. You need to clear the old value. Only rerender will not work here. Unless you do so it won't get rest to None.

So add this line in your saveList method.

public PageReference saveList(){
    //other code will remain here
    inputText3 = '';
}
0

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.