使用rowKey在原始数据表与新实体 [英] Using rowKey in primefaces dataTable with new entity

查看:101
本文介绍了使用rowKey在原始数据表与新实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,我知道我必须从我的对象中选择一个独一无二的字段,就像一个ID。那么我的rowKey是item.id。当在dataTable中加载所有内容时,这可以正常工作,但是如果我添加一个新的项目(这个对象没有保留在数据库中),并且使用空ID,那么在我的dataTable中它并不是apper。



这是什么解决方案?我只需要使用ID作为rowKey,但有时候我需要添加一个带有空ID的新对象(因为这并没有在数据库中保留)。



查看我的代码:

 < p:dataTable rowKey =#{item.id}var =item
value =#{orcamentoMB.itens}
emptyMessage =Nãofoi encontrado nenhum registro
id =dataTableItens
selection =#{orcamentoMB.selectedItemOrcamento}
selectionMode =singlerowIndexVar =rowIndex
rowStyleClass =#{(rowIndex mod 2)eq 0?'first-row':'second-row'}>


解决方案

然后使用另一个唯一标识符。也许 hashCode()甚至 toString()

  rowKey =#{not empty item.id?item.id:item.hashCode()}


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

这篇关于使用rowKey在原始数据表与新实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆