在我的应用程序(RichFaces 4.1)中,我有一个extendedDataTable,在我的支持bean中,我需要跟踪选定的行。我用以下代码实现了这个目标:
JSF:
<rich:extendedDataTable id="freie"
selectionMode="multipleKeyboardFree"
selection="#{myBean.tableSelection}"
...
<a4j:ajax execute="@this" event="selectionchange"
listener="#{myBean.tableSelection}"
render="indicatorPanel" />
爪哇:
UIExtendedDataTable dataTable= (UIExtendedDataTable) event.getComponent();
Object originalKey= dataTable.getRowKey();
_tableSelectedEntries.clear();
for (Object selectionKey: _tableSelection) {
dataTable.setRowKey(selectionKey);
if (dataTable.isRowAvailable()) {
_tableSelectedEntries.add((Entry) dataTable.getRowData());
}
}
dataTable.setRowKey(originalKey);
只要不过滤表格,这样就可以了。我使用标准的RichFaces方式来过滤表格:
<rich:column sortBy="#{mitarbeiter.vorname}"
filterValue="#{mitarbeiterFilterBean.firstNameFilter}"
filterExpression="#{fn:containsIgnoreCase(mitarbeiter.vorname, mitarbeiterFilterBean.firstNameFilter)}">
当表被过滤并且我选择例如第一行时,我得到了支持bean中未过滤表的第一行的rowKey。当我的表被过滤时,如何获取所选行的rowData?
我认为我的代码与showcase中的代码相同。
答案 0 :(得分:2)
我可以通过使我的过滤器bean SessionScoped来解决我的问题。我也不再将当前选定的行绑定到我的支持bean。我使用以下方法获取所选行:
public void tableSelection (AjaxBehaviorEvent event) {
UIExtendedDataTable dataTable= (UIExtendedDataTable) event.getComponent();
for (Object selectionKey: dataTable.getSelection()) {
也可以使用rowKeyVar来获得正确的行索引。
答案 1 :(得分:1)
也许你忽略了一些东西,因为我尝试了它并且它有效。
我复制了selectableTable
的来源,并添加了filterTable
示例用法:要获取所选项目/项目数据,只需使用所选项目列表的get方法
源代码(xhtml):
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:rich="http://richfaces.org/rich"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Richfaces Welcome Page</title>
</h:head>
<h:body>
<h:panelGrid columns="2">
<h:form>
<fieldset style="margin-bottom: 10px;">
<legend>
<h:outputText value="Selection Mode " />
</legend>
<h:selectOneRadio value="#{exTableSelect.selectionMode}">
<f:selectItem itemLabel="Single" itemValue="single" />
<f:selectItem itemLabel="Multiple" itemValue="multiple" />
<f:selectItem itemLabel="Multiple Keyboard-free" itemValue="multipleKeyboardFree" />
<a4j:ajax render="table, res" />
</h:selectOneRadio>
</fieldset>
<rich:extendedDataTable value="#{exTableSelect.inventoryItems}" var="car"
selection="#{exTableSelect.selection}" id="table" style="height:300px; width:500px;"
selectionMode="#{exTableSelect.selectionMode}">
<a4j:ajax execute="@form" event="selectionchange" listener="#{exTableSelect.selectionListener}"
render=":res" />
<f:facet name="header">
<h:outputText value="Cars marketplace" />
</f:facet>
<rich:column filterValue="#{exTableSelect.vendorFilter}"
filterExpression="#{fn:containsIgnoreCase(car.vendor, exTableSelect.vendorFilter)}">
<f:facet name="header">
<h:panelGroup>
<h:outputText value="Vendor " />
<h:inputText value="#{exTableSelect.vendorFilter}">
<a4j:ajax render="table" execute="@this" event="change" />
</h:inputText>
</h:panelGroup>
</f:facet>
<h:outputText value="#{car.vendor}" />
</rich:column>
</rich:extendedDataTable>
</h:form>
<a4j:outputPanel id="res">
<rich:panel header="Selected Rows:" rendered="#{not empty exTableSelect.selectionItems}">
<rich:list type="unordered" value="#{exTableSelect.selectionItems}" var="sel">
<h:outputText value="#{sel.vendor} - #{sel.model} - #{sel.price}" />
</rich:list>
</rich:panel>
</a4j:outputPanel>
</h:panelGrid>
</h:body>
</html>
Managed Bean:
public class ExTableSelect {
private String selectionMode = "multiple";
private Collection<Object> selection;
private List<InventoryItem> inventoryItems;
private List<InventoryItem> selectionItems = new ArrayList<InventoryItem>();
private String vendorFilter;
public void selectionListener(AjaxBehaviorEvent event) {
UIExtendedDataTable dataTable = (UIExtendedDataTable) event.getComponent();
Object originalKey = dataTable.getRowKey();
selectionItems.clear();
for (Object selectionKey : selection) {
dataTable.setRowKey(selectionKey);
if (dataTable.isRowAvailable()) {
selectionItems.add((InventoryItem) dataTable.getRowData());
}
}
dataTable.setRowKey(originalKey);
}
public Filter<?> getFilterVendor() {
return new Filter<InventoryItem>() {
public boolean accept(InventoryItem t) {
String vendor = getVendorFilter();
if (vendor == null || vendor.length() == 0 || vendor.equals(t.getVendor())) {
return true;
}
return false;
}
};
}
@PostConstruct
public void addInventory(){
InventoryItem i = new InventoryItem();
i.setVendor("A");
InventoryItem i2 = new InventoryItem();
i2.setVendor("AB");
InventoryItem i3 = new InventoryItem();
i3.setVendor("AC");
InventoryItem i4= new InventoryItem();
i4.setVendor("E");
InventoryItem i5 = new InventoryItem();
i5.setVendor("F");
InventoryItem i6 = new InventoryItem();
i6.setVendor("G");
InventoryItem i7 = new InventoryItem();
i7.setVendor("H");
InventoryItem i8 = new InventoryItem();
i8.setVendor("I");
InventoryItem i9 = new InventoryItem();
i9.setVendor("J");
inventoryItems= new ArrayList<InventoryItem>();
inventoryItems.add(i);
inventoryItems.add(i2);
inventoryItems.add(i3);
inventoryItems.add(i4);
inventoryItems.add(i5);
inventoryItems.add(i6);
inventoryItems.add(i7);
inventoryItems.add(i8);
inventoryItems.add(i9);
}
public Collection<Object> getSelection() {
return selection;
}
public void setSelection(Collection<Object> selection) {
this.selection = selection;
}
public List<InventoryItem> getInventoryItems() {
return inventoryItems;
}
public void setInventoryItems(List<InventoryItem> inventoryItems) {
this.inventoryItems = inventoryItems;
}
public InventoryItem getSelectionItem() {
if (selectionItems == null || selectionItems.isEmpty()) {
return null;
}
return selectionItems.get(0);
}
public List<InventoryItem> getSelectionItems() {
return selectionItems;
}
public void setSelectionItems(List<InventoryItem> selectionItems) {
this.selectionItems = selectionItems;
}
public String getSelectionMode() {
return selectionMode;
}
public void setSelectionMode(String selectionMode) {
this.selectionMode = selectionMode;
}
public void setVendorFilter(String vendorFilter) {
this.vendorFilter = vendorFilter;
}
public String getVendorFilter() {
return vendorFilter;
}
}