Discussion:
Checkbox being selected is lost when Table Pagination is used
dsefcik
2008-10-14 21:04:23 UTC
Permalink
Hi, 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..
Rick Fincher
2008-10-15 01:17:39 UTC
Permalink
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 dsefcik
Hi, 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..
Futaleufu_John
2008-10-15 12:53:40 UTC
Permalink
Another option is to use a table select phase listener. I have several
multi-select, paginated tables in my application, all handled with table
select phase listeners and appropriate data bindings.

I generally put my table select phase listeners in a session bean and grab a
reference to them in the page beans.

Because I use phase listeners so extensively, I created a helper class that
packages the phase listener and its support methods such as getSelected,
setSelected, etc.

http://blogs.sun.com/winston/entry/single_selectable_row_table_component
http://blogs.sun.com/winston/entry/single_selectable_row_table_component
http://winstonprakash.com/articles/jsf/multi_select_table.htm
http://winstonprakash.com/articles/jsf/multi_select_table.htm
Post by dsefcik
Hi, 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?
--
View this message in context: http://www.nabble.com/Checkbox-being-selected-is-lost-when-Table-Pagination-is-used-tp19983103p19992874.html
Sent from the Netbeans IDE Users mailing list archive at Nabble.com.
Rick Fincher
2008-10-15 18:41:09 UTC
Permalink
Hi John,

Are there advantages to using a phase listener? I've seen Winston's
posts on that, but never had a need to use one.

Where do the added phase listeners get control as opposed to the getters
and setter of table elements?

Thanks!

Rick
Post by Futaleufu_John
Another option is to use a table select phase listener. I have several
multi-select, paginated tables in my application, all handled with table
select phase listeners and appropriate data bindings.
I generally put my table select phase listeners in a session bean and grab a
reference to them in the page beans.
Because I use phase listeners so extensively, I created a helper class that
packages the phase listener and its support methods such as getSelected,
setSelected, etc.
http://blogs.sun.com/winston/entry/single_selectable_row_table_component
http://blogs.sun.com/winston/entry/single_selectable_row_table_component
http://winstonprakash.com/articles/jsf/multi_select_table.htm
http://winstonprakash.com/articles/jsf/multi_select_table.htm
Post by dsefcik
Hi, 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?
Futaleufu_John
2008-10-16 12:34:02 UTC
Permalink
Virtually every table in my web app uses phase listeners. In most cases the
listener is "hooked" to a radio button column. The user selects a row, which
is highlighted, and additional information related to that row is retrieved
from the database and displayed. This lets the user "drill down" to get the
specific information he or she is looking for.

Phase listener methods (as detailed in Winston's posts) are bound to
properties of the table row group, column, and component. Everything is then
handled behind the scenes.
Post by Rick Fincher
Are there advantages to using a phase listener? I've seen Winston's
posts on that, but never had a need to use one.
Where do the added phase listeners get control as opposed to the getters
and setter of table elements?
--
View this message in context: http://www.nabble.com/Checkbox-being-selected-is-lost-when-Table-Pagination-is-used-tp19983103p20012809.html
Sent from the Netbeans IDE Users mailing list archive at Nabble.com.
Futaleufu_John
2008-10-16 12:34:52 UTC
Permalink
Virtually every table in my web app uses phase listeners. In most cases the
listener is "hooked" to a radio button column. The user selects a row, which
is highlighted, and additional information related to that row is retrieved
from the database and displayed. This lets the user "drill down" to get the
specific information he or she is looking for.

Phase listener methods (as detailed in Winston's posts) are bound to
properties of the table row group, column, and component. Everything is then
handled behind the scenes.
Post by Rick Fincher
Are there advantages to using a phase listener? I've seen Winston's
posts on that, but never had a need to use one.
Where do the added phase listeners get control as opposed to the getters
and setter of table elements?
--
View this message in context: http://www.nabble.com/Checkbox-being-selected-is-lost-when-Table-Pagination-is-used-tp19983103p20012818.html
Sent from the Netbeans IDE Users mailing list archive at Nabble.com.
Futaleufu_John
2008-10-16 12:35:42 UTC
Permalink
Virtually every table in my web app uses phase listeners. In most cases the
listener is "hooked" to a radio button column. The user selects a row, which
is highlighted, and additional information related to that row is retrieved
from the database and displayed. This lets the user "drill down" to get the
specific information he or she is looking for.

Phase listener methods (as detailed in Winston's posts) are bound to
properties of the table row group, column, and component. Everything is then
handled behind the scenes.
Post by Rick Fincher
Are there advantages to using a phase listener? I've seen Winston's
posts on that, but never had a need to use one.
Where do the added phase listeners get control as opposed to the getters
and setter of table elements?
--
View this message in context: http://www.nabble.com/Checkbox-being-selected-is-lost-when-Table-Pagination-is-used-tp19983103p20012823.html
Sent from the Netbeans IDE Users mailing list archive at Nabble.com.
dsefcik
2008-10-15 19:29:28 UTC
Permalink
thanks to all for your responses. I figured I would have to write some code but just wanted to make sure I wasn't overlooking an easier, built in way to do it.



Daren
Loading...