Reputation: 551
I use primefaces version 5.2 and jsf version 2.2.6.
I have a datatable and I want to reorder the columns with drag and drop. Besides I have also the column toggle functionality, with which I can hide columns. The reordering and hiding of columns are saved into the database.
For me it is no problem if on specific positions are hidden columns, I can drop the dragged column after or before the position of the hidden ones.
I use the following ajax event:
<p:ajax event="colReorder" listener="#{transactionsPage.onColumnReorder}"/>
and the method signature looks like this:
public void onColumnReorder(AjaxBehaviorEvent event)
If I could have used ReorderEvent to get the fromIndex and toIndex, it would have been very easy to deal with this situation, but unfortunately this event can be used only for dragging and dropping rows and not columns.
Is there a way to find out these indexes? Even only the fromIndex could be enough.
Upvotes: 1
Views: 4165
Reputation: 551
I managed to find a solution, maybe it can be helpful for someone in the future.
Because I didn't find indexes for fromIndex and toIndex for reordering the columns(there are only such indexes for reordering rows, which work with the ReorderEvent), I get the datatable from the AjaxBehaviorEvent and from that I get the columns and overwrite these indexes of the reordered columns in the database columns and then I can restore the view later with the reordered columns.
The code for triggering the event from the xhtml page looks like this:
<p:ajax event="colReorder" listener="#{transactionsPage.onColumnReorder}"/>
The code for the managed bean looks like this:
public void onColumnReorder(AjaxBehaviorEvent event) {
DataTable table = (DataTable) event.getSource();
List<ViewAttributes> allViewAttributesList = loadAllViewAttributes();
for (int i = 0; i < table.getColumns().size(); i++) {
UIComponent colComponentDestination = (UIComponent) table.getColumns().get(i);
//allViewAttributes is the list of columns from the database, in which I set the new indexes
for (int j = 0; j < allViewAttributesList.size(); j++) {
ViewAttributes viewAttrSource = allViewAttributesList.get(j);
if (viewAttrSource.getColumnName().equals(colComponentDestination.getId()) && i != viewAttrSource.getPosition()) {
viewAttrSource.setPosition(new Long(i));
//save column with new index in the database
getServiceManager().getViewService().getViewDao().update(viewAttrSource);
}
}
}
}
Upvotes: 1