primefaces dataTable multiple selection using List instead of Array
-
12-06-2021 - |
Question
i want to obtain this:
<h:form id="form">
<p:dataTable value="#{testBean.allItems}" var="item" selection="#{testBean.category.itemList}" selectionMode="multiple">
<p:column>#{item.name}</p:column>
</p:dataTable>
</h:form>
where
@ManagedBean
public class TestBean
{
private static List<Item> itemDB = new ArrayList<Item>();
static
{
itemDB.add(new Item("zero"));
itemDB.add(new Item("one"));
itemDB.add(new Item("two"));
itemDB.add(new Item("three"));
itemDB.add(new Item("four"));
itemDB.add(new Item("five"));
}
private Category category;
@PostConstruct
public void init()
{
category = new Category();
category.setName("root");
category.getItemList().add(itemDB.get(2));
category.getItemList().add(itemDB.get(3));
}
public List<Item> getAllItems()
{
return itemDB;
}
public Category getCategory()
{
return category;
}
public void setCategory(Category category)
{
this.category = category;
}
}
i think my choices are:
- create a sort of translator from List to Array and vice versa, but i'm having a headeache about make it work with ValueExpressions...
- extend PrimeFaces DataTable and DataTableRenderer but it can be a real pain to figure out
any better idea?
Solution 3
i've extended primefaces DataTable
with my own class.
OTHER TIPS
The Collections-API has some relatively simple methods for this case.
Item[] itemArr = itemDB.toArray(new Item[0]);
and
itemDB = Arrays.asList(itemArr);
This is how I'm doing it. I haven't done a lot of testing with it so far, but it does appear to work. I think a patch to Primefaces is probably a better solution.
Here's the list methods I wanted to use:
public List<R> getSelectionRecordList() {
return selectionItems;
}
public void setSelectionRecordList(List<R> selectionItems) {
this.selectionItems = selectionItems;
}
Here's the two extra array methods I added to access that list.
public R[] getSelectionRecordArray() {
if (null == selectionItems) {
return null;
}
return (R[]) selectionItems.toArray();
}
public void setSelectionRecordArray(R[] selectionArray) {
if (null == selectionArray || selectionArray.length == 0) {
selectionItems = null;
} else {
selectionItems = new ArrayList<R>(selectionArray.length);
for (R record : selectionArray) {
this.selectionItems.add(record);
}
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow