1

Expecting only checkboxes for multiselect type questions and radio buttons for singleselect type questions, but here both checkboxes and radiobuttons are displaying for multiselect type questions - how can these be rendered

Am unable to render the multiselect and singleselect in the vf page, suggest me to fix this.

code:

<apex:page standardController="Account" extensions="OnlineExAnswer" sidebar="false" showHeader="false">
    <apex:form id="frm" >
        <apex:pageBlock title="Questionare">
            <apex:pageBlockSection columns="1">
                <apex:repeat value="{!onWrprList }" var="qustwrpr">
                    <apex:outputField value="{!qustwrpr.que.Question_Des__c }" rendered="{!qustwrpr.que.Question_Type__c='Multi-Select'}"/> 
                    <!--  <apex:outputField value="{!qustwrpr.que.Answer_Option__c}"/> -->
                    <apex:selectCheckboxes value="{!qustwrpr.ansList}"> 
                        <apex:selectOptions value="{!qustwrpr.anss}"/> 
                    </apex:selectCheckboxes>
                   <!-- <apex:outputField value="{!qustwrpr.que.Question_Des__c }" rendered="{!qustwrpr.que.Question_Type__c='Single-Select'}"/>
                    <apex:selectRadio value="{!qustwrpr.ansList}">
                        <apex:selectOptions value="{!qustwrpr.anss}"/> 
                    </apex:selectRadio> -->

                </apex:repeat>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>     
</apex:page>

Apex:

public class OnlineExAnswer{
    public List<Question__c> questionList {get; set;}
    public List<OnlineWrapper> onWrprList {get; set;}
    public List <String> ansList {get; set;}
    private Account acc;
    public OnlineExAnswer(ApexPages.StandardController Controller){

        onWrprList=new List<OnlineWrapper>();
        acc= (Account) controller.getrecord();
        System.debug('::::::'+acc);
        Account acc1=[Select Id,MasterSetAcc__c from Account where Id=:acc.Id Limit 1]; // with this query we can get fields of Account.
        questionList= [Select Question_Des__c,Answer_Option__c, Question_Type__c  from Question__c where MasterSet1__c=:acc1.MasterSetAcc__c];
        for(Question__c que : questionList){

            OnlineWrapper owrpr = new OnlineWrapper();
            owrpr.que = que;
            if(que.Question_Type__c == 'Multi-Select'){
                system.debug('::::: answerOption String'+que.Answer_Option__c);
                List<string> ansList=que.Answer_Option__c.split(';');
                system.debug('########' + ansList);
                for(String ans:ansList){
                    owrpr.anss.add(new selectOption(ans,ans)); 
                }
                system.debug('@@@@@@@@@@@@'+ owrpr.anss);


                owrpr.isCheckBox= true;   
            }
            else if(que.Question_Type__c == 'Single-Select'){
                List<string> ansList=que.Answer_Option__c.split(';');
                for(String ans:ansList){
                    owrpr.anss.add(new selectOption(ans,ans)); 
                }
                system.debug('^^^^^^^^^^^^'+ owrpr.anss);
                owrpr.isRadiobutton= true;   
            }
            else{
                owrpr.isText= true;  
            }
            onWrprList.add(owrpr); 
        }

        system.debug('$$$$$$$$'+ questionList.size());
        system.debug('%%%%%%%%'+ onWrprList.size());

    }


    public class OnlineWrapper{
        public List<String> ansList {get; set;}
        public List<SelectOption> anss{get;set;}
        //  public List<String> selectedOptions{get;set;}
        public Question__c que {get; set;}
        public Boolean isCheckBox {get;set;}
        public Boolean isRadioButton {get;set;}
        //public String text{get;set;}
        public Boolean isText{get;set;}
        public OnlineWrapper(){
            ansList=new List<String>(); 
            anss=new List<SelectOption>();
            this.isCheckBox=false;
            isRadioButton=false;
            isText=false;
        }
    }
}
  • Please clarify what the expected behavior is and what you observe. – Adrian Larson Feb 7 '17 at 16:19
  • @Adrian Larson : I just wanted to display only checkboxes for multiselect and radiobuttons for singleselect. Unable to render, attached the screenshot. – Sweety4sfdc Feb 7 '17 at 16:25
  • 1
    @Sweety4sfdc please, add description to the screenshot. Is it expected behavior? Or it's what you would like to see? – EvAzi Feb 7 '17 at 16:26
  • Expecting only checkboxes for multiselect type questions and radio buttons for singleselect type questions, but here both checkboxes and radiobuttons are displaying for multiselect type questions - how can these be rendered – Sweety4sfdc Feb 7 '17 at 16:36
  • so all the above questions in the screenshot are multi select questions? – night crawler Feb 7 '17 at 17:02
1

The main issue is you are not rendering properly based on Question Type. Wrap the question type options inside outputPanel and display those conditionally.

<apex:repeat value="{!onWrprList }" var="qustwrpr">
    <apex:outputPanel rendered="{!qustwrpr.que.Question_Type__c='Multi-Select'}">
        <apex:outputField value="{!qustwrpr.que.Question_Des__c }"/> 
        <apex:selectCheckboxes value="{!qustwrpr.ansList}"> 
            <apex:selectOptions value="{!qustwrpr.anss}"/> 
        </apex:selectCheckboxes>
    </apex:outputPanel>
    <apex:outputPanel rendered="{!qustwrpr.que.Question_Type__c='Single-Select'}">
       <apex:outputField value="{!qustwrpr.que.Question_Des__c }"/>
        <apex:selectRadio value="{!qustwrpr.ansList}">
            <apex:selectOptions value="{!qustwrpr.anss}"/> 
        </apex:selectRadio>
    </apex:outputPanel>
</apex:repeat>

Which will finally display like this:

Questions

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.