Home Backend Development PHP Tutorial 基于ThinkPHP实现批量删除_php技巧

基于ThinkPHP实现批量删除_php技巧

May 16, 2016 pm 08:02 PM
thinkphp batch deletion

本文实例分析了基于ThinkPHP实现批量删除的代码实例,分享给大家供大家参考,具体如下:

废话不多说,先上效果图:


HTML布局(基于bootstrap)

<div class="panel panel-default">
  <div 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>
  </div>
  <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>
</div>
Copy after login

JS脚本处理(使用ajax技术)
首先判断有没有选中的值,如果没有则提示;如果有,则传递到服务器端处理

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

  // 删除操作
  $('#discard').click(function() {
    if($(':checked').size() > 0) {
      layer.confirm('确定要删除吗?', {
        btn: ['确定','取消'], //按钮
        shade: false //不显示遮罩
      }, function(){
        $.post("{:U('Single/discard')}", {data: $('form').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('取消了删除!', {time: 1000});
      });
    } else {
      layer.alert('没有选择!');
    }
  });

Copy after login

PHP代码:
获取提交的数据,然后循环得到每一个id的值,接着进行删除操作。

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

以上就是ThinkPHP实现批量删除的关键性代码,希望对大家的学习有所帮助。

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to delete Weibo posts in batches? -How to cancel your Weibo account? How to delete Weibo posts in batches? -How to cancel your Weibo account? Mar 18, 2024 pm 03:00 PM

How to delete Weibo posts in batches? -How to cancel your Weibo account?

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

How to run thinkphp project

Various ways to implement batch deletion operations in MyBatis Various ways to implement batch deletion operations in MyBatis Feb 19, 2024 pm 07:31 PM

Various ways to implement batch deletion operations in MyBatis

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

There are several versions of thinkphp

Detailed explanation of how to use MyBatis batch delete statements Detailed explanation of how to use MyBatis batch delete statements Feb 20, 2024 am 08:31 AM

Detailed explanation of how to use MyBatis batch delete statements

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

How to run thinkphp

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

How to install thinkphp

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Which one is better, laravel or thinkphp?

See all articles