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

标签 java spring spring-mvc jsp jstl

我通过 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/

相关文章:

java - 尝试使用 wordnet java api 查找同义词

mysql - 嵌套对象插入需要时间 -Spring Data JPA、Hibernate 和 MySQL

forms - 避免 spring <form :input path ="var"/> for int variable 中的默认值

java - 由于 StackOverflowError,无法完成对 Web 应用程序 [/app] 注释的扫描

java - 自动生成时间戳

java - 为什么我不能替换 ":)"

java - 使用jsp的displaytag迭代对象列表

java - 无法从类型 [java.lang.Object[]] 转换为类型

java - Spring Data Cassandra 连接问题

java - 如何在Spring Security xml中设置响应头?