如何更新 Primefaces 数据表中的特定行 [英] How to update specific row in Primefaces datatable

查看:34
本文介绍了如何更新 Primefaces 数据表中的特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Primefaces 3.5,Mojarra 2.1.14.这是我的 PF 数据表,它包含一个名为自动"的不可编辑布尔列和可编辑的标签"列:

Primefaces 3.5, Mojarra 2.1.14. This is my PF datatable, it contains one non-editable boolean column named 'automatic', and editable 'label' column:

<p:dataTable value="#{bean.contents}" paginator="true" var="row" 
  editable="true" editMode="cell" rows="25" rowsPerPageTemplate="10,25,50" id="list">
    <p:column>
        <f:facet name="header"><h:outputText value="header1" /></f:facet>
        <p:selectBooleanCheckbox value="#{row.automatic}" disabled="true" id="isAutomatic"></p:selectBooleanCheckbox>
    </p:column>
    <p:column>
        <f:facet name="header"><h:outputText value="header2" /></f:facet>
        <p:cellEditor>
            <f:facet name="output">
                <h:outputText value="#{row.label}"></h:outputText>
            </f:facet>
            <f:facet name="input">
                <p:inputText value="#{row.label}"></p:inputText>
            </f:facet>
        </p:cellEditor>
    </p:column>
    <p:ajax event="cellEdit" process="@this" listener="#{myBean.onEditLabel}" update="isAutomatic"/>
</p:dataTable>

单元格编辑事件监听器:

Cell edit event listener:

public void onEditLabel(CellEditEvent event) {
    Object oldValue = event.getOldValue();
    Object newValue = event.getNewValue();

    if(newValue != null && !newValue.equals(oldValue)) {
        DataTable s = (DataTable) event.getSource();
        MyEntity d = (MyEntity) s.getRowData();
        try {
            d.setAutomatic(false);
            myDAO.save(d);
            addMessage("Change saved!");
        } catch (Exception ex) {
            addErrorMessage("Label could not be saved!");
            getFacesContext().validationFailed();
        }
    }
}   

单元格编辑器工作,将数据发送到侦听器,并正确地持久化到数据库中.自动"标志也由单元格编辑事件侦听器清除,并正确持久保存到数据库中.问题是客户端上的自动"复选框未更新.

The cell editor works, sends data to listener, and it is correctly persisted to the database. The 'automatic' flag is also cleared by cell edit event listener, and gets correctly persisted to the database. The problem is that 'automatic' checkbox is not updated on the client.

我也试过

    <p:ajax event="cellEdit" process="@this" listener="#{myBean.onEditLabel}" update="list"/>

正确更新了复选框,但也会导致失去焦点,浪费带宽.

which correctly updated the checkbox, but also causes focus to be lost, and wastes bandwidth.

如何在 cellEdit 事件触发后仅更新特定单元格?

How can I update just a specific cell after cellEdit event is fired?

推荐答案

你的 p:ajax 标签在 p:dataTable 内没有在某些特定的行或列中,所以你很容易更新一些相关的组件 id.您可以借助 RequestContext 更新单元格中的特定组件.因此,从 p:ajax 中删除 update 并将其添加到您的 onEditLabel 方法中:

You p:ajax tag is inside p:dataTable no in some specific row or column, so you cent so easy update some relative component id. You can with help of RequestContext update specific component in cell. So, remove update from p:ajax and add this to your onEditLabel method:

RequestContext.getCurrentInstance().update(
  s.getClientId(FacesContext.getCurrentInstance()) +
  ":" + event.getRowIndex() +
  ":isAutomatic"
);

如您所见,单元格内组件的 id 在您分配的 id 之前具有行号.

As you can see, id of component inside cell has row number before id you assigned.

这篇关于如何更新 Primefaces 数据表中的特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆