批量删除ajax+java

function del(){
        var selected=$('#dg').datagrid('getSelections');
        if(selected.length>0){
            $.messager.confirm('请确认', '你确定要删除所选的记录吗?', function(n){
                if(n){
                    var ids = [];
                    for(var i = 0;i<selected.length;i++){
                        ids.push(selected[i].id);
                    }
                    $.ajax({
                        url:'indexMenu/deleteMenu.do',
                        dataType:'json',
                        data:{ids:ids},
                        success:function(data){
                            if(data.code=='1'){
                                $.messager.alert('提示消息','删除失败','info');
                            }else {
                                $('#dg').datagrid('reload');
                                $.messager.alert('提示消息','删除成功','info');
                            }
                        }
                    });
                }
            })
        }else{
            $.messager.alert('错误消息','你未有选择要删除的记录','error');
        }
    }
Controller---------------------------------    
@RequestMapping("/deleteMenu")
    @ResponseBody
    public Map<String, Object> deleteMenu(@RequestParam(value = "ids[]") Long[] ids) {
        Map<String, Object> resMap = new HashMap<String, Object>();
        try {
            if (null != ids && ids.length > 0) {
                indexMenuService.deleteMenu(ids);
                resMap.put(CardGodConstant.MAP_KEY_MSG, CardGodConstant.MAP_VALUE_SUCCESS);
                resMap.put(CardGodConstant.MAP_KEY_CODE, CardGodConstant.STR_VALUE_0);
            }
            return resMap;
        } catch (Exception e) {
            logger.error(CardGodConstant.ERROR_MSG + e);
            e.printStackTrace();
            resMap.put(CardGodConstant.MAP_KEY_MSG, CardGodConstant.MAP_VALUE_FAILURE);
            resMap.put(CardGodConstant.MAP_KEY_CODE, CardGodConstant.STR_VALUE_1);
            return resMap;
        }
    }

ServiceImpl------------------------

 public void deleteMenu(Long []ids) {
        for (int i = 0; i < ids.length; i++) {
            indexMenuMapper.deleteByPrimaryKey(ids[i]);
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_40727252/article/details/80626808