P:datatable selection null

datatablejsfjsf-2primefaces

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.

Best Answer

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.

Related Topic