Lyubomyr Shaydariv
Lyubomyr Shaydariv

Reputation: 21105

PrimeFaces DataTable: (Multi)selection does not work for dynamically built tables giving only nulls

I'm working with the multiple row selection to give a user ability to delete the selecting records. According to the PDF documentation, and the ShowCase Labs, I must use the code translated to the Java like that:

final DataTable = new DataTable();
...
// (1)
dataTable.setSelectionMode("multiple");
// (2)
dataTable.setValueExpression("selection", createValueExpression(DbeBean.class, "selection", Object[].class));
// (3)
dataTable.setValueExpression("rowKey", createValueExpression("#{" + VARIABLE + ".indexKey}", Object.class));
...
final ClientBehaviorHolder dataTableAsHolder = dataTable;
...
// (4)
dataTableAsHolder.addClientBehavior("rowSelect", createAjaxBehavior(createMethodExpression(metaData.controllerBeanType, "onRowSelect", void.class, new Class<?>[] {SelectEvent.class})));
  1. multiple - This line features the multiple selection, works fine visually at the front-end.
  2. selection - Being invoked, the #{dbeBean.selection} is really bound and the public void setSelection(T[] selection) is only invoked.
  3. rowKey - Being invoked, works fine, the getIndexKey() is invoked and returns the necessary result.
  4. rowSelect - This event handler is invoked too, DbeBean.onRowSelect(SelectEvent e).

I also use lazy data model (I don't really believe it may be the reason but who knows?; by the way, it returns List<T> though setSelection() requires T[] -- why it's like that?):

public abstract class AbstractLazyDataSource<T extends IIndexable<K>, K> extends LazyDataModel<T> { 
... 
@Override
public final List<T> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
    ...
    final IResultContainer<T> resultContainer = getData(querySpecifier);
    final List<T> data = resultContainer.getData();
    setRowCount(resultContainer.getTotalEntitiesCount());
    return getPage(data, first, pageSize);
}   
... 
@Override
public final K getRowKey(T object) {
    return object.getIndexKey(); // T instanceof IIndexable<K>, have to return a unique ID
}
...

However, the handlers do not work as they are expected to work. Please help me to understand why (2) DbeBean.setSelection(T[] selection) & (4) DbeBean.onRowSelect(SelectEvent e) get only the null value: T[] selection = null, and SelectEvent: e.getObject = null, respectively. What am I doing wrong?

Thanks in advance.

Upvotes: 0

Views: 3160

Answers (1)

Lyubomyr Shaydariv
Lyubomyr Shaydariv

Reputation: 21105

I've got it to work: I simply removed the rowKey property during to the dynamic p:dataTable creation (DataTable), and simply overloaded getRowData in lazy data model. Now it works.

Upvotes: 1

Related Questions