【问题标题】:How to update specific row in dataTable with the updated information from backing bean如何使用来自支持 bean 的更新信息更新 dataTable 中的特定行
【发布时间】:2016-09-09 13:19:36
【问题描述】:

一旦用户单击“更新”命令按钮,我需要您的帮助来更新数据表中的特定行值,而不是具有特定值的整个数据表。

数据表如下所示:

    <p:dataTable value="#{testController.employeeList}" id="Employee" var="emp" 
rowIndexVar="rowIndex"
    selection="#{testController.selectedEmployees}" rowKey="#{emp.id}" 
rowSelectMode="checkbox">
                        <p:columnGroup type="header">
                        <p:row>
                            <p:column/>
                            <p:column headerText="ID"/>
                            <p:column headerText="Name"/>
                            <p:column headerText="Remarks"/>
                            <p:column headerText="Update"/>
                        </p:row>
                    </p:columnGroup>
                    <p:column selectionMode="multiple"/>
                    <p:column headerText="ID">
                        <h:outputText value="#{emp.id}"/>
                    </p:column>
                    <p:column headerText="Name">
                        <h:outputText value="#{emp.name}" id="name"/>
                    </p:column>
                    <p:column headerText="Remarks">
                        <h:inputText id="inputT1" value="#{emp.remarks}"/>
                    </p:column>
                    <p:column headerText="Update">
                        <p:commandButton id="updateCmd" title="Update"
                                         actionListener="#{testController.updateRecord}">
                        </p:commandButton>
                    </p:column>
</p:dataTable>

这里是方法代码:

public void updateRecord(ActionEvent actionEvent) {
    FacesContext context = FacesContext.getCurrentInstance();
    Employee selectedRecord = context.getApplication().evaluateExpressionGet(context, "#{emp}", Employee.class);
    selectedRecord.setRemarks("Updated");
    String name1=selectedRecord.getRemarks();
    System.out.println("name1"+name1);
        // I need to update the row only or the cell with the remarks Updated by
 //without refreshing full dataTable
    //RequestContext.getCurrentInstance().update("request:Employee"); 
//Above line will update the full table, I need only the row or the cell value
    }

【问题讨论】:

  • @Kukeltje 它没有显示答案
  • 在您的xhtml页面中,我可以看到只有一个输入字段备注。但是在您的更新方法中,您正在更新名称。你想达到什么目的?
  • @Unknown 我已经修改了代码,我只想更新该行或单元格的备注“刷新”。
  • @Unknown 感谢您的解决方案...我会在明天之前尝试一下,我会及时通知您

标签: ajax jsf jsf-2 primefaces datatable


【解决方案1】:

1)你可以使用commandButton的update属性来用ajax更新组件

从 Primefaces 文档中,update 属性的描述是

要使用 ajax 更新的组件。

因此您可以通过组件 ID 更新您的特定列。

<p:dataTable>
...
<p:column headerText="Name">
        <h:outputText value="#{emp.name}" id="name" />
</p:column>
<p:column headerText="Remarks">
     <h:inputText id="inputT1" value="#{emp.remarks}"/>
</p:column>
<p:commandButton id="updateCmd" title="Update" update="name inputT1" partialSubmit="true"   actionListener="#{testController.updateRecord}">
</p:commandButton>
</p:dataTable>

2) 如果要使用 Java 更新特定列,则需要将 rowId 传递给控制器​​。

<h:form id="myform">
        <p:dataTable value="#{testController.employeeList}" id="Employee"
                    var="emp" widgetVar="employeeTable" rowIndexVar="row"
                    filteredValue="#{testController.filteredEmployees}"
                    rowKey="#{emp.id}">
                    <p:column headerText="Id">
                        <h:outputText value="#{emp.id}" />
                    </p:column>
                    <p:column headerText="Name">
                        <h:outputText value="#{emp.name}" id="name" />
                    </p:column>
                    <p:column headerText="Remarks">
                        <p:inputText id="inputT1" value="#{emp.remarks}"/>
                    </p:column>
                    <p:column headerText="Update" >

                    <p:commandButton id="updateCmd" title="Update" partialSubmit="true"
                                     actionListener="#{testController.updateRecord}">
                            <f:attribute name="rowId" value="#{row}" />
                     </p:commandButton>
                     </p:column>
      </p:dataTable>

ActionListner 方法:

public void updateRecord(ActionEvent actionEvent) {
        Integer rowId = (Integer)actionEvent.getComponent().getAttributes().get("rowId");
        FacesContext context = FacesContext.getCurrentInstance();
        Employee selectedRecord = context.getApplication().evaluateExpressionGet(context, "#{emp}", Employee.class);
        selectedRecord.setRemarks("Updated");
        String name1=selectedRecord.getRemarks();
        System.out.println("name1"+name1);
      RequestContext.getCurrentInstance().update("myform:Employee:"+rowId+":inputT1"); 
        }

【讨论】:

    猜你喜欢
    • 2013-03-17
    • 2015-02-22
    • 2013-04-12
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2019-08-22
    • 2018-03-24
    • 2013-01-17
    相关资源
    最近更新 更多