如何使用支持 bean 的更新信息更新 dataTable 中的特定行
How to update specific row in dataTable with the updated information from backing bean
我需要您的帮助,以便在用户单击 "Update" 命令按钮后仅更新数据表中的特定行值,而不是更新具有特定值的整个数据表。
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
属性的描述是
Component(s) to be updated with 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");
}
我需要您的帮助,以便在用户单击 "Update" 命令按钮后仅更新数据表中的特定行值,而不是更新具有特定值的整个数据表。
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
属性的描述是
Component(s) to be updated with 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");
}