1

我希望从以下 Primefaces 3.1 p:datatable 传递多个参数:

 <p:dataTable value="#{tableBean.carsModel}" var="var" rowkey="#{var.model}" 
              selection="#{tableBean.car}" selectionMode="single">
    <p:ajax event="rowSelect" listener="#{tableBean.onRowClick}"></p:ajax>
    <p:column>
        <f:facet name="header">
            <h:outputText styleClass="outputText" value="Model"></h:outputText>
        </f:facet>
        <h:outputText styleClass="outputText" value="#{var.model}"></h:outputText>
    </p:column>
    <p:column>
        <f:facet name="header">
            <h:outputText styleClass="outputText" value="Color"></h:outputText>
        </f:facet>
        <h:outputText styleClass="outputText" value="#{var.randomColor}"></h:outputText>
    </p:column>
</p:dataTable>

我有一种情况,我有多个主键作为主键,我正在使用 rowkey="#{var.model}但是,如何使用多个主键。

我也在使用CarDataModel extends ListDataModel<Car> implements SelectableDataModel<Car>{ 类。有人可以告诉我如何使用吗?

@Override
public Car getRowData(String rowKey) {
    //In a real app, a more efficient way like a query by rowKey 
    //should be implemented to deal with huge data
    List<Car> cars = (List<Car>) getWrappedData();
    for(Car car : cars) {
        if(car.getModel().equals(rowKey))
            return car;
    }
    return null;
}

@Override
public Object getRowKey(Car car) {
    return car.getModel();
}

任何帮助表示赞赏。

4

3 回答 3

5

您需要将复合键用作行键。

例如

rowKey="#{car.compositeKey}"

或者,如果您坚持使用SelectableDataModel

@Override
public Car getRowData(String rowKey) {
    List<Car> cars = (List<Car>) getWrappedData();

    for (Car car : cars) {
        if (car.getCompositeKey().equals(rowKey))
            return car;
    }

    return null;
}

@Override
public Object getRowKey(Car car) {
    return car.getCompositeKey();
}

至于具体的实现getCompositeKey(),目前还不清楚你有哪两个主键,你使用的是什么持久化 API。例如,JPA 已经支持复合键,因此您的实体应该已经支持它。但是,如果您出于某种原因没有使用 JPA,那么这里有一个示例,它假设 themodel和 the都color表示复合键,只是为了说明这个想法:

public String getCompositeKey() {
    return model + "." + color;
}

或者

public Object[] getCompositeKey() {
    return new Object[] { model, color };
}

或者基于Object#equals()合同唯一表示为复合键的任何东西。

于 2012-05-14T22:34:50.640 回答
4

您可以直接编写包含所有键的 rowKey:

<p:dataTable id="productItemsTable" var="i" value="#{b2BOrdersBean.listProductItems}" 
                rowKey="#{i.id.orderId}_#{i.id.productType}_#{i.id.partId}" 
                selectionMode="single" selection="#{b2BOrdersBean.selectedProductItemRow}">

你不需要担心创建一个函数来解决这个问题。

只需在 rowKey 中键入它有效的每个键。

于 2016-09-26T07:39:28.047 回答
1

我通过扩展基类提供唯一(行)键解决了这个问题,使用传递对象的属性对其进行初始化,并添加一个带有计数器值的附加 Id 字段。尚未尝试,但应该使用反射来自动化属性字段的副本。我很困惑为什么 PF 没有提供这样一个隐含的机制。

于 2012-09-27T13:36:51.893 回答