【问题标题】:How to reorder columns in datatable with drag and drop Primefaces如何通过拖放 Primefaces 对数据表中的列重新排序
【发布时间】:2016-03-09 14:35:11
【问题描述】:

我使用 primefaces 5.2 版和 jsf 2.2.6 版。

我有一个数据表,我想通过拖放对列重新排序。此外,我还有列切换功能,我可以使用它隐藏列。列的重新排序和隐藏保存到数据库中。

对我来说,如果在特定位置是隐藏列是没有问题的,我可以将拖动的列放在隐藏的位置之后或之前。

我使用以下 ajax 事件:

<p:ajax event="colReorder" listener="#{transactionsPage.onColumnReorder}"/>

方法签名如下所示:

public void onColumnReorder(AjaxBehaviorEvent event)

如果我可以使用 ReorderEvent 来获取 fromIndex 和 toIndex,那么处理这种情况会很容易,但不幸的是,这个事件只能用于拖放行而不是列。

有没有办法找出这些索引?甚至只有 fromIndex 就足够了。

【问题讨论】:

标签: events primefaces datatable drag-and-drop


【解决方案1】:

我设法找到了一个解决方案,也许它可以在将来对某人有所帮助。

因为我没有找到用于对列重新排序的 fromIndex 和 toIndex 索引(只有这样的用于重新排序行的索引,它与 ReorderEvent 一起使用),所以我从 AjaxBehaviorEvent 获取数据表并从中获取列和覆盖数据库列中重新排序的列的这些索引,然后我可以稍后使用重新排序的列恢复视图。

从 xhtml 页面触发事件的代码如下所示:

<p:ajax event="colReorder" listener="#{transactionsPage.onColumnReorder}"/>

托管 bean 的代码如下所示:

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);
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2018-01-25
    • 1970-01-01
    • 2020-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多