欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Laravel框架實現(xiàn)的批量刪除功能示例

 更新時間:2019年01月16日 10:03:08   作者:snow_small  
這篇文章主要介紹了Laravel框架實現(xiàn)的批量刪除功能,結(jié)合實例形式分析了Laravel框架批量刪除功能相關(guān)的前端界面布局及后臺控制器實現(xiàn)技巧,需要的朋友可以參考下

本文實例講述了Laravel框架實現(xiàn)的批量刪除功能。分享給大家供大家參考,具體如下:

1、HTML的內(nèi)容

<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>  // 復(fù)選框
    </tr>
  @endforeach
  <a style="font-size: 15px;" id="delAll" type="button" class="btn btn-primary" onclick="delKeys()">批量刪除</a>
  </tbody>

2、js的內(nèi)容

// 全選
var ck = $('.ck');
function checkAll(qx)
{
  if (qx.checked) {
    for (var i=0; i<ck.length; i++) {     // 實現(xiàn)全選
      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都放進數(shù)組
    }
  }
  if (items == null || items.length == 0)    // 當(dāng)沒選的時候,不做任何操作
  {
    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、控制器中的內(nèi)容

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

更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家基于Laravel框架的PHP程序設(shè)計有所幫助。

相關(guān)文章

最新評論