我在spring控制器中通过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.");
}
}.
现在在UI中,我有复选框,我还提供了一次选择所有内容并删除的功能。
现在,当我全选并按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>
我发现,根复选框<label>Select All <input type="checkbox" id="ckbCheckAll" value="0">
的默认值也通过数组传递,因此我将其默认值设置为"0"
,因此我可以轻松跳过根复选框的值,但仍然可以成为问题。请给我最好的解决方案。
答案 0 :(得分:2)
由于您的方法提早返回,因此仅删除一条记录。若要解决此问题,请创建一个布尔变量以返回方法控件,而不是返回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;
}