Hi,
Myabe this is a recurrent issue, but just to serve as a conclusion...
>From the posts I've found regarding the use of checkboxes inside a
DataTable, the most widely used approach would be to include a boolean
property in the JavaBean (eg. selected) that populates the DataTable,
so when anyone selects several checkboxes and clicks on a typical
button called "Delete selected", the following code gets executed:
public String deleteSelected()
{
// Iterate through the data rows ...
int first = table.getFirst();
int rows = table.getRows();
for (int index = first; index < (first + rows); index++) {
// Position the table at the current row ...
table.setRowIndex(index);
// And get the message bean for this row ...
MyJavaBean javaBean = (MyJavaBean)
FacesContext.getCurrentInstance().getRequestMap().get("javaBean");
// If this row is selected, delete the corresponding message
if (javaBean.isSelected()) {
...
}
}
table.setRowIndex(-1); // We're done now
return (null); // Redisplay the current page
}
IMHO, this mixes presentation logic into your domain model, so it
shouldn't be recommended.
So, I was wondering whether there is a cleaner way to achieve this
needed behaviour. I remember when using Struts that I could give the
checkbox a value, which was the ID of my JavaBean:
<form>
<table>
<tr>
<td><input type="checkbox" id="chkItems" value="${myBean.id}"/></td>
<td>MyBean 1</td>
</tr>
<tr>
<td><input type="checkbox" id="chkItems" value="${myBean.id}"/></td>
<td>MyBean 2</td>
</tr>
<tr>
<td><input type="checkbox" id="chkItems" value="${myBean.id}"/></td>
<td>MyBean 2</td>
</tr>
</table>
<input type="submit" value="Delete Selected" />
So when I submitted the form, I could get through the request the
String[] parameter named "chkItems" where I had all the information I
needed to delete my objects...
I think there could be two nice solutions to this problem using MyFaces:
1) Extend the DataTable component to have a new attribute that would
render the checkbox and a new method to return all the checkboxes
selected
2) Create a new checkbox component that allow to bind a value distintc
to a boolean (or maybe just extend the selectBooleanCheckBox)
What dou you think? Are there any other good alternatives?