Suppose the code of this page:
<h:form prependId="false" id="form">
<h:selectManyCheckbox id="checkBoxList" value="#{backedBean.lstIdSelectedItems}" layout="pageDirection">
<f:selectItems value="#{backedBean.lstAvailableItems}" var="item" itemLabel="#{item.label}" itemValue="#{item.value}" />
<f:ajax listener="#{backedBean.itemClicked}" />
</h:selectManyCheckbox>
</h:form>
And the code of a session managed bean:
public class BackedBean implements Serializable {
private List<SelectItem> lstAvailableItems;
private List<Long> lstIdSelectedItems;
public BackedBean() {
lstAvailableItems = new ArrayList<SelectItem>();
lstIdSelectedItems = new ArrayList<Long>();
}
@PostConstruct
private void postConstruct(){
for (int i = 0; i < 10; i++) {
SelectItem item = new SelectItem(new Long(i), "CHKID " + i);
lstAvailableItems.add(item);
}
}
public void itemClicked(AjaxBehaviorEvent ae){
HtmlSelectManyCheckbox uiCmp = (HtmlSelectManyCheckbox)ae.getSource();
// (1) Here I would like to get the ID of the item that has been clicked.
}
In (1) I would like to get the ID of the element that has been clicked by the user. I can see in the lstIdSelectedItems array list the IDs of all elements selected by the user, but how can I get the ID of the element that the user has clicked?
I have tried to use the f:attribute tag inside of the selectManyCheckbox, but the attribute is not in the component map when the ajax listener method is called in the backed bean. I have used this, but doesn't work:
<h:selectManyCheckbox id="checkBoxList" value="#{backedBean.lstIdSelectedItems}" layout="pageDirection">
<f:selectItems value="#{backedBean.lstAvailableItems}" var="item" itemLabel="#{item.label}" itemValue="#{item.value}">
<f:attribute name="clicked" value="#{item.value}" />
</f:selectItems>
<f:ajax listener="#{backedBean.itemClicked}" />
</h:selectManyCheckbox>
Any ideas?
Regards.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…