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

时间:2016-05-13 11:03:19

标签: ajax jsf jsf-2 primefaces datatable

一旦用户点击“更新”命令按钮,我需要您的帮助,只更新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 个答案:

答案 0 :(得分:0)

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