1

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?

4

1 回答 1

0

不,这没有必要。您可以只指定rowKey属性,以便<p:dataTable>可以计算行的唯一标识符,而无需SelectableDataModel.

<p:dataTable value="#{bean.items}" var="item" rowKey="#{item.id}" ...>

对于您感兴趣或需要的情况,您应该如何实现它:

@ManagedBean
@ViewScoped
public class Bean implements Serializable {

    private List<Item> items;
    private ItemDataModel itemModel;

    public Bean() {
        items = itemService.list();
        itemModel = new ItemDataModel(items);
    }

    // ...
}

ItemDataModel看起来像这样的地方:

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();
    }

}

最后使用itemModel而不是itemsas <p:dataTable value>

<p:dataTable value="#{bean.itemModel}" var="item" ... />
于 2013-08-21T17:51:03.443 回答