0
votes

I'm trying to get the row object, in my case of class Story.class, while reordering a row in a primefaces datable. The indexes, which I get from a ReorderEvent, unfortunately are not enough since I have more then one datatable where I would like to reuse the same rowReorder listener.

Here some code fragments:

<p:ajax event="rowReorder"
                listener="#{reorderStoryView.onRowReorder}" />

The following code line returns null:

Story story2 = context.getApplication().
      evaluateExpressionGet(context, "#{story}", Story.class);

The following code line returns not the current rowData, have problems to determine which row data I get:

Story story =(Story)((DataTable)event.getComponent()).getRowData();

Can't find any additional information about this problem, maybe you can help me out.

Thx in advance

/d

1
For determining what datatable the event comes from: You explicitly cast to a Datatable. Why not use the id of that datatable?Kukeltje
Thought about it, as primarily backend developer I have some concerns about this solution and further I wasn't able to define numeric values in the id field of the datatable. Do you have any idea why?David M.

1 Answers

1
votes

You can read the source table adding this line to your listener code:

String source = ((DataTable)event.getSource()).getClientId();

An example listener method:

public void onRowReorder(ReorderEvent event) {
    int from = event.getFromIndex();
    int to = event.getToIndex();
    // the source table
    String source = ((DataTable)event.getSource()).getClientId();
    // Or String source =  event.getComponent().getClientId();
    // Access from backing bean (require reordering in backing bean)
    // reorderStoryView.stories.get(from)
    // reorderStoryView.stories.get(to)
    // Access to the moved row from table component
    DataTable myDatatable = (DataTable) FacesContext.getCurrentInstance()
        .getViewRoot().findComponent(source);
    Story story = myDatatable.getRowData(); 
    ...
}