msharma
msharma

Reputation: 575

Using h:outputFormat to message-format the f:selectItems of a h:selectOneRadio

I am having some trouble with using h:selectOneRadio. I have a list of objects which is being returned which needs to be displayed. I am trying something like this:

<h:selectOneRadio id="selectPlan" layout="pageDirection">
    <f:selectItems value="#{detailsHandler.planList}" />
</h:selectOneRadio>

and planList is a List of Plans. Plan is defined as:

public class Plan {
    protected String id;
    protected String partNumber;
    protected String shortName;
    protected String price;
    protected boolean isService;
    protected boolean isOption;  
    //With all getters/setters
}

The text that must appear for each radio button is actually in a properties file, and I need to insert params in the text to fill out some value in the bean. For example the text in my properties file is:

plan_price=The price of this plan is {0}.

I was hoping to do something like this:

<f:selectItems value="<h:outputFormat value="#{i18n.plan_price}">
    <f:param value="#{planHandler.price}">
</h:outputFormat>" />

Usually if it's not a h:selectOneRadio component, if it's just text I use the h:outputFormat along with f:param tags to display the messages in my .property file called i18n above, and insert a param which is in the backing bean. here this does not work. Does anyone have any ideas how I can deal with this?

I am being returned a list of Plans each with their own prices and the text to be displayed is held in property file. Any help much appreciated.

Thanks!


I am now able to resolve the above issue following the recommendation below. But now I have another question. Each radio button item must display like this:

Click **here**  to see what is included. The cost is XX.

Now the above is what is displayed for each radio button. The "here" needs to be a hyperlink which the user can click and should bring up a dialog box with more info.I can display the sentence above but how do I make the "here" clickable?

Since the above is what is displayed it is the label for SelectItem(Object value, String label) which is returned.

Any ideas much appreciated.

Upvotes: 0

Views: 1645

Answers (1)

Naganalf
Naganalf

Reputation: 1101

The value passed to <f:selectItems /> must be a list or array of type javax.faces.model.SelectItem.

You can set the output label in the constructor of the SelectItem. I imagine you can access your properties file from the backing bean. The method to get the SelectItems would look something like this:

public List<SelectItem> getPlanItems() {
  List<SelectItem> list = new ArrayList<SelectItem>();
  for (Plan plan : planList) {
    String label = buildPlanPriceLabel(plan.getPrice());
    list.add(new SelectItem(plan, label));
  }
  return list;
}

Leaving buildPlanPriceLabel as an exercise for the reader.

Upvotes: 1

Related Questions