views:

33

answers:

1

I develop something like a shopping cart. I can select multiple items (by clicking on the checkbox next to that item) and add them to a list. How can I know which checkbox I am selecting. If I do this

<h:selectBooleanCheckbox value="#{document.checked}"/>

it only tell me a state of a checkbox is selected or not, but does not tell me which one. Remember the number of items is unknown until runtime since I load them from a datasource, meaning I dont know how many check box there are.

+3  A: 

You should have a managed bean from which you get and set the booleans for the different checkboxes (different fields with getter and setters for each checkbox value).

Update: take a look at this example, because I think that is what you should be doing. Where the example says "Employee", you take "Article" or whatever your cart items are.

http://www.javabeat.net/tips/43-how-to-use-hselectbooleancheckbox-within-hd.html

Zheileman
OK, that was not clear in the question.So, several checkboxes are generated at runtine. And there aren't fields in the managed bean to receive the selection?Perhaps you will need to save the selection into an input hidden and manage it by yourself, not by managed-properties.
Zheileman
Srry, I just update my post. Can u elaborate a bit on what u mean by saving my selection into an input hidden? Can u provide a bit of sample code?
Harry Pham
uhmmm, I see what going on here. So you shift the selection logic back to the Model, so you can take advantage of EL and managed bean.. uhmmm very smart :D I think it will definitely work. Thank you
Harry Pham