Re: Show Stopper: User table row selection - Java Studio Creator / VWP
by Chris Kutler Mar 05, 2007; 06:46pm :: Rate this Message:(use ratings to moderate[?])
Reply | Reply to Author | Show Only this Message
More links
http://blogs.sun.com/divas/entry/radio_buttons_in_tables
http://blogs.sun.com/divas/entry/links_in_tables
http://blogs.sun.com/divas/entry/using_checkboxes_in_a_table
http://blogs.sun.com/divas/entry/iterating_through_table_data
I've already read those blog entries. The problem I'm trying to solve is not mentioned in blogs - it is my application which needs to highlight and select radiobuttons not the user. In other words how can I highlight row and select coresponding radiobutton having RowKey object.
best regards
Grzesiek
The TableSelectPhaseListener is in fact a holder for the selected values (internally it's a phase listener but thats not of concern to you). If you set the keepselected property then it's really just a wrapper for a Map.
So, in theory, manipulating the underlying value in the TableSelectPhaseListener should give the results you want.
I already tried something like this:
void preredner()
{
//..
RowKey row = dataProvider.findFirst("TABLE_ID", value);
tablePhaseListener.setSelected(row, value);
//..
}
But it only highlighted needed row and didn't check radiobutton. It also caused that table behavior became unexpected.
best regards
Grzesiek
The two examples I looked at (Winston's and The Diva's FoodTable) had something dodgy going on with the selectValue() property. I'm not sure what they were trying to achieve there but it's unnecessery (I used a map instead of the tableSelectPhaseListener -- it was a lot simpler)
In general if you remove all bindings from SelectedValue and go with the tableSelectPhaseListener's defaults that will probably solve your problem. IE you will be able to select a row by manipulating the underlying tableSelectPhaseListener's property.
void preredner()
{
//..
RowKey row = dataProvider.findFirst("TABLE_ID", value);
tablePhaseListener.setSelected(row, row);
/////^^^^^^^^
//..
}
The above may work if you have followed the examples and bound the selectedValue as suggested in Winston's blog and the Diva's blog
What should happen if you remove the bindings as I suggested earlier is:
tablePhaseListener.setSelected(row, new Boolean(true));
I'm planning to document how to do this using direct binding to a Map -- it's a lot simpler. Will take a few days though.
Selectable Table Row Simplified
This describes a simpler way to implement a selectable table row.
First things:
*Create a page with a table bound to a database with 10 or so records.
*Add a button
*Add a messagegroup
*Add a column with a radiobutton to the table.
*Add a property to your sessionbean of type Map. Edit your sessionbean so it looks like this:
/**
* Holds value of property selectHolder.
*/
private Map selectHolder=new HashMap();
/**
* Getter for property selectHolder.
* @return Value of property selectHolder.
*/
public Map getSelectHolder() {
return this.selectHolder;
}
/**
* Setter for property selectHolder.
* @param selectHolder New value of property selectHolder.
*/
public void setSelectHolder(Map selectHolder) {
this.selectHolder = selectHolder;
}
This map will store the selected property of your radio button but you could use the same technique to store any property
Bind the 'selected' property of your radiobutton to the map. IMPORTANT: the property editor won't do this sort of binding, nor will the bind to data dialog. Either right click on the radio button and choose 'property bindings' or edit the jsp. Remember to apply your changes.
Navigate to selectHolder property in the sessionbean then manually edit the binding to look like this:
#{SessionBean1.selectHolder[currentRow.tableRow.rowId]}
Now bind the selected property of your tablerowgroup to the same property:
#{SessionBean1.selectHolder[currentRow.tableRow.rowId]}
Doubleclick your button. Add this code to the button action:
Iterator i=Arrays.asList(tableRowGroup1.getSelectedRowKeys()).iterator();
while (i.hasNext()){
info(((RowKey)i.next()).getRowId());
}
If you run your app now it will display the row numbers of the rows you select. At this point you have a table with selectable rows!
To make the radiobutton work like a radiobutton group set the 'name' property of the column to 'radioButton1'
To change the style of the column set the 'selectId' property of the column to 'radioButton1' (This might also make the select all button work -- I'm not sure)
To get the style updated dynamically add this javascript in the body but before the table:
<script>
function initAllRows() {
var table = document.getElementById("form1:table1");
table.initAllRows();
}
</script>
and call initAllRows() on the onClick handler of the radioButton.
To select a row from the server-side (assuming you have the rowkey already):
getSessionBean1().getSelectHolder().put(row.getRowId(),new Boolean(true));
To see if row 3 is selected:
selectHolder.get(new Integer(2));
etc. etc.
Acknowledgements:
All this stuff is pinched. The binding is relatively novel; the other knowledge is better presented in Winson's blog and the Tutorial Divas' example.
Warning: Using a Map instead of the TableSelectPhaseListener means you can have hidden selected rows, which is apparently a bad idea. To rewrite TableSelectPhaseListener so that it exposes the internal map is pretty trivial (there's a quick attempt on netbeans.org issues http://www.netbeans.org/issues/show_bug.cgi?id=97183)
I went through the blogs from Winston and have the folowing concern
If the table consists of multiple pages and I want to select rows from different pages then click on display row no. button. For this the result will be only the rows that are selected in the last page. The question how can we keep track of the selected rows when we move from table page to another.