You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Enrique Medina <e....@gmail.com> on 2005/06/05 12:12:12 UTC
DataTable & CheckBox
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?
Re: DataTable & CheckBox
Posted by Enrique Medina <e....@gmail.com>.
Still can't find any good solution? Any proposals?
2005/6/5, Enrique Medina <e....@gmail.com>:
> 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?
>