首页 > 语言 > PHP > 正文

Laravel框架实现的批量删除功能示例

2024-05-05 00:06:13
字体:
来源:转载
供稿:网友

本文实例讲述了Laravel框架实现的批量删除功能。分享给大家供大家参考,具体如下:

1、HTML的内容

<tr>    <th><input type="checkbox" class="checkbox-inline" onclick="checkAll(this)"></th>  // 用来全选  </tr>  </thead>  <tbody>  @foreach ($keys as $key)    <tr>      <td><input type="checkbox" class="ck checkbox-inline" name="item[]" value="{{ $key->id }}"></td>  // 复选框    </tr>  @endforeach  <a style="font-size: 15px;" id="delAll" type="button" class="btn btn-primary" onclick="delKeys()">批量删除</a>  </tbody>

2、js的内容

// 全选var ck = $('.ck');function checkAll(qx){  if (qx.checked) {    for (var i=0; i<ck.length; i++) {     // 实现全选      ck[i].setAttribute("checked", "checked");    }  } else {    for (var i=0; i<ck.length; i++) {     // 取消全选      ck[i].removeAttribute("checked");    }  }}// 批量删除function delKeys(){  var items = [];  for (var i=0; i<ck.length; i++) {    if (ck[i].checked) {      items.push(ck[i].value);    // 将id都放进数组    }  }  if (items == null || items.length == 0)    // 当没选的时候,不做任何操作  {    return false;  }  layer.confirm('您确定要删除我们吗?', {    btn: ['确定', '取消'],  }, function() {    $.post("{{ url('key/delAll') }}", {      "_token": "{{ csrf_token() }}",      "keys": items    }, function(data) {      if (data.status == 0) {        layer.msg(data.msg, { icon: 6});        location.href = location.href;      } else {        layer.msg(data.msg, { icon: 5});      }    });  }, function() {});

3、控制器中的内容

public function delAll(Request $request){     for ($i=0; $i<count($request['keys']); $i++) {       $res = Key::where('id', $request['keys'][$i])->update(['isDelete' => 1]);  // 遍历删除     }     if ($res) {       $data = [         'status' => 0,         'msg' => '删除成功'       ];     } else {       $data = [         'status' => 1,         'msg' => '删除失败'       ];     }     return $data;}

希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。


注:相关教程知识阅读请移步到PHP教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选