本文介绍了如何使用备份bean更新的信息更新dataTable中的特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ p

我需要您的帮助,只有在用户点击更新命令按钮时,才更新dataTable中的特定行值,而不是具有特定值的整个dataTable。 dataTable看起来像:

 < p:dataTable value =#{testController.employeeList}id =Employeevar =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 =备注/>
< p:column headerText =更新/>
< / 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 =备注>
< h:inputText id =inputT1value =#{emp.remarks}/>
< / p:column>
< p:column headerText =Update>
< p:commandButton id =updateCmdtitle =Update
actionListener =#{testController.updateRecord}>
< / p:commandButton>
< / p:column>
< / p:dataTable>

这里是方法代码:

  public void updateRecord(ActionEvent actionEvent){
FacesContext context = FacesContext.getCurrentInstance();
员工selectedRecord = context.getApplication()。evaluateExpressionGet(context,#{emp},Employee.class);
selectedRecord.setRemarks(Updated);
String name1 = selectedRecord.getRemarks();
System.out.println(name1+ name1);
//我需要更新行或单元格更新为
//不刷新完整的dataTable
//RequestContext.getCurrentInstance().update(\"request:Employee );
//以上行将更新完整表,我只需要行或单元格值
}


解决方案

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



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

所以您可以通过组件标识更新您的特定列。 / p>

 < p:dataTable> 
...
< p:column headerText =Name>
< h:outputText value =#{emp.name}id =name/>
< / p:column>
< p:column headerText =备注>
< h:inputText id =inputT1value =#{emp.remarks}/>
< / p:column>
< p:commandButton id =updateCmdtitle =Updateupdate =name inputT1partialSubmit =trueactionListener =#{testController.updateRecord}>
< / p:commandButton>
< / p:dataTable>

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

 < h:form id =myform> 
< p:dataTable value =#{testController.employeeList}id =Employee
var =empwidgetVar =employeeTablerowIndexVar =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 =备注>
< p:inputText id =inputT1value =#{emp.remarks}/>
< / p:column>
< p:column headerText =Update>

< p:commandButton id =updateCmdtitle =更新partialSubmit =true
actionListener =#{testController.updateRecord}>
< f:attribute name =rowIdvalue =#{row}/>
< / p:commandButton>
< / p:column>
< / p:dataTable>


ActionListner方法:

  public void updateRecord(ActionEvent actionEvent){
Integer rowId =(Integer)actionEvent.getComponent()。getAttributes() (rowId);
FacesContext context = FacesContext.getCurrentInstance();
员工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);
}


I need your assistance in updating only a specific row values in a dataTable not the whole dataTable having particular values once the user click on the "Update" commandButton.

The dataTable looks like:

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

And here is the method code:

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)You can use update attribute of commandButton to update the components with ajax

From Primefaces documentation the description for update attribute is

So you can update your specific column by the components 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) If you want to update the particular column using Java then you need to pass the rowId to the 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 method:

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

这篇关于如何使用备份bean更新的信息更新dataTable中的特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 07:02