我希望从以下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>
Run Code Online (Sandbox Code Playgroud)
我有一种情况,我正在使用多个键作为主键, 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();
}
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏。
您需要使用复合键作为行键。
例如
rowKey="#{car.compositeKey}"
Run Code Online (Sandbox Code Playgroud)
或者,如果您坚持使用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();
}
Run Code Online (Sandbox Code Playgroud)
至于 的具体实现getCompositeKey(),目前还不清楚您拥有哪两个主键以及您使用的是什么持久性 API。例如 JPA 已经支持复合键,因此您的实体应该已经支持它。但是,如果您出于某种原因不使用 JPA,那么这里有一个示例,它假定 themodel和 the 都color代表复合键,只是为了说明这个想法:
public String getCompositeKey() {
return model + "." + color;
}
Run Code Online (Sandbox Code Playgroud)
或者
public Object[] getCompositeKey() {
return new Object[] { model, color };
}
Run Code Online (Sandbox Code Playgroud)
或者任何唯一表示为基于Object#equals()合同的复合键的东西。
您可以直接使用所有键编写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}">
Run Code Online (Sandbox Code Playgroud)
您无需担心创建函数来解决此问题。
只需在有效的rowKey中键入每个键即可。
| 归档时间: |
|
| 查看次数: |
6498 次 |
| 最近记录: |