Thinkphp 全选、反选 批量删除

完整案例

前台

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>我的钱包</title>
    <link href="__PUBLIC__/HomeStyle/css/layout_903ac4c.css" rel="stylesheet" type="text/css" />
    <link href="__PUBLIC__/HomeStyle/css/widgets_680817d.css" rel="stylesheet" type="text/css" />
    <link href="__PUBLIC__/HomeStyle/css/main.html_aio_7fcf01c.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        /*.row-fluid{*/
            /*border-top: 1px solid #DDD;*/
            /*padding: 12px 0 6px;*/
            /*background-color: #eff3f8;*/
            /*border-bottom: 1px solid #DDD;*/
            /* 100%;*/
        /*}*/
        .page {
            display: inline-block;
            margin: 20px 0;
            text-align: center;
        }
        .page a {
            background-color: #fafafa;
            border: 1px solid #e0e8eb;
            color: #2283c5;
            margin: 0 2.5px;
            padding: 6px 12px;
            position: relative;
            text-decoration: none;
        }
        .bullet .top button  {
            height: 25px;
            color: #fff;
            background-color: #00b38a;
            padding: 0px 10px;
            display: inline-block;
            vertical-align: middle;
            line-height: 25px;
            margin-right: 5px;
        }
    </style>



</head>
<body>

<!--首页 用户 公司 标题栏-->
<include file="Public/header" nav="recharge" />
        <!--end #header-->

	<input type="hidden" id="serverTime" value="1494292351403"/>

	<!-- 页面主体START -->
	<div class="container clearfix" id="container">

        <!--左侧菜单栏-->
        <div class="user_bindSidebar">
            <dl class="user_sideBarmenu">
                <dt class="title">帐号设置 </dt>
                <dd>
                    <a   href="{:U('Home/Company/bind_account')}">帐号绑定</a>
                </dd>
                <dd>
                    <a href="{:U('Home/Company/updatePwd')}">修改密码</a>
                </dd>
                <dd>
                    <a href="{:U('Home/Company/index')}">公司资料</a>
                </dd>

                <dt class="title">招聘管理</dt>
                <dd>
                    <a href="{:U('Home/Recruit/Recruitment_manage')}">我的招聘</a>
                </dd>
                <dd>
                    <a  href="{:U('Home/Wallet/apply_invoice')}">申请发票</a>
                </dd>
                <dd>
                    <a  class="hover"  href="{:U('Home/Wallet/recharge')}">我的钱包</a>
                </dd>

            </dl>

        </div>
<input type="hidden" value="1" id="hasSidebar"/>

<div class="user_userinfo_content">
    <dl class="c_section">
        <dt><h1>充值记录</h1></dt>
 <dd class="recharge">
     <!--用户基本信息栏-->
     <include file="Public/company_header_user" nav=""/>
                </dd>
        <dd class="bullet_out">
        <div class="top-up">
            <ul class="reset">
                <li class="current">
                    <a href="/Wallet/recharge">充值记录</a>
                </li>
                <li>
                    <a href="/Wallet/consume">消费记录</a>
                </li>
            </ul>
            <div class="bullet">
                <form action="{:U('Home/Wallet/recharge')}" method="get">
                    <div class="top">
                        <span>充值单号</span>
                        <input type="text" maxlength="15"  name="OrderNumber" value="{$map.OrderNumber}"  />
                        <span>支付类型</span>
                        <select style="height: 25px; color: #999; font-size: 12px; background-color: #fafafa;-->
                        border: 1px solid #d4d2d2; vertical-align: middle; line-height: 25px;" name="mr_paytype" id="paytypesel" >
                            <option value="0">全部</option>
                            <option value="1">微信</option>
                            <option value="2">支付宝</option>
                        </select>
                        <span>订单状态</span>
                        <select style="height: 25px; color: #999; font-size: 12px; background-color: #fafafa;-->
                        border: 1px solid #d4d2d2; vertical-align: middle; line-height: 25px;" name="mr_ordertype" id="ordertypesel">
                            <option value="0">全部</option>
                            <option value="1">等待付款</option>
                            <option value="2">已付款</option>
                        </select>
                        <input type="submit" id="search_submit" style="float: right;" value="搜索"/>
                    </div>
                </form>
                    <table class="conent" cellpadding="0" cellspacing="0" style=" border-color:#d4d2d2">
                    <thead>
                    <tr class="conent_F">
                        <!--<th><input type="checkbox" style=" 20px; height:20px;" /></th>-->
                        <th>编号</th>
                        <th>用户名</th>
                        <th>充值单号</th>
                        <th>支付类型</th>
                        <!--<th>订单类型</th>-->
                        <th>订单金额</th>
                        <th>时间</th>
                        <th>状态</th>
                        <!--<th>操作</th>-->
                    </tr>
                    </thead>
                        <tbody>
                        <foreach name="data" item="value">
                            <tr class="conent_W">
                                <!--<td><input name="check_me[]" type="checkbox"  value="{$value.Id}" style=" 20px; height:20px;" /></td>-->
                                <td>{$value.Id}</td>
                                <td>{$value.username}</td>
                                <td>{$value.OrderNumber}</td>
                                <td><eq name="value.Paytype" value="1">微信<else/><eq name="value.Paytype" value="2">支付宝<else/> </eq></eq></td>
                                <!--<td>{$value.Ordertype}</td>-->
                                <td>{$value.Money}</td>
                                <td>{$value.CreateTime|date='Y.m.d',###}</td>
                                <td class="Wait"><eq name="value.Status" value="1"><i class="red">等待付款</i><else/><eq name="value.Status" value="2">已付款<else/><i style="color:#EAC100">已取消</i></eq> </eq> </td>
                                <!--<td> <a class="Look" href="">查看</a><a class="Fail" href="javascript:;" onclick="$.del('{:U('/Home/Wallet/del', array('Id'=>$value['Id']))}');">删除</a></td>-->
                                <!--<td><a class="Fail" href="javascript:;" onclick="$.del({$value.Id})">删除</a></td>-->

                            </tr>
                        </foreach>
                        </tbody>

                    </table>

                <!--<div class="bottom"><input id="checkbox_all" type="checkbox" style=" 20px; height:20px; vertical-align: middle; margin-top: -4px;" /><span>全选</span>  <a href="" onclick="deleteCheckBox();">删除所选项</a> </div>-->

            </div>
        </div>
            <!--分页-->
            <div class="row-fluid">
                <div class="page">{$page}</div>
            </div>

            <script src="__PUBLIC__/HomeStyle/js/jquery.1.10.1.min.js"></script>
            <link rel="stylesheet" href="__PUBLIC__/Assets/css/lagou.global.css">
            <script src="__PUBLIC__/Assets/lagou/lagou.element.js"></script>
            <script src="__PUBLIC__/Assets/lagou/lagou.global.js"></script>
            <script type="text/javascript">



//批量删除选中行 //在给每一个 type=checkbox 加相同的名字 check_me[] // function deleteCheckBox() { // //提示是否删除 // if(confirm("确定要删除吗?")){ // //将所有复选框的默认值放入数组中 // var check=document.getElementsByName('check_me[]'); // var checkArray=new Array(); // //将所有选中的复选框的默认值写入到数组中 // for(var i=0;i<check.length;i++){ // if(check[i].checked){ // checkArray.push(check[i].value); // } // } // //异步传值到后台 // $.ajax({ // url:"/Home/Wallet/batchDel" , // type:"get", // dataType:'json', // data:{"checkArray":checkArray}, // success:function (data) { // // //成功执行的方法 // if(data.msg=="删除成功!"){ // location.reload(); // }else{ // alert(data.msg); // } // } // }); // // } // } jQuery(function($){ // //上面 全选和全不选的切换 // var check2=true; // $(' table th input:checkbox').on('click',function () { // $(".bullet :checkbox").prop("checked", check2); // check2=!check2; // }); // //下面 全选和全不选的切换 // var check=true; // $("#checkbox_all").click(function () { // $(".bullet :checkbox").prop("checked", check); // check=!check; // });

$.extend({ action:function(url,obj) { var obj=$(obj).parents('form'); $.ajax({ url:url, type:'get', data:obj.serialize(), success:function(res) { // alert(response.msg); $.lagou.alert(response.msg,response.code,2); $.lagou.formHide(); location.reload(); } }); }, // del:function (Id) { // if(confirm("确定要删除吗?")){ // jQuery.ajax({ // url:"/Home/Wallet/del", // type:"get", // data:{"Id":Id}, // success:function (msg) { // alert(msg.msg); // location.reload(); // } // }) // } // } }) }) </script> </dd> </dl> </div> </div> <include file="Public/footer" nav='index'/> </body> </html>


// 这就是全选按钮 方法二
//$('table th input:checkbox').on('click' , function(){
// var that = this;
// $(this).closest('table').find('tr > td:first-child input:checkbox')
// .each(function(){
// this.checked = that.checked;
// $(this).closest('tr').toggleClass('selected');
// });
//});

  

后台

<?php
/** 钱包:充值和消费
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/8
 * Time: 9:26
 */
namespace HomeController;
class WalletController extends CompanyBaseController {

    //定义数据表变量
    protected  $recharge=null;
    protected $consume=null;

    public function __construct()
    {
        parent::__construct();
        $this->recharge=M('Recharge');
        $this->consume=M('Consume');
    }
    //钱包查询
    public function recharge()
    {
        if (!session('?user.id')) {
            $this->redirect('User/login');
        } else {
            $name = $_SESSION['user']['username'];
            $user = M("Users");
            $usermodel = $user->where("username=" . $name)->find();

            $this->assign("user", $usermodel);

        }
        //获取页面所有查询条件
//        $map=I();
//        $a=array_filter($map);
        $a=I();
       //订单搜索
        //var_dump($a);
        if(!empty($a['OrderNumber']))
        {
            $where['OrderNumber']=array('LIKE','%'.$a['OrderNumber'].'%');
        }
        //支付类型
        if ($a['mr_paytype'] != 0) {
            $where['Paytype']=array('EQ',$a['mr_paytype']);
           // $where = $where . " and paytype =" . $a['mr_paytype'];
        }
        //订单状态
        if ($a['mr_ordertype'] != 0) {
            $where['Ordertype']=array('EQ',$a['mr_ordertype']);
           // $where = $where . " and status =" . $a['mr_ordertype'];
        }
        //获取用户id
        $uid = session("user.id");
        $where['UserId']=$uid;
       // $where['Delete']=1;
        //分页部分
        //获取总行数
        $total =$this->recharge->where($where)->count();
        //每10个一页
        $Page= newThinkPage($total,5);
        $Page->setConfig('first','首页');
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('last','尾页');
        $showPage=$Page->show();

        //查询 去掉了订单类型  r.Ordertype
        $data=$this->recharge->where($where)->page(I('get.p',1),5)->order('CreateTime desc')->join('as r left join lg_users as u on r.UserId=u.id')->field('r.Id,u.username,r.OrderNumber,r.Paytype,r.Money,r.CreateTime,r.Status,r.Usertype')->select();
        //返回上一次搜索条件
        $this->assign('map',$a);
        //返回分页栏
        $this->assign('page',$showPage);
        //列表
        $this->assign('data',$data);
        //展示页面
        $this->display();
    }

    public function del()
    {
        //调试方法
//        $this->ajaxReturn($_GET["Id"]);
//        return;

        if(IS_AJAX){
            $id=$_GET["Id"];
           // $id2=$_REQUEST["Id"];
            //异步请求不能使用var_dump()
            //1正常;2已删除;
            //失败,直接用save是跟新整条,必须每个字段多包括。
//            $data['Id']=$id;
//            $data['Delete']=2;
//            $rs=$this->recharge->save($data['Delete']);

            // 成功,更新某个字段。
//            $delete='2';
//            $rs=$this->recharge->where('Id='.$id)->setField('Delete','$delete');
            //成功
            $data['Delete']=2;
            $rs=$this->recharge->where('id='.$id)->field('Delete')->filter('strip_tags')->save($data);
            //当使用field('email')的时候,只允许更新email字段的值(采用strip_tags方法过滤),name字段的值将不会被修改。
            //filter(strip_tags) 调用strip_tags过滤方法
            if($rs===false){
                $msg['code']=0;
                $msg['msg']='删除失败!';
            }else{
                $msg['code']=1;
                $msg['msg']='删除成功!';
            }
            $this->ajaxReturn($msg);
        }

    }

    //批量删除
    public function batchDel()
    {
        //调试方法一
//        $this->ajaxReturn($_GET["checkArray"]);
//        return;
        //调试方法二
//        $data=I('get.');
//        $this->ajaxReturn($data['checkArray']);
 //       return;

        if(IS_AJAX){
            $data=$_GET["checkArray"];
            if(!empty($data)){
                //批量删除方法一
//                foreach ($data as $v){
//                    //删除的条件
//                    $condition['Id']=$v;
//                    $d['Delete']=2;
//                    //删除数据:假删除
//                    $result=$this->recharge->where($condition)->field('Delete')->filter('strip_tags')->save($d);
//                }

                //批量删除方法二
                //判断id是数组还是一个数值
                if(is_array($data)){
                    $condition = 'id in('.implode(',',$data).')';
                }else{
                    $condition = 'id='.$data;
                }
                $d['Delete']=2;
                $result=$this->recharge->where($condition)->field('Delete')->filter('strip_tags')->save($d);
                //判断是否成功
                if($result===false){
                    $msg['code']=0;
                    $msg['msg']='删除失败!';
                }else{
                    $msg['code']=1;
                    $msg['msg']='删除成功!';
                }
            }
           $this->ajaxReturn($msg);

        }
    }

    //消费记录
    public function consume(){
        if (!session('?user.id')) {
            $this->redirect('User/login');
        } else {
            $name = $_SESSION['user']['username'];
            $user = M("Users");
            $usermodel = $user->where("username=" . $name)->find();

            $this->assign("user", $usermodel);

        }


        //获取用户id
        $uid = session("user.id");
        $where['userid']=$uid;
        $where['usertype']=2;//企业类型为2
        //获取总行数
        $total =$this->consume->where($where)->count();
        //分页
        $Page= newThinkPage($total,10);
        $Page->setConfig('first','首页');
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('last','尾页');
        $showPage=$Page->show();
        //查询
        $data=$this->consume->where($where)->page(I('get.p',1),10)->order('createtime desc')->join('as r left join lg_users as u on r.userid=u.id')->field('r.id,u.username,r.paynumber,r.money,r.createtime,r.usertype')->select();
        //返回分页栏
        $this->assign('page',$showPage);
        //列表
        $this->assign('data',$data);
        //展示页面
        $this->display();

    }

    //消费记录
    public function consume_details(){
        //接收记录id
        $id= $_GET['id'];
        //获取失败
//        $username= $_GET['username'];
//        var_dump($username);
        //获取用户id
        $uid = session("user.id");
        $username2=session("user.username");
        $where['userid']=$uid;
        $where['id']=$id;
        $where['usertype']=2;//企业类型为2
        //查询
        $data=$this->consume->where($where)->select();
       // $data=$this->consume->where($where)->join('as r left join lg_users as u on r.userid=u.id')->field('r.id,u.username,r.paynumber,r.money,r.createtime,r.usertype')->select();
        $data[0]['username']=$username2;
        // var_dump($data[0]);
        //列表
        $this->assign('value',$data[0]);

        //展示页面
        $this->display();

    }

    //支付页面
    public function pay(){
        if (!session('?user.id')) {
            $this->redirect('User/login');
        } else {
            $name = $_SESSION['user']['username'];
            $user = M("Users");
            $usermodel = $user->where("username=" . $name)->find();

            $this->assign("user", $usermodel);
            $this->display();
        }

    }

    //申请发票
    public function  apply_invoice(){
        if (!session('?user.id')) {
            $this->redirect('User/login');
        } else {
            $name = $_SESSION['user']['username'];
            $user = M("Users");
            $usermodel = $user->where("username=" . $name)->find();

            $this->assign("user", $usermodel);
            $this->display();
        }

    }

    public function  apply_invoice_do(){
        if(IS_AJAX){
        //调试方法一
//        $this->ajaxReturn($_POST["content"]);
//        return;
            $data['content']= $_POST["content"];
            $data['user_id'] = session('user.id');
            $data['create_time']=time();
            $Invoice = M("Invoice");
            $result=$Invoice->data($data)->add();
             //var_dump($result); 异步使用会中断
            if ($result>0){
                $msg="ok";
            }else{
                $msg="提交失败";
            }
            $this->ajaxReturn($msg);
        }

    }

}
?>

  

原文地址:https://www.cnblogs.com/hao-1234-1234/p/7067458.html