p:数据表选择null [英] p:datatable selection null

查看:120
本文介绍了p:数据表选择null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用primefaces 4.0的可选数据表,但是所选对象始终为null. 我已经厌倦了在此处此处说,但仍然为空...

I'm trying to use selectable datatable of primefaces 4.0, but the selected object is always null. I have tired add rowKey like here and here said, but still get null...

这是我的页面:

<p:dataTable id="appDetailTable" var="appDetail"  value="#{newAppraiseBean.appDetailDataModel}"  
             paginator="true" rows="5" paginatorPosition="bottom" selection="#{newAppraiseBean.selectedAppDetail}" 
             rowKey="#{appDetail.appraiseDetailID}" selectionMode="single">
  <p:ajax event="rowSelect" listener="#{newAppraiseBean.modifyAppDetail()}" oncomplete="newAppDlg.show();" update=":newAppraiseForm:newAppDetail"/>   
</p:dataTable>

在我的后备豆中:

newAppraiseBean.modifyAppDetail():(只需打印所选项目)

newAppraiseBean.modifyAppDetail(): (Just print the selected item)

public void modifyAppDetail(){
    System.out.println("modify, selectedAppDetail:"+selectedAppDetail);
}

数据模型:

private class AppraiseDetailDataModel extends ListDataModel<Appraisedetail> implements SelectableDataModel<Appraisedetail> {

    public AppraiseDetailDataModel(List<Appraisedetail> list) {
        super(list);
    }

    @Override
    public Object getRowKey(Appraisedetail t) {
        return t.getAppraiseDetailID();
    }

    @Override
    public Appraisedetail getRowData(String string) {
        List<Appraisedetail> appList=(List<Appraisedetail>) getWrappedData();
        for(Appraisedetail app:appList){
            System.out.println(app.getAppraiseDetailID());
            if(app.getAppraiseDetailID()==Integer.parseInt(string)){
                return app;
            }
        }
        return null;
    }

}

它总是打印为空,我不知道我在想什么.

It always print null and I don't know what am I missing.

我已经简化了代码,并将其放在 google驱动器.
这是netbean项目的压缩文件,您可以在解压缩后直接用netbean打开它.
当然,在简化代码之后,问题仍然存在.

I have simplified my code and put it on google drive.
This is an zipped file of netbean project, you may open it directly with netbean after unzip it.
And of course the problem remains after I simplify my code.

推荐答案

在仔细检查代码后,我解决了问题.
我发现我没有指定appraiseDetailID,它也是rowKey.
我没有指定它,因为我希望数据库在将数据插入到数据库时生成ID. 而且getRowKey方法始终为null,因为尚未将数据插入DB中,当然也没有生成ID.
随后,当primefaces要使用rowKey为"null"的getObject时,它什么也不会得到.

I solved the problem after I carefully check my code.
I found that I didn't specify the appraiseDetailID, which is also the rowKey.
I didn't specify it because I want DB to generate the id when the data inserted to DB. And the method getRowKey always get null because the data have not inserted into DB, and Of course the id has not generated.
Subsequently, primefaces get nothing while it want to getObject with rowKey "null".

因此,我自己指定ID后,一切正常!
对于遇到相同问题的用户,请记住指定rowKey ,以便您可以使用可选的数据表.

So, after I specify the id myself, all works fine!
For those who got the same problem, remember to specify rowKey so that you can use selectable datatable.

这篇关于p:数据表选择null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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