How to get index of selected item of jsf f:selectItems? - jsf

I have seleconeradio, for example:
<h:selectOneRadio value="#{myBean.selectedValue}" layout="pageDirection">
<f:selectItems value="#{myBean.myList}" var="a" itemValue="#{a}" itemLabel="#{a}"/>
</h:selectOneRadio>
where myList is list of integers, e.g. 1,3,2,4.
If user selects second element (i.e. 3) I want in myBean selectedValue to be 2, so I want to get index of selectItems item.
What should I write in f:selectItems itemValue tag? Or it is impossible?
P.S. I can do it by creating a new class in which I have the index property and create a new list of that class, giving the right index. But it is very bad solution.

You can actually use c:forEach for this case. This is especially usefull when you have to deal with a collection containing duplicates and therefore can't use indexOf() for example.
<h:selectOneRadio value="#{myBean.selectedValue}" layout="pageDirection">
<c:forEach items="#{myBean.myList}" var="a" varStatus="idx">
<f:selectItem itemValue="#{idx.index}" itemLabel="#{a}"/>
</c:forEach>
</h:selectOneRadio>
Just be sure to include the JSP JSTL Core namespace if you haven't done yet.
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core

you should use indexOf(Object o) .. it returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element...
your code should probably look like this..
int index = myList.indexof(selectedValue);

Related

How to get value from Map<TestClass,Object> in item label

Below is the jsf code
<h:selectOneMenu>
<f:selectItems value="#{bean.mapObject}"var="entity"
itemValue="#{entity.key}" itemLabel="#{entity.value.code}"/>
</h:selectOneMenu>
Backing bean has map defined as
private Map<TestClass, Object> mapObject;
TestClass has two variables and getters/setters
String code;
String name;
Issue I am having is : On JSF, on Selectonemenu, for itemLabel i would like to display the TestClass String value i.e code should be displayed.
Presently on SelectOneMenu on UI, I am getting
TestClass[code=t,name=anu] on the drop down, instead i want code value i..e 't'
Please let me know how to acheive this.
The <f:selectItems var> should in case of maps specify the Map.Entry. The <f:selectItems value> should therefore specify Collection<Map.Entry>. This is not natively recognized yet when you just specify a Map (will come in JSF 2.3).
You basically need to explicitly set the value to Map#entrySet() as below in case you intend to use map key as item value and ma:
<h:selectOneMenu>
<f:selectItems value="#{bean.mapObject.entrySet()}"var="entry"
itemValue="#{...}" itemLabel="#{...}" />
</h:selectOneMenu>
The Map.Entry object has getKey() and getValue() methods which return respectively the map entry's key and map entry's value, which are in your case respectively TestClass and Object. You seem to want to display the code property of TestClass as item label. So, this should do:
<h:selectOneMenu>
<f:selectItems value="#{bean.mapObject.entrySet()}"var="entry"
itemValue="#{...}" itemLabel="#{entry.key.code}" />
</h:selectOneMenu>
You're not terribly clear on what exactly you'd like to use as item value, so I've left it open.
See also:
Our selectOneMenu wiki page
You need to change the itemlabel attribute, (with current value entity.value.code). It should be replaced by entity.key.code ?
Also the value needs to call the method .entrySet , because the map in itself is not a collection.
<h:selectOneMenu>
<f:selectItems value="#{bean.mapObject.entrySet()}" var="entry"
itemValue="#{entry.key}" itemLabel="#{entry.key.code}"/>
</h:selectOneMenu>
Alternatively, since it's the key you want, you could iterate the keys immediately.
<h:selectOneMenu>
<f:selectItems value="#{bean.mapObject.keySet()}" var="key"
itemValue="#{key}" itemLabel="#{key.code}"/>
</h:selectOneMenu>

How can I add attributes to components which don't have their own renderers using the f:attribute component?

I want to write a custom renderer for the h:selectOneMenu component and eventually make use of the description property of the UISelectItem class to add a title a.k.a. tooltip to f:selectItems following BalusC's profound guides in https://stackoverflow.com/a/25512124/3280015 and http://balusc.blogspot.de/2008/08/styling-options-in-hselectonemenu.html.
Now I did extend the com.sun.faces.renderkit.html_basic.MenuRenderer in my own CustomMenuRenderer, registered it with the faces-config.xml and overrode the renderOption method, adding the following code before option tag is terminated by the Responsewriter:
String titleAttributeValue = (String) component.getAttributes().get("title");
if (titleAttributeValue != null) {
String indexKey = component.getClientId(context)
+ "_currentOptionIndex";
Integer index = (Integer) component.getAttributes().get(indexKey);
if (index == null) {
index = 0;
}
component.getAttributes().put(indexKey, ++index);
}
I'm not quite sure I'm doing the indexKey thing right or whether I need it for the title attribute or should use a writer.writeAttribute("title", titleAttributeValue, null); instead because I don't have a list like in the optionClasses tutorial, but the code works so far!
In the actual view definition use case I did:
<h:selectOneMenu value="#{cc.data}">
<f:attribute name="title" value="outerTEST" />
<c:forEach items="#{cc.list}" var="val">
<f:selectItem value="#{val}" itemValue="#{val}" itemLabel="#{val.label}">
<f:attribute name="title" value="innerTEST #{val.description}" />
</f:selectItem>
</c:forEach>
</h:selectOneMenu>
(I just put the #{val.description} there in the title value to clarify my intention, it is currently still empty and I will have to think about how to populate it per element later, but for the sake of the question we can assume it is already filled.)
But now I'm getting the "outerTEST" properly showing up in the title attribute of the option in the resulting XHTML in the Browser, yet I'm not seeing any of the "innerTEST" which would and should be individual per selectItem and which is what this is eventually all about.
I understand the f:selectItem and f:selectItemscomponents do not have their own renderers but rendering of options is generally handled by the MenuRenderer via its renderOption method.
But how then would I add individual titles to the individual selectItems??
Thanks

can't use the "end" property of JSF 2.0 repeat tag's varStatus

I'm using the repeat tag of JSF 2.0 to loop through a list of objects and display some of their properties. I want to use the varStatus attribute of repeat so that I can access the loop index, the number of the last list item, and to tell whether the end of the list has been reached (so the spacer won't be displayed). I thought this would work:
<ui:repeat var="anObject" varStatus="repeatStatus" value="#{objectList}">
<h:panelGroup>
<h:outputText value="Item #{repeatStatus.index + 1} of #{repeatStatus.end}" />
<h:outputText value="#{anObject.text}" />
</h:panelGroup>
<h:outputText value=" " rendered="#{false == repeatStatus.last}" />
</ui:repeat>
However, it never displays anything for repeatStatus.end. The index and last properties work well.
Instead of repeatStatus.end, I tried using objectList.size(), but that worked for only the first item in the list.
How can I display the number of items in the list as part of the "Item x of y" text?
The end is only used when you set the size attribute.
<ui:repeat ... size="#{fn:length(objectList)}">
Alternatively, you can also just use it directly.
Item #{repeatStatus.index + 1} of #{fn:length(objectList)}
By the way, the boolean comparison in #{false == repeatStatus.last} is ugly. It returns a boolean already; if you want to negate it, rather use #{not repeatStatus.last}.

How to retrieve and show list of values in listbox and text corresponding to list item in textarea

I need to use a list box to show some values from database and do further processing when a single value from the list is selected.
At the PrimeFaces showcase site the example loads fixed (static) data into the listbox and there is one PrimeFaces command for each list item. How do I show items in a list box dynamically, when I may not know the number of items beforehand?
I also need to show some text corresponding to the item selected in list, in a textarea. Do I have to use an event listener for this purpose? I would like to leave the text area blank at the beginning. Only when a value is selected in the list box, then I want to use a bean to retrieve and store data using that textarea. Is this possible? How do I implement this?
How do I show items in a list box dynamically, when I may not know the number of items beforehand?
Use <f:selectItems> which you bind to a List<T> property. Basic example, assuming you're using EJB/JPA to interact with DB:
private Item selectedItem; // +getter+setter
private List<Item> availableItems; // +getter
#EJB
private ItemService service;
#PostConstruct
public void init() {
availableItems = service.list();
}
with
<p:selectOneListbox value="#{bean.selectedItem}" converter="itemConverter">
<f:selectItems value="#{bean.availableItems}" var="item"
itemValue="#{item}" itemLabel="#{item.someLabel}" />
</p:selectOneListbox>
The itemConverter should implement javax.faces.convert.Converter and convert from the Item object to its unique string representation (usually its DB identifier) in getAsString() and convert the other way round getAsObject().
I also need to show some text corresponding to the item selected in list, in a textarea. Do I have to use an event listener for this purpose?
Just put a <p:ajax> (the PrimeFaces equivalent of standard JSF <f:ajax>) in the listbox which updates the textarea. E.g.
<p:selectOneListbox value="#{bean.selectedItem}" converter="itemConverter">
<f:selectItems value="#{bean.availableItems}" var="item"
itemValue="#{item}" itemLabel="#{item.someLabel}" />
<p:ajax update="textarea" />
</p:selectOneListbox>
<p:inputTextarea id="textarea" value="#{bean.selectedItem.someText}" />
It'll be invoked when you select an item.
See also:
Our h:selectOneMenu wiki page - same applies to PrimeFaces p:selectOneListbox
Yes, for demonstration purposes most of the examples are loaded with static data. But if you look at the same example on PF showcase, the second listbox code is as follows:
<h:outputText value="Scrollbar: " />
<p:selectOneListbox id="scroll" value="#{autoCompleteBean.selectedPlayer1}"
converter="player" style="height:100px">
<f:selectItems value="#{autoCompleteBean.players}"
var="player" itemLabel="#{player.name}" itemValue="#{player}" />
</p:selectOneListbox>
and f:selectItems value attribute can point to a collection, an array, a map or a SelectItem instance. So coming to the above example players could be any list that is being populated using a database in the managed bean.
But if the instance is not a SelectItem, the labels are obtained by calling a toString on each object and finally the selected itemValue is set to the selectedPlayer1 attribute but you can also see that there is a converter in between so the incoming itemValue string is converted back to a player object and then set to selectedPlayer1.
And if you want to display the selected item in a text area, you can do something like this:
<h:outputText value="Scrollbar: " />
<p:selectOneListbox id="scroll" value="#{autoCompleteBean.selectedPlayer1}"
converter="player" style="height:100px">
<f:selectItems value="#{autoCompleteBean.players}"
var="player" itemLabel="#{player.name}" itemValue="#{player}" />
<p:ajax update="displayArea"/>
</p:selectOneListbox>
<p:inputTextarea id="displayArea" value="#{autoCompleteBean.selectedPlayer1}" />
Here the inputTextarea is updated using ajax with the value selected by the user.

How to dynamically create a <f:selectItem> list?

Is there a way to dynamically create a selectItem list? I dont really want to have to create lots of bean code to make my lists return List<SelectItem>.
I tried this:
<ice:selectManyCheckbox>
<ui:repeat var="product" value="#{productListingService.list}">
<f:selectItem itemLabel="#{product.description}" value="#{product.id}"/>
</ui:repeat>
</ice:selectManyCheckbox>
but it doesnt work.
Any ideas?
Use <f:selectItems> instead. It accepts next to List<SelectItem> and SelectItem[] also a Map<String, Object> as value where the map key is the item label and map value is the item value. Or if you're already on JSF 2.0, then you can use a List<SomeBean> instead where the current item can be referenced by the var attribute.
<f:selectItems value="#{productListingService.list}" var="product"
itemLabel="#{product.description}" itemValue="#{product.id}" />
See also:
Our <h:selectOneMenu> wiki page

Resources