ThinkPHP中ajax实现批量删除,,以…

本文介绍了在ThinkPHP中使用Ajax实现批量删除信息的方法。通过HTML表格展示数据,包括ID、标题、状态等字段,使用全选功能和单个选择配合Ajax发送请求,后台PHP接收并处理删除操作。在控制器中通过循环删除选中的记录,并返回相应的提示信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 利用ajax批量删除信息删除
  2. <table class="table table-border table-bordered table-bg table-hover  table-responsive" >
          <thead>
             <tr class="text-c">
                <th width="25"><input type="checkbox" name="" value=""></th>
                <th width="40">排序</th>
                <th>ID</th>
                <th width="200">标题</th>
                <th>文章预览审核</th>
                <th >所属地区</th>
                <th >添加时间</th>
                <th >添加者</th>
                <th >操作</th>
             </tr>
          </thead>
          <tbody>
          <foreach name="newlist" item="vo">
             <tr class="text-c va-m check-tr">
                <td><input type="checkbox" value="{$vo.id}" name=""></td>
                <td> <input type="text"  class="input-text" onchange="changeOrder(this,'{$vo.id}')" value="{$vo.sort_id}"></td>
                <td>{$vo.id}</td>
                <td>{$vo.title|mb_substr=0,15,'utf-8'}...</td>
                 <th class="td-status">
                   <switch name="vo['status']" >
                      <case value="0"><a  style="text-decoration:none"  href="{:U('Local/local_view',array('id'=>$vo['id']))}"  title='文章预览审核'>审核并查看详情</a></case>
                      <case value="1"><a  style="text-decoration:none"  href="{:U('Local/local_view',array('id'=>$vo['id']))}"  title='已经审核通过'><span class="label label-success radius">审核通过</span></a></case>
                      <case value="2"><a  style="text-decoration:none"  href="{:U('Local/local_view',array('id'=>$vo['id']))}"  title='审核未通过'><span class="label label-danger radius">审核未通过</span></a>
                         <a  style="text-decoration:none" onclick="replay_msg('回复消息','__CONTROLLER__/replay/id/{$vo.id}',500,380)" href="javascript:;" title='审核未通过'><span class="label label-danger radius">回复消息</span></a></case>
                      <case value="3"><a  style="text-decoration:none"  href="{:U('Local/local_view',array('id'=>$vo['id']))}"  title='审核未通过'><span class="label label-danger radius">审核未通过</span></a></case>
                   </switch>
                </th>
                 <td>{$vo.local_name}</td>
                 <td>{:date('Y-m-d',$vo['add_time'])}</td>
                 <td>{$vo.author}</td>
                <td class="f-14 td-manage">
                    <a style="text-decoration:none" class="ml-5"
                   href="{:U('Local/local_edit',array('id'=>$vo['id']))}" title="编辑">
                   <i class="Hui-iconfont">&#xe6df;</i></a> <a style="text-decoration:none" class="ml-5"
                   onClick="local_del(this,'__CONTROLLER__/local_del','{$vo.id}')"title="删除">
                   <i class="Hui-iconfont">&#xe6e2;</i>
                   </a>
                </td>
             </tr>
             </foreach>
          </tbody>
       <tfoot>
       <!--分页显示?-->
       <tr><td textalign="center" cl nowrap="true" colspan="9" height="20"><div class="pages"><?php echo $page; ?></div></td></tr>
       </tfoot>
       </table>

  3. JS脚本处理(使用ajax技术)
  4. 首先判断有没有选中的值,如果没有则提示;如果有,则传递到服务器端处理
  5. /* 批量删除 */
  6.   // 全选
  7.   $('.all').click(function() {
  8.     if($(this).is(':checked')) {
  9.       $(':checkbox').attr('checked', 'checked');
  10.     } else {
  11.       $(':checkbox').removeAttr('checked');
  12.     }
  13.   });
  14.  
  15.   // 删除操作
  16.   $('#del').click(function() {
  17.     if($(':checked').size() > 0) {
  18.       layer.confirm('确定要删除吗?', {
  19.         btn: ['确定','取消'], //按钮
  20.         shade: false //不显示遮罩
  21.       }, function(){
  22.         $.post("{:U('Single/discard')}", {data: $('form').serializeArray()}, function(res) {
  23.           if(res.state == 1) {
  24.             layer.msg(res.message, {icon: 1, time: 1000});
  25.           } else {
  26.             layer.msg(res.message, {icon: 2, time: 1000});
  27.           }
  28.           setTimeout(function() {
  29.             location.reload();
  30.           }, 1000);
  31.         });
  32.       }, function(){
  33.         layer.msg('取消了删除!', {time: 1000});
  34.       });
  35.     } else {
  36.       layer.alert('没有选择!');
  37.     }
  38.   });
  39.  
  40. PHP代码:
  41.  获取提交的数据,然后循环得到每一个id的值,接着进行删除操作。写在控制器里面;;;
  42.  public functiondel() {
  43.   $contact = M('contact');
  44.   $deleteArr = I('post.data');
  45.   for($i=0;$i<</span>count($deleteArr);$i++) {
  46.     $contact->delete($deleteArr[$i]['value']);
  47.   }
  48.   $this->ajaxReturn(array('message'=>'删除成功!'));
  49.  }
  50.  v






  1. 普通批量删除
  2. 首先是视图部分: 
  3.  <</span>form action="__MODULE__/Admin/User/del" method="get">
  4.        
  5.         <</span>th width="4%"><</span>input type="checkbox" name="checkbox10" id="checkbox10"></</span>th>
  6.         <</span>th width="13%">用户名</</span>th>
  7.         <</span>th width="10%">真实姓名</</span>th>
  8.         <</span>th width="13%">手机号</</span>th>
  9.         <</span>th width="21%">邮箱</</span>th>
  10.         <</span>th width="11%">注册时间</</span>th>
  11.         <</span>th width="17%">操作</</span>th>
  12.        <</span>/tr>
  13.       
  14.        
  15.         
     
  16.         
    {$vo.username}</</span>td>
  17.         
    {$vo.realname}</</span>td>
  18.         
    {$vo.telphone}</</span>td>
  19.         
    {$vo.email}</</span>td>
  20.         
    {$vo.resgistertime}</</span>td>
  21.         
    <</span>a href="__MODULE__/Admin/User/modi/id/{$vo.id}">修改</</span>a><</span>a href="#"></</span>a> <</span>a href="__MODULE__/Admin/User/del/id/{$vo.id}">删除</</span>a></</span>td>
  22.       <</span>/tr>
  23.       </</span>volist>
  24.        
  25.        
  26.       <</span>/table>
  27.      </</span>div>
  28.      <</span>div class="input-group pull-left form">
  29.         <</span>button type="submit" class="btn btn-danger ">删 除</</span>button>
  30.       <</span>/div>
  31.       </</span>form>
  32.  
  33.  仍然是采用表单传值的方法,不过这次不需要验证,因为是对数据的直接处理而非让用户输入数据,所以不必担心数据的不合法性省略了model部分。这里采用的一种比较巧妙地一种方法是将name定义为了一个数组,而在控制器中则只需要对传入的id判断一下是不是数组,省去了分开写的麻烦。
  34.  接下来是控制器的部分 
  35.  public function del(){
  36.   // $name getActionName();   //作为公共的函数使用时添加
  37.   $adminUsersModel = D("adminUsers"); //获取当期模块的操作对象 
  38.   $id = $_GET['id'];  //判断id是数组还是一个数值 
  39.   if(is_array($id)){   
  40.     $where = 'id in('.implode(',',$id).')';  
  41.   }else{  
  42.    $where = 'id='.$id; 
  43.   }  //dump($where); 
  44.   $list=$adminUsersModel->where($where)->delete();  
  45.   if($list!==false) {
  46.      $this->success("成功删除{$list}条!", U("Admin/User/lists")); 
  47.   }else{   
  48.     $this->error('删除失败!');  
  49.   } 
  50.  }
  51.  来源于:一个论坛,亲测成功。。


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值