1

Please find below the Vf page and Apex class that I am using. I am fetching the rating value as URL parameter(/apex/FeedbackPage?rating=2) and storing it in a variable called rating. Its value can be either 1 or 2 or 3 or 4. Based on its value, I am trying to check the appropriate radio button(if rating is 4, Very Good needs to be checked; if rating is 3, Good needs to be checked and so on). I have flag variables defined for each rating and making the corresponding flat value to true if it gets matched with the rating value. But it is observed that the appropriate radio button is not checked with this code. I even tried using something like checked="{IF(flag1,'checked' ,'')}" to the input tag but it didnt work either. Please let me know if I am missing anything here.

<apex:page controller="FeedbackCtrl">
          <form>
              <input type="radio" name="rating" value="4" checked="{!flag4}">Very Good</input>
              <input type="radio" name="rating" value="3" checked="{!flag3}">Good</input>
              <input type="radio" name="rating" value="2" checked="{!flag2}">Bad</input>
              <input type="radio" name="rating" value="1" checked="{!flag1}">Very Bad</input>
          </form>
</apex:page>

Class:

public with sharing class FeedbackCtrl 
{
    public String rating {get;set;}
    public Boolean flag1 {set;get;}
    public Boolean flag2 {set;get;}
    public Boolean flag3 {set;get;}
    public Boolean flag4 {set;get;}
    public FeedbackCtrl()
    {
        rating = ApexPages.currentPage().getParameters().get('rating');
        if(rating.equals('1') )
        {
            flag1=true;
        }
        else if(rating.equals('2') )
        {
            flag2=true;
        } 
        else if(rating.equals('3') )
        {
            flag3=true;
        } 
        else if(rating.equals('4') )
        {
            flag4=true;
        }
    }
}

enter image description here Thanks for your time.

2 Answers 2

0

Because of the way HTML works, and the limitations of Visualforce, you're forced to go "the long way around":

<apex:page controller="FeedbackCtrl">
          <form>
              <apex:outputText rendered="{!flag4}">
                  <input type="radio" id="rating4" name="rating" value="4" checked="checked" />
              </apex:outputText>
              <apex:outputText rendered="{!!flag4}">
                  <input type="radio" id="rating4" name="rating" value="4" />
              </apex:outputText>
                  <label for="rating4">Very Good</label>

              <apex:outputText rendered="{!flag3}">
                  <input type="radio" id="rating3" name="rating" value="3" checked="checked" />
              </apex:outputText>
              <apex:outputText rendered="{!!flag3}">
                  <input type="radio" id="rating3" name="rating" value="3" />
              </apex:outputText>
                  <label for="rating3">Good</label>

              <apex:outputText rendered="{!flag2}">
                  <input type="radio" id="rating2" name="rating" value="2" checked="checked" />
              </apex:outputText>
              <apex:outputText rendered="{!!flag2}">
                  <input type="radio" id="rating2" name="rating" value="2" />
              </apex:outputText>
                  <label for="rating2">Bad</label>

              <apex:outputText rendered="{!flag1}">
                  <input type="radio" id="rating1" name="rating" value="1" checked="checked" />
              </apex:outputText>
              <apex:outputText rendered="{!!flag1}">
                  <input type="radio" id="rating1" name="rating" value="1" />
              </apex:outputText>
                  <label for="rating1">Very Bad</label>
          </form>
</apex:page>

As a side note, do not use "equals", because it'll crash your page if rating is null. You can simply write this instead:

public class FeedbackCtrl {
 public String rating {get;set;}
    public Boolean flag1 {set;get;}
    public Boolean flag2 {set;get;}
    public Boolean flag3 {set;get;}
    public Boolean flag4 {set;get;}
    public FeedbackCtrl()
    {
        rating = ApexPages.currentPage().getParameters().get('rating');
        flag1 = rating == '1';
        flag2 = rating == '2';
        flag3 = rating == '3';
        flag4 = rating == '4';
    }
}
2

You should use apex:selectRadio for your use case as below

<!-- Page: -->
<apex:page controller="FeedbackCtrl">
    <apex:form>
        <apex:selectRadio value="{!rating}">
            <apex:selectOptions value="{!items}"/>
         </apex:selectRadio><p/>

     </apex:form>

</apex:page>

Controller

Here you will create proper key-value pair for SelectOption.

public class FeedbackCtrl {
    String rating = null;

    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>(); 
        options.add(new SelectOption('4','Very Good')); 
        options.add(new SelectOption('3','Good')); 
        options.add(new SelectOption('2','Bad'));
        options.add(new SelectOption('1','Very Bad'));
        return options; 
    }

    public String getRating() {
        return rating ;
    }

    public void setRating(String rating) { this.rating = rating ; }


    public FeedbackCtrl()
    {
        rating = ApexPages.currentPage().getParameters().get('rating');
    }
}

Output

rating

Update based on comments

You dont need input radio here.

Create 4 transparent image png files. Put that in a table with 4 columns and single row and based on the rating, just change the background of the cell with yellow.

3
  • Hi Santanu, thanks for your response. However, apex:selectRadio is not used for some reasons. Please let me know if it is possible with input type="radio".
    – Suhasini
    Jun 20, 2018 at 14:02
  • what is the reason you are not using selectOption? Jun 20, 2018 at 14:06
  • I updated my question with an image. For capturing the selected response in that fashion we went with input type="radio".
    – Suhasini
    Jun 20, 2018 at 14:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .