Hi,
The HTML page gets redrawn when you page, so the selected state gets
lost unless you save it in the session bean. This is very simple (Code
is below). Just create a boolean property in the page bean that you use
to control the checked/unchecked state of the checkbox. Bind that to
the "selected" property of the checkbox in the table.
Create a map of booleans keyed by some value. RowKey is usually used.
In my example below, I used the database stock number of the table
items, because I wanted to be able to retrieve a list of selected stock
numbers. You could do that with RowKeys too, but I wanted the selected
stock numbers to persist after the page with the table was gone.
In the session bean I created the Map and a "clear" method to empty the
map. I just retrieve the map and use it directly in the page bean, but
I suppose it would be better form to have methods in the session bean to
do that.
If you use a database primary key like I did your selected items can
persist throughout the session. You have to be sure to clear the map
when you don't want the selections around anymore.
This is also a cool way of sharing info among users. If you put the
selected map in the Application Bean, users will be able to see
selections made by other users. Until the app is restarted anyway
(unless you save it to the database).
Rick
Code follows:
In the Page Bean:
private boolean checkBoxSelectChecked;
/**
* Getter for property checkBoxSelectChecked.
* @return Value of property checkBoxSelectChecked.
*/
public boolean isCheckboxSelectChecked() {
boolean bVal;
String sn = (String)
getValue("#{currentRow.value['STOCK_NUMBER']}");
Boolean isChecked = (Boolean)
getSessionBean1().getCheckBoxSelectedMap().get(sn);
if (isChecked != null) {
bVal = isChecked.booleanValue();
return bVal;
} else {
return false;
}
}
/**
* Setter for property checkBoxSelectChecked.
* @param certIconVisible New value of property checkBoxSelectChecked.
*/
public void setCheckboxSelectChecked(boolean b) {
String sn = (String)
getValue("#{currentRow.value['STOCK_NUMBER']}");
if (!clearCheckboxes) {
getSessionBean1().getCheckBoxSelectedMap().put(sn, new
Boolean(b));
} else {
getSessionBean1().getCheckBoxSelectedMap().put(sn, new
Boolean(false));
}
}
In the session Bean:
private HashMap<String, Boolean> checkBoxSelectedMap = new
HashMap<String, Boolean>();
public HashMap getCheckBoxSelectedMap() {
return checkBoxSelectedMap;
}
public void clearCheckBoxSelectedMap() {
checkBoxSelectedMapclear();
}
Post by dsefcikHi, I have a table with pagination enabled and checkboxes to select each row. The problem I am having is that a user will select a checkbox and using the table pagination buttons go to the next page to make more selections. When they do this, the previous page checkbox selections are lost. How can I make it so the checkbox selections are rembered as the user navigates thru all of the table pages?
tia..