ScrollableDataTable Selection Bug?
buttau Dec 11, 2008 1:29 PMI found a strange behavior by ScrollableDataTable and its Selection.
if you use such a code to get selection in some action method
(this pattern is used in RichFaces-Demo)
Iterator<Object> iterator = getSelection().getKeys(); while (iterator.hasNext()){ Object key = iterator.next(); table.setRowKey(key); if (table.isRowAvailable()) { selectionList.add((Item) table.getRowData()); } }
and do rerender the scrollableDataTable after action, the ScrollableDataTable will be not properly rendered. No header is visible, and the columns are not properly rendered.
but there is a workaround. you must set row key to NULL after getting row data, like this:
Iterator<Object> iterator = getSelection().getKeys(); while (iterator.hasNext()){ Object key = iterator.next(); table.setRowKey(key); if (table.isRowAvailable()) { selectionList.add((Item) table.getRowData()); } table.setRowKey(null); }
i've tested with richfaces-3.3.0.BETA3, IE 7.0, Firefox 3.0
hier is my test code:
Page
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:a4j="http://richfaces.org/a4j" xmlns:rich="http://richfaces.org/rich" xmlns:itc="http://www.it-choice.de/jsf/core"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta http-equiv="Cache-Control" content="no-cache" /> <meta http-equiv="Pragma" content="no-cache" /> <meta http-equiv="Expires" content="0" /> <title>Test</title> </head> <body> <f:view contentType="text/html"> <a4j:form id="form"> <h:panelGrid columnClasses="panel" border="0" columns="1" width="100%"> <rich:panel > <h:panelGrid columns="4"> <a4j:commandButton value="Selection" reRender="view" action="#{testBean.performSearch}"/> <a4j:outputPanel id="ids_panel" ajaxRendered="true"> <h:inputText id="ids" readonly="true" value="#{testBean.ids}" /> </a4j:outputPanel> </h:panelGrid> </rich:panel> <rich:panel id="view" > <f:facet name="header"> <h:outputText value="Liste"></h:outputText> </f:facet> <rich:scrollableDataTable rowKeyVar="rkv" frozenColCount="1" height="300px" width="600px" id="viewgrid" rows="30" value="#{testBean.data}" var="row" binding="#{testBean.tableComponent}" selection="#{testBean.selection}" sortMode="single"> <rich:column id="c1" width="100"> <f:facet name="header"><h:outputText value="C1" /></f:facet> <h:outputText value="#{row.c1}" /> </rich:column> <rich:column id="c2" width="100"> <f:facet name="header"><h:outputText value="C2" /></f:facet> <h:outputText value="#{row.c2}" /> </rich:column> </rich:scrollableDataTable> </rich:panel> </h:panelGrid> </a4j:form> </f:view> </body> </html>
Bean:
package de.itc.beans; import java.util.ArrayList; import java.util.Iterator; import org.richfaces.component.html.HtmlScrollableDataTable; import org.richfaces.model.selection.SimpleSelection; public class TestBean2 { public class TestItem { private String c1; private String c2; public TestItem (String c1, String c2) { this.c1 = c1; this.c2 = c2; } public String getC1() { return c1; } public void setC1(String c1) { this.c1 = c1; } public String getC2() { return c2; } public void setC2(String c2) { this.c2 = c2; } } private HtmlScrollableDataTable tableComponent = null; private SimpleSelection selection; ArrayList<TestItem> data; String ids = "-"; String columns; public TestBean2() { data = new ArrayList<TestItem>(); for (int i=0; i<32; i++) { data.add(new TestItem("C1-" + i, "C2-" + i)); } } public String getIds() { return ids; } public void setIds(String ids) { this.ids = ids; } public ArrayList<TestItem> getData() { return data; } public void setData(ArrayList<TestItem> data) { this.data = data; } public SimpleSelection getSelection() { return selection; } public void setSelection(SimpleSelection selection) { this.selection = selection; } public HtmlScrollableDataTable getTableComponent() { if (tableComponent == null) { tableComponent = new HtmlScrollableDataTable(); } return tableComponent; } public void setTableComponent(HtmlScrollableDataTable tableComponent) { this.tableComponent = tableComponent; } public String getColumns() { return columns; } public void setColumns(String columns) { this.columns = columns; } public String performSearch(){ int selectionSize = getSelection().size(); this.ids = "-"; if (selectionSize > 0) { Iterator<Object> iterator = getSelection().getKeys(); while (iterator.hasNext()) { Object key = iterator.next(); tableComponent.setRowKey(key); if (tableComponent.isRowAvailable()) { TestItem row = (TestItem)tableComponent.getRowData(); if (this.ids.equals("-")) { this.ids = row.getC1(); } else { this.ids += "," + row.getC1(); } } tableComponent.setRowKey(null); // COMMENT THIS LINE TO REPRODUCE THE BUG } } return null; } }