The #{tableBean.cars}
must implement SelectableDataModel
if you don't specify the rowKey
attribute on the <p:dataTable>
.
You have however implemented it on the #{tableBean}
instead. This is not right. You can find correct examples in the PrimeFaces showcase. Basically, your TableBean
class must look like this according to the showcase code example:
@ManagedBean
@ViewScoped
public class TableBean implements Serializable {
private List<Car> cars;
private Car car;
private CarDataModel carsModel;
public TableBean() {
cars = new ArrayList<Car>();
// Populate cars here and thus NOT in the getter method!
carsModel = new CarDataModel(cars);
}
// ...
}
Where the CarDataModel
look like this (again, just copied from PrimeFaces showcase code example):
public class CarDataModel extends ListDataModel<Car> implements SelectableDataModel<Car> {
public CarDataModel() {
}
public CarDataModel(List<Car> data) {
super(data);
}
@Override
public Car getRowData(String rowKey) {
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();
}
}
Finally use #{tableBean.carsModel}
instead of #{tableBean.cars}
as value of <p:dataTable>
. Exactly as in the showcase example.
<p:dataTable value="#{tableBean.carsModel}" var="car" ... />
Another, easier, way is to just specify the rowKey
attribute on the <p:dataTable>
.
<p:dataTable value="#{tableBean.cars}" var="car" rowKey="#{car.model}" ... />
This way you don't need the whole SelectableDataModel
. You only need to ensure that it's never null
and always unique across the rows. See also DataModel must implement org.primefaces.model.SelectableDataModel when selection is enabled, but I have already defined rowKey.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…