Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF link in SelectItem label

Is it possible to set a <a href />around my <f:selectItem itemLabel="label" /> where my link text is the itemLabel?

I'm using the plain sun components.

like image 981
onigunn Avatar asked Sep 06 '25 03:09

onigunn


1 Answers

The desired result is not possible in HTML. You'll need to add a shot of JavaScript for this.

<h:selectOneMenu onchange="window.location=this.options[this.selectedIndex].value">
    <f:selectItems value="#{bean.links}" />
<h:selectOneMenu>

Where bean.getLinks() returns a List<SelectItem> with a fullworthy URL as item value. If you want to show the link as both value and label, just use the SelectItem constructor taking a single argument.

links = new List<SelectItem>();
links.add(new SelectItem("http://google.com"));
links.add(new SelectItem("http://stackoverflow.com"));
// ...

If you want to hardcode them in the view, then you can of course grab f:selectItem:

<h:selectOneMenu onchange="window.location=this.options[this.selectedIndex].value">
    <f:selectItem itemValue="http://google.com" />
    <f:selectItem itemValue="http://stackoverflow.com" />
<h:selectOneMenu>
like image 186
BalusC Avatar answered Sep 08 '25 01:09

BalusC