为了账号安全,请及时绑定邮箱和手机立即绑定

删除功能无法正常工作选中所有复选框

删除功能无法正常工作选中所有复选框

一只萌萌小番薯 2022-05-21 17:10:28
我在我的 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 中,我有复选框,我还提供了通过一次选择全部并删除来删除的功能。现在,当我全选并按下删除按钮时,只会删除单个记录。但是,如果不全选,它工作正常
查看完整描述

1 回答

?
慕森卡

TA贡献1806条经验 获得超8个赞

由于您的方法提前返回,因此只有一条记录被删除。要解决此问题,请创建一个布尔变量以返回方法控制,而不是返回 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;

}


查看完整回答
反对 回复 2022-05-21
  • 1 回答
  • 0 关注
  • 78 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号