Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using rowKey in primefaces dataTable with new entity

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'}">
like image 348
Shelly Avatar asked Sep 14 '13 00:09

Shelly


2 Answers

Use another unique identifier then. Perhaps the hashCode() or even the toString()?

rowKey="#{not empty item.id ? item.id : item.hashCode()}"
like image 186
BalusC Avatar answered Oct 18 '22 19:10

BalusC


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}"
like image 2
jbbarquero Avatar answered Oct 18 '22 19:10

jbbarquero