删除功能无法正常工作 select 所有复选框
delete functionality not working properly select all checkboxes
我在 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,我有复选框,我还提供了通过 select 一次全部删除并删除的功能。
现在,当我 select 全部按下删除按钮时,只有一条记录会进入 delete.However,没有 [=41=,它工作正常]全部
这里是删除代码
@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"
,这样我就可以轻松地跳过根复选框的值,但是它仍然成为问题。请给我建议最好的解决方案。
由于您的方法提前返回,因此只删除了一条记录。要解决此问题,请创建一个布尔变量来返回方法控制而不是返回 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;
}
我在 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,我有复选框,我还提供了通过 select 一次全部删除并删除的功能。
现在,当我 select 全部按下删除按钮时,只有一条记录会进入 delete.However,没有 [=41=,它工作正常]全部
这里是删除代码
@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"
,这样我就可以轻松地跳过根复选框的值,但是它仍然成为问题。请给我建议最好的解决方案。
由于您的方法提前返回,因此只删除了一条记录。要解决此问题,请创建一个布尔变量来返回方法控制而不是返回 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;
}