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

xiaoxiao2021-02-28  116

利用ajax批量删除信息删除 <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" οnchange="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" οnclick="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"></i></a> <a style="text-decoration:none" class="ml-5" onClick="local_del(this,'__CONTROLLER__/local_del','{$vo.id}')"title="删除"> <i class="Hui-iconfont"></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> JS脚本处理(使用ajax技术) 首先判断有没有选中的值,如果没有则提示;如果有,则传递到服务器端处理 /* 批量删除 */   // 全选   $('.all').click(function() {     if($(this).is(':checked')) {       $(':checkbox').attr('checked', 'checked');     } else {       $(':checkbox').removeAttr('checked');     }   });     // 删除操作   $('#del').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('没有选择!');     }   });   PHP代码:  获取提交的数据,然后循环得到每一个id的值,接着进行删除操作。写在控制器里面;;;  public functiondel() {   $contact = M('contact');   $deleteArr = I('post.data');   for($i=0;$i<</span>count($deleteArr);$i++) {     $contact->delete($deleteArr[$i]['value']);   }   $this->ajaxReturn(array('message'=>'删除成功!'));  }  v 普通批量删除 首先是视图部分:   <</span>form action="__MODULE__/Admin/User/del" method="get">                 <</span>th width="4%"><</span>input type="checkbox" name="checkbox10" id="checkbox10"></</span>th>         <</span>th width="13%">用户名</</span>th>         <</span>th width="10%">真实姓名</</span>th>         <</span>th width="13%">手机号</</span>th>         <</span>th width="21%">邮箱</</span>th>         <</span>th width="11%">注册时间</</span>th>         <</span>th width="17%">操作</</span>th>        <</span>/tr>                                    {$vo.username}</</span>td>          {$vo.realname}</</span>td>          {$vo.telphone}</</span>td>          {$vo.email}</</span>td>          {$vo.resgistertime}</</span>td>          <</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>       <</span>/tr>       </</span>volist>                       <</span>/table>      </</span>div>      <</span>div class="input-group pull-left form">         <</span>button type="submit" class="btn btn-danger ">删 除</</span>button>       <</span>/div>       </</span>form>    仍然是采用表单传值的方法,不过这次不需要验证,因为是对数据的直接处理而非让用户输入数据,所以不必担心数据的不合法性省略了model部分。这里采用的一种比较巧妙地一种方法是将name定义为了一个数组,而在控制器中则只需要对传入的id判断一下是不是数组,省去了分开写的麻烦。  接下来是控制器的部分   public function del(){   // $name = getActionName();   //作为公共的函数使用时添加   $adminUsersModel = D("adminUsers"); //获取当期模块的操作对象    $id = $_GET['id'];  //判断id是数组还是一个数值    if(is_array($id)){        $where = 'id in('.implode(',',$id).')';     }else{      $where = 'id='.$id;    }  //dump($where);    $list=$adminUsersModel->where($where)->delete();     if($list!==false) {      $this->success("成功删除{$list}条!", U("Admin/User/lists"));    }else{        $this->error('删除失败!');     }   }  来源于:一个论坛,亲测成功。。
转载请注明原文地址: https://www.6miu.com/read-34833.html

最新回复(0)