XHTML side:
<p:dataTable id="selectProductTable" var="product" value="#{manageFormsView.productModel}" selection="#{manageFormsView.product}" >
bean side:
private SelectableDataModel<Product> productModel=new SelectableDataModel<Product>() {
@Override
public Product getRowData(String rowKey) {
//In a real app, a more efficient way like a query by rowKey should be implemented to deal with huge data
for(Product product : productList) {
if(product.getModel().equals(rowKey))
return product;
}
return null;
}
@Override
public Object getRowKey(Product p) {
return p.getModel();
}
};
i do not want to generate a new class which implements SDM, can not i do inline implementaton like above?
I am getting exception:
javax.faces.FacesException: DataModel must implement org.primefaces.model.SelectableDataModel when selection is enabled.
The exception message is misleading. Implementing only the
SelectableDataModelinterface is not sufficient. You also need to extend anDataModelimplementation such asListDataModel. That can’t be done in flavor of an anonymous class. You really need to create another class.You can if necessary generify it if you have a common base entitiy (with
getId()and so on), so that you don’t need to create another one for every entity.As a completely different alternative, you can also use
rowKeyattribtue of the<p:dataTable>and let it refer exactly the same value asSelectableDataModel#getRowKey(). This way you don’t need the wholeSelectableDataModelinterface anymore.See also: