如何将inputText的值重置为rowUnselectCheckbox上的原始值(How to reset the value of an inputText to the original val

编程入门 行业动态 更新时间:2024-10-18 10:27:45
如何将inputText的值重置为rowUnselectCheckbox上的原始值(How to reset the value of an inputText to the original value on rowUnselectCheckbox)

我需要你的帮助,将inputText中的值重置为原始值,一旦取消选中该行的复选框,就会在页面加载中检索该值。 在这种情况下,我有一个dataTable,一旦用户点击行中的复选框,inputText将被启用并可编辑。 如果用户取消选中复选框,我想撤消inputText中添加的文本。 在当前情况下,如果用户取消勾选该行,则添加的字符将保留在inputText中。

这是dataTable的代码:

<p:dataTable value="#{testController.employeeList}" id="Employee" var="emp" selection="#{testController.selectedEmployees}" rowKey="#{emp.id}" emptyMessage="No Employees found."> <p:ajax event="rowSelectCheckbox" listener="#{testController.EnableInputText}" update="Employee"/> <p:ajax event="rowUnselectCheckbox" listener="#{testController.onRowUnselect}" update="Employee"/> <p:columnGroup type="header"> <p:row> <p:column/> <p:column headerText="ID"/> <p:column headerText="Name"/> <p:column headerText="Remarks"/> <p:column headerText="Delete"/> </p:row> </p:columnGroup> <p:column selectionMode="multiple" style="width:2%;text-align:center"/> <p:column headerText="ID"> <h:outputText value="#{emp.id}"/> </p:column> <p:column headerText="Name"> <h:outputText value="#{emp.name}"/> </p:column> <p:column headerText="Remarks"> <h:inputText id="inputT1" value="#{emp.remarks}" disabled="#{emp.disable}"/> </p:column> </p:dataTable>

和支持bean中的代码:

private List<Employee> employeeList; private List<Employee> selectedEmployees; public void EnableInputText(SelectEvent event) { for (int i = 0; i < selectedEmployees.size(); i++) { for (int j = 0; j < employeeList.size(); j++) { if (selectedEmployees.get(i).getId().equals(employeeList.get(j).getId())) { employeeList.get(j).setDisable(false); } } } } public void onRowUnselect(UnselectEvent unselectEvent) { Employee emp = (Employee)unselectEvent.getObject(); for (Employee e : employeeList) { if (emp.getId().equals(e.getId())) { e.setDisable(true); } } }

假设inputText中的值为Test,并且用户勾选了行中的复选框并将其编辑到Test555,然后在复选框的untick上,inputText值应该返回Test

I need your help in resetting the value in an inputText to its original value which is being retrieved on the page load once the checkbox of the row is unticked. In this case, I am having a dataTable and once the user clicks on the checkbox in the row, the inputText will be enabled and editable. I would like to do undo to the added text in the inputText if the user unticked the checkbox. In the current case, if the user unticked the row, then the added characters will remain in the inputText.

Here is the code for the dataTable:

<p:dataTable value="#{testController.employeeList}" id="Employee" var="emp" selection="#{testController.selectedEmployees}" rowKey="#{emp.id}" emptyMessage="No Employees found."> <p:ajax event="rowSelectCheckbox" listener="#{testController.EnableInputText}" update="Employee"/> <p:ajax event="rowUnselectCheckbox" listener="#{testController.onRowUnselect}" update="Employee"/> <p:columnGroup type="header"> <p:row> <p:column/> <p:column headerText="ID"/> <p:column headerText="Name"/> <p:column headerText="Remarks"/> <p:column headerText="Delete"/> </p:row> </p:columnGroup> <p:column selectionMode="multiple" style="width:2%;text-align:center"/> <p:column headerText="ID"> <h:outputText value="#{emp.id}"/> </p:column> <p:column headerText="Name"> <h:outputText value="#{emp.name}"/> </p:column> <p:column headerText="Remarks"> <h:inputText id="inputT1" value="#{emp.remarks}" disabled="#{emp.disable}"/> </p:column> </p:dataTable>

And the code in the backing bean:

private List<Employee> employeeList; private List<Employee> selectedEmployees; public void EnableInputText(SelectEvent event) { for (int i = 0; i < selectedEmployees.size(); i++) { for (int j = 0; j < employeeList.size(); j++) { if (selectedEmployees.get(i).getId().equals(employeeList.get(j).getId())) { employeeList.get(j).setDisable(false); } } } } public void onRowUnselect(UnselectEvent unselectEvent) { Employee emp = (Employee)unselectEvent.getObject(); for (Employee e : employeeList) { if (emp.getId().equals(e.getId())) { e.setDisable(true); } } }

Suppose the value in the inputText is Test and the user ticked the checkbox in the row and editted it to Test555 then on the untick of the checkbox, the inputText value should return back to Test

最满意答案

这看起来很容易。

您只需要使用另一个变量oldRemarks 。 在员工列表初始化期间设置oldRemarks =备注。

public Employee(Integer id,String name,String remarks,Boolean disabled){ super(); this.id = id; this.name = name; this.remarks = remarks; this.disabled = disabled; this.oldRemarks = remarks; }

在你的UnselectEvent中

public void onRowUnselect(UnselectEvent unselectEvent){ Employee emp = (Employee)unselectEvent.getObject(); for (Employee e : employeeList) { if (emp.getId().equals(e.getId())) { e.setDisabled(true); e.setRemarks(e.getOldRemarks()); } } }

This looks to be easy.

You just need to use another variable oldRemarks. During the initialization of the employee list set the oldRemarks=remarks.

public Employee(Integer id,String name,String remarks,Boolean disabled){ super(); this.id = id; this.name = name; this.remarks = remarks; this.disabled = disabled; this.oldRemarks = remarks; }

And in your UnselectEvent

public void onRowUnselect(UnselectEvent unselectEvent){ Employee emp = (Employee)unselectEvent.getObject(); for (Employee e : employeeList) { if (emp.getId().equals(e.getId())) { e.setDisabled(true); e.setRemarks(e.getOldRemarks()); } } }

更多推荐

inputText,<p,value,电脑培训,计算机培训,IT培训"/> <meta name="descrip

本文发布于:2023-07-25 05:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1256819.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   原始   inputText   rowUnselectCheckbox   reset

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!