gpt4 book ai didi

java - 删除功能无法正常工作选择所有复选框

转载 作者:行者123 更新时间:2023-12-02 01:41:59 24 4
gpt4 key购买 nike

我通过 spring Controller 中的 ajax 调用传递了员工 ID 数组。

function deleteEntries() {

var empList = $('input[type=checkbox]:checked').map(function (_, el) {
return $(el).val();
}).get();

if (empList.length !== 0) {
var r = confirm("Are you sure want to remove multiple entries? \nWarning: This process cannot be undone");
if (r === true) {

$.ajax({
type: 'Post',
url: baseUrl + 'delete_all',
data: {
empList: empList

},
success: function (successMsg) {
location.reload();
},
fail: function (data) {
unblockMyScreen();
alert('Failed to retrieve data');
}
});


}
} else
{
alert("Choose atleast single record to delete.");
}
}.

现在在用户界面中,我有复选框,我还提供了通过一次选择全部并删除来删除的功能。

现在,当我选择全部并按删除按钮时,只会删除单个记录。但是,如果没有选择全部,它就可以正常工作

这是删除代码

  @RequestMapping(value = "/delete_all", method = RequestMethod.POST)
@ResponseBody
public boolean deleteMultipleRecord(@RequestParam(value = "empList[]", required = false) String[] empListToBeRemoved, HttpServletRequest request) {
// String[] empListToBeRemoved = request.getParameterValues("empList");
Employee emp = new Employee();
for (int i = 0; i <= empListToBeRemoved.length; i++) {
if (!empListToBeRemoved[i].equals("0")) {
emp.setEmpIdEnc(empListToBeRemoved[i]);
try {
List<OrgStructureTagging> list = orgStructureTaggingDAO.findEmpByProperty("EMP_ID", emp.getEmpId());
for (OrgStructureTagging structureTagging : list) {
System.out.println("all ids of employees" + structureTagging.getEmployee().getName());
orgStructureTaggingDAO.delete(structureTagging);
}
return true;
} catch (Exception e) {
e.printStackTrace();
log.error("Error Occured While updating the field");
return false;
}
}
}
return false;
}

这就是我的 JSP 代码的样子:

       <table> 
<thead>
<tr class="">
<th width="10%" >
<label>Select All <input type="checkbox" id="ckbCheckAll" value="0">
</label>
</th>
</thead>
<tbody>
<tr>
<td style="text-align: center">
<label> <input type="checkbox" class="checkBoxClass" value="${tl.employee.empIdEnc}">
</label>
</td>
</tr>
</tbody>

我发现,root 复选框的默认值 <label>Select All <input type="checkbox" id="ckbCheckAll" value="0">也是通过数组传递的,所以我将其默认值设置为"0" ,所以我可以轻松地跳过根复选框值,但仍然会出现问题。请建议我最好的解决方案。

最佳答案

由于您的方法提前返回,因此只有一条记录被删除。要解决此问题,请创建一个 boolean 变量来返回方法控制,而不是返回 true/false,同时将长度减 1 以避免 ArrayIndexOutOfBoundsException。这是可能对您有帮助的代码片段

@RequestMapping(value = "/delete_all", method = RequestMethod.POST)
@ResponseBody
public boolean deleteMultipleRecord(@RequestParam(value = "empList[]", required = false) String[] empListToBeRemoved, HttpServletRequest request) {
Employee emp = new Employee();
for (int i = 0; i <= empListToBeRemoved.length-1; i++) {
boolean result = false;
if (!empListToBeRemoved[i].equals("0")) {
emp.setEmpIdEnc(empListToBeRemoved[i]);
try {
List<OrgStructureTagging> list = orgStructureTaggingDAO.findEmpByProperty("EMP_ID", emp.getEmpId());
for (OrgStructureTagging structureTagging : list) {
System.out.println("all ids of employees" + structureTagging.getEmployee().getName());
orgStructureTaggingDAO.delete(structureTagging);
}
result = true;
} catch (Exception e) {
e.printStackTrace();
log.error("Error Occured While updating the field");
result = false;
}
}
}
return result;
}

关于java - 删除功能无法正常工作选择所有复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54340526/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com