Home  >  Article  >  Backend Development  >  ThinkPHP implements batch deletion

ThinkPHP implements batch deletion

不言
不言Original
2018-06-08 15:38:571753browse

This article mainly introduces the code examples to implement batch deletion based on ThinkPHP. It has certain reference value. Interested friends can refer to it.

This article provides an example analysis of the code to implement batch deletion based on ThinkPHP. Examples, shared with everyone for your reference, are as follows:

Without further ado, here are the renderings:


##HTML Layout (based on bootstrap)

<p class="panel panel-default">
  <p class="panel-heading">
    留言列表
    <a class="btn btn-xs btn-default pull-right" href="javascript:window.history.back();">返回</a>
    <a class="btn btn-xs btn-info pull-right mr-5" id="discard" href="javascript:;">删除</a>
  </p>
  <table class="table">
    <thead>
      <tr>
        <th><input class="all" type="checkbox"/></th>
        <th>id</th>
        <th>名称</th>
        <th>邮箱</th>
        <th>内容</th>
        <th>日期时间</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
       <form>
         <volist name="list" id="vo" empty="$empty">
           <tr>
             <td><input name="delete[]" type="checkbox" value="{$vo.id}" /></td>
             <td>{$vo.id}</td>
             <td>{$vo.name}</td>
             <td>{$vo.email}</td>
             <td>{$vo.subject}</td>
             <td>{$vo.datetime|date="Y-m-d H:i", ###}</td>
           <td>
            <a class="delete" href="javascript:;" data-id="{$vo.id}">删除</a>
           </td>
           </tr>
         </volist>
       </form>
    </tbody>
  </table>
</p>

JS script processing (using ajax technology)
First determine whether There is no selected value. If there is no value, it will prompt; if there is, it will be passed to the server for processing

/* 批量删除 */
  // 全选
  $(&#39;.all&#39;).click(function() {
    if($(this).is(&#39;:checked&#39;)) {
      $(&#39;:checkbox&#39;).attr(&#39;checked&#39;, &#39;checked&#39;);
    } else {
      $(&#39;:checkbox&#39;).removeAttr(&#39;checked&#39;);
    }
  });

  // 删除操作
  $(&#39;#discard&#39;).click(function() {
    if($(&#39;:checked&#39;).size() > 0) {
      layer.confirm(&#39;确定要删除吗?&#39;, {
        btn: [&#39;确定&#39;,&#39;取消&#39;], //按钮
        shade: false //不显示遮罩
      }, function(){
        $.post("{:U(&#39;Single/discard&#39;)}", {data: $(&#39;form&#39;).serializeArray()}, function(res) {
          if(res.state == 1) {
            layer.msg(res.message, {icon: 1, time: 1000});
          } else {
            layer.msg(res.message, {icon: 2, time: 1000});
          }
          setTimeout(function() {
            location.reload();
          }, 1000);
        });
      }, function(){
        layer.msg(&#39;取消了删除!&#39;, {time: 1000});
      });
    } else {
      layer.alert(&#39;没有选择!&#39;);
    }
  });

PHP code:
Get the submitted data, then loop to get the value of each id, and then perform the delete operation.

public function discard() {
  $contact = M(&#39;contact&#39;);
  $deleteArr = I(&#39;post.data&#39;);
  for($i=0;$i<count($deleteArr);$i++) {
    $contact->delete($deleteArr[$i][&#39;value&#39;]);
  }
  $this->ajaxReturn(array(&#39;message&#39;=>&#39;删除成功!&#39;));
}

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About thinkPHP’s method of implementing batch deletion

Analysis on thinkphp framework’s implementation of deletion and batch deletion

About the functional methods of adding and displaying data in the thinkphp framework

##

The above is the detailed content of ThinkPHP implements batch deletion. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn