1
votes

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.

3
What's the scope of your backing bean?kolossus

3 Answers

3
votes

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.

1
votes

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.

1
votes

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>