Reputation: 1035
Well, I know that i must choose a field from my object that is unique, like a ID. Then my rowKey is "item.id". This works fine when load all itens in a dataTable, but if I add a new item (this object didn't persisted in database yet) with null ID, it don't apper in my dataTable.
What is the solution for this ? I just can use ID as a rowKey but sometimes i need add a new object with NULL ID (because this is didn't persisted on database yet).
Look my code:
<p:dataTable rowKey="#{item.id}" var="item"
value="#{orcamentoMB.itens}"
emptyMessage="Não foi encontrado nenhum registro"
id="dataTableItens"
selection="#{orcamentoMB.selectedItemOrcamento}"
selectionMode="single" rowIndexVar="rowIndex"
rowStyleClass="#{(rowIndex mod 2) eq 0 ? 'first-row' : 'second-row'}">
Upvotes: 6
Views: 18442
Reputation: 53
This is not an answer, but a comment to proposed solutions.
Keep in mind that you may end up with non unique rowKeys. For that reason I decided to use -1 as rowkey for a new row and to accept only one new row at the time, hope this helps.
Upvotes: -1
Reputation: 2842
If you're not using EL 2.2, so that you can't call methods, you can create a method in your Item class for obtaining a unique ID to be used as rowKey, for instance:
public int getRowKey() { return this.hashCode(); }
And then you can use in the JSF page:
rowKey="#{item.rowKey}"
Upvotes: 2
Reputation: 1108782
Use another unique identifier then. Perhaps the hashCode()
or even the toString()
?
rowKey="#{not empty item.id ? item.id : item.hashCode()}"
Upvotes: 12