3

在我的应用程序(RichFaces 4.1)中,我有一个扩展数据表,在我的支持 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?

我认为我的代码的工作方式与展示中的相同。

4

2 回答 2

2

我可以通过使过滤器 bean SessionScoped 来解决我的问题。我也不再将当前选定的行绑定到我的支持 bean。我使用以下方法获取选定的行:

public void tableSelection (AjaxBehaviorEvent event) {
    UIExtendedDataTable dataTable= (UIExtendedDataTable) event.getComponent();
    for (Object selectionKey: dataTable.getSelection()) {

也可以使用rowKeyVar来获得正确的行索引。

于 2012-10-05T07:13:50.633 回答
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>

托管豆:

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;
    }
}
于 2012-10-03T12:08:35.597 回答