laravel 8 批量删除 全选反选 批量恢复 批量修改

laravel 8 批量删除 全选反选 批量恢复 批量修改,第1张

前端部分页面

全选 反选

//全选
$("#ck").click(function () {
    var ck = $("input[name='ck[]']");
    ck.each(function (i, e) {
        $(e).attr('checked', !$(e).attr('checked'));
    })
})

批量删除

//前端

//批量删除
$("#delAll").click(function () {
    var ck = $("input[name='ck[]']:checked");
    var arr = [];
    ck.each(function () {
        var id = $(this).val();
        //将选中的id值存入数组
        arr.push(id)
    })
        //将数组转化为字符串
    var ids = arr.join(',');
        //ajax提交
    $.ajax({
        url: "{{route('delAll')}}",//路由
        data: {
            id: ids,
            _token: "{{csrf_token()}}",
        },
        success: function (res) {
            alert(res.msg);
                //循环删除
            ck.each(function () {
                $(this).parents('tr').remove();
                   //刷新
                location.reload();
            })
        }
    })
})
//后台  返回值可做封装

/**
 * 批量删除
 * @param Request $request
 * @return \Illuminate\Http\JsonResponse
 */
public function delAll(Request $request)
{
        //将数据转化为数组
    $ids = explode(',', $request->get('id'));
        //批量软删除
    $res = Good::destroy($ids);
        //返回数据
    if ($res) {
        return response()->json([
            'code' => 200,
            'msg' => '删除成功',
            'data' => ''
        ]);
    } else {
        return response()->json([
            'code' => 2001,
            'msg' => '删除失败',
            'data' => ''
        ]);
    }
}

批量恢复

//前端 

//批量恢复
$("#resAll").click(function () {
    var ck = $("input[name='ck[]']:checked");
    var arr = [];
    ck.each(function () {
        var id = $(this).val();
        arr.push(id)
    })
    var ids = arr.join(',');
    $.ajax({
        url: "{{route('resAll')}}",
        data: {
            id: ids,
            _token: "{{csrf_token()}}",
        },
        success: function (res) {
            alert(res.msg);
            ck.each(function () {
                $(this).parents('tr').remove();
                location.reload();
            })
        }
    })
//后端  返回值可做封装

/**
 * 批量恢复
 * @param Request $request
 * @return \Illuminate\Http\JsonResponse
 */
public function resAll(Request $request)
{
    $ids = explode(',', $request->get('id'));
    foreach ($ids as $id) {
        $res = Good::where('id', $id)->restore();
    }
    if ($res) {
        return response()->json([
            'code' => 200,
            'msg' => '恢复成功',
            'data' => ''
        ]);
    } else {
        return response()->json([
            'code' => 2001,
            'msg' => '恢复失败',
            'data' => ''
        ]);
    }
}

批量修改状态

//前端

//批量修改上下架
$("#update").click(function () {
    var ck = $("input[name='ck[]']:checked");
    var arr = [];
    ck.each(function () {
        var id = $(this).val();
        arr.push(id)
    })
    var ids = arr.join(',');
    $.ajax({
        url: "{{route('update')}}",
        data: {
            id: ids,
            _token: "{{csrf_token()}}",
        },
        dataType: "json",
        success: function (res) {
            alert(res.msg);
            location.reload();
        }
    })
})
//后端 返回值可做封装

/**
 * 批量修改
 * @param Request $request
 */
public function update(Request $request)
{
    $ids = explode(',', $request->get('id'));

    foreach ($ids as $id) {
        $status = Good::where('id', $id)->first()->status;
        if ($status == 0) {
            $res = Good::where('id', $id)->update(['status' => 1]);
        } else {
            $res = Good::where('id', $id)->update(['status' => 0]);
        }

    }
    if ($res) {
        return response()->json([
            'code' => 200,
            'msg' => '修改成功',
            'data' => ''
        ]);
    } else {
        return response()->json([
            'code' => 2001,
            'msg' => '修改失败',
            'data' => ''
        ]);
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1320999.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-11
下一篇 2022-06-11

发表评论

登录后才能评论

评论列表(0条)

保存