Is it mandatory create a class that implements SelectableDataModel
when I want to use selection
property on my <p:dataTable>
? If yes, how can I "link" my dataTable with a class that implements SelectableDataModel
?
Asked
Active
Viewed 159 times
1

BalusC
- 1,082,665
- 372
- 3,610
- 3,555

Ronaldo Lanhellas
- 2,975
- 5
- 46
- 92
-
http://stackoverflow.com/questions/10513873/datamodel-must-implement-org-primefaces-model-selectabledatamodel-when-selection/10514441#10514441 – rags Aug 22 '13 at 10:01
1 Answers
0
No, that's not necessary. You can just specify the rowKey
attribute so that <p:dataTable>
can figure the unique identifier of the row without the need for SelectableDataModel
.
<p:dataTable value="#{bean.items}" var="item" rowKey="#{item.id}" ...>
For the case you're interested, or need to, here's how you should implement it:
@ManagedBean
@ViewScoped
public class Bean implements Serializable {
private List<Item> items;
private ItemDataModel itemModel;
public Bean() {
items = itemService.list();
itemModel = new ItemDataModel(items);
}
// ...
}
Where the ItemDataModel
look like this:
public class ItemDataModel extends ListDataModel<Item> implements SelectableDataModel<Item> {
public ItemDataModel() {
// Default c'tor, keep alive.
}
public ItemDataModel(List<Item> data) {
super(data);
}
@Override
public Item getRowData(String rowKey) {
List<Item> items = (List<Item>) getWrappedData();
for (Item item : items) {
if (item.getId().equals(rowKey)) {
return item;
}
}
return null;
}
@Override
public Object getRowKey(Item item) {
return item.getId();
}
}
Finally use itemModel
instead of items
as <p:dataTable value>
.
<p:dataTable value="#{bean.itemModel}" var="item" ... />

BalusC
- 1,082,665
- 372
- 3,610
- 3,555