p:datatable selection null

十年热恋 提交于 2019-12-23 03:16:11

问题


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...

here's my page:

<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>

In my backing bean:

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

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

DataModel:

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.

Update

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.


回答1:


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".

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.




回答2:


Try this :

if(app.getAppraiseDetailID().toString().equals(rowkey)) { ...

instead of what you have. AppraiseDetailDataModel must also implement Serializable. Also remove the "()" in:

listener="#{newAppraiseBean.modifyAppDetail()}"

Finally, ensure that the method signature for the listener is:

public void modifyAppDetail(SelectEvent event)

You can set a breakpoint in that method and inspect event.getObject(), which should reference the selected row.




回答3:


I was dealing with same problem, even tho I had properly identified list of object. In my case I forgot to wrap the dataTable in a form.

<h:form>
    <p:dataTable> ... </p:dataTable>
</h:form>


来源:https://stackoverflow.com/questions/22816268/pdatatable-selection-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!