1

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.

user513951
  • 12,445
  • 7
  • 65
  • 82
nosnhoj
  • 793
  • 1
  • 12
  • 30

3 Answers3

3

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.

nosnhoj
  • 793
  • 1
  • 12
  • 30
  • I have a similar problem. I have set the rowKey with a Primary key which is fetched from MySQL table. Still when I try to select and export I get the rows from first. http://stackoverflow.com/questions/30054708/pdataexporter-selected-rows-only – Arun Raja May 06 '15 at 08:15
1

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.

argh1969
  • 69
  • 7
  • Hi argh, sorry for reply lately. However, I did what you said and the problem remains. Do you have other thoughts? Thank you! – nosnhoj Apr 03 '14 at 14:06
  • could you shown my the source of the entire backing bean, perhaps ? I use this all the time, and it works fine for me. – argh1969 Apr 03 '14 at 20:12
  • Thank you, I have simplify my code and put it on google drive: https://drive.google.com/file/d/0B5ye4CP3K_a3blBnbVk3cmNjMEE/edit?usp=sharing BTW, 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. Thank you again. – nosnhoj Apr 04 '14 at 02:22
  • Thank you argh1969, I found the solution. It turns out that the rowKey was null. It works fine after I set the rowKey. Thank you anyway. – nosnhoj Apr 04 '14 at 06:45
1

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

<h:form>
    <p:dataTable> ... </p:dataTable>
</h:form>
Benjamin Zach
  • 1,452
  • 2
  • 18
  • 38
krizajb
  • 1,715
  • 3
  • 30
  • 43