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'}">
Use another unique identifier then. Perhaps the hashCode()
or even the toString()
?
rowKey="#{not empty item.id ? item.id : item.hashCode()}"
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}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With