简体   繁体   中英

delete functionality not working properly select all checkboxes

I passed an array of employee Id via ajax call in my spring controller.

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.");
                }
            }.

Now in UI, I have check boxes, also I provide the functionality to delete by selecting all at once and delete.

Now When I select all and press delete button then only single record is going to delete.However, It is working fine without select all

Here is the delete code

  @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;
    }

THIS IS HOW MY JSP CODE LOOKS LIKE:

       <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>

What I found, a default value of root checkbox <label>Select All <input type="checkbox" id="ckbCheckAll" value="0"> is also passed via array, so I set its default value as "0" , so i can easily skip the root checkbox value, but still it become problematic. kindly suggest me the best solution.

Only one record is getting deleted since your method is returning early. To fix this issue, create a boolean variable for returning the method control instead of returning true/false also decrement 1 from length to avoid ArrayIndexOutOfBoundsException. Here is the code snippet that might help you

@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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM