【问题标题】:Primefaces datatable rowReorder get rowPrimefaces 数据表 rowReorder 获取行
【发布时间】:2015-12-27 11:39:15
【问题描述】:

我正在尝试获取行对象,以我的 Story.class 类为例,同时在 primefaces 数据表中重新排序行。不幸的是,我从 ReorderEvent 获得的索引还不够,因为我有多个数据表,我想在其中重用相同的 rowReorder 侦听器。

这里有一些代码片段:

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

以下代码行返回 null:

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

以下代码行返回的不是当前的rowData,判断我得到的是哪一行数据有问题:

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

找不到有关此问题的任何其他信息,也许您可​​以帮助我。

提前谢谢

/d

【问题讨论】:

  • 为了确定事件来自哪个数据表:您明确地转换为数据表。为什么不使用该数据表的 id?
  • 想了一下,作为主要的后端开发人员,我对这个解决方案有些担心,而且我无法在数据表的 id 字段中定义数值。你知道为什么吗?

标签: primefaces datatable


【解决方案1】:

您可以阅读源表,将此行添加到您的侦听器代码中:

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

一个示例监听方法:

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

【讨论】:

  • 正如我在问题中提到的,您的评论(可选)解决方案不起作用。我无法确定索引属于哪个列表。您的主要解决方案会引发 NullPointerException,因为 DataTable myDatatable = (DataTable) FacesContext.getCurrentInstance() .getViewRoot().findComponent(source); 返回 null。猜猜我已经尝试过了或类似的解决方案...
  • 所有显示的代码都应该可以顺利运行,否则在您的代码中某处存在一些隐藏的问题。在您的情况下,source 字符串变量的返回值是什么?
  • 我将数据表组件放在手风琴组件的选项卡中,这最终会改变行为吗?
  • 但是,返回值是多少?该值将为您打开根据源表进行操作的大门,无论它在组件树中的任何位置。
  • 看起来不错,它正是表格的第一个
    元素的 id。
    .... source = j_idt40:0:storiesTable
猜你喜欢
  • 2021-08-24
  • 2014-01-23
  • 2015-10-23
  • 1970-01-01
  • 2012-11-18
  • 1970-01-01
  • 1970-01-01
  • 2014-10-08
  • 2013-09-15
相关资源
最近更新 更多