如何使用来自backingbean的更新信息更新dataTable中的特定行



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

数据表看起来像:

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

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传递给Controller。

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

最新更新