有些时候我们需要数据进行分类以便统计,例如,有按时间段区分、按类别区分,同时如果多条数据还要带有分页。今天我写的就是这个需求,我这有大量的考勤统计数据,但是我们要按时间端来筛选显示,以前我可能会用异步再异步的方式,什么意思呢?就是说我会写来个异步加载数据的方法,显示点击查询异步加载出所有符合添加的数据,再点击分页再异步加载每一页的数据,这就是异步再异步加载方式。今天我讲的是异步再刷新的方式,我在点击分页是让其正常跳转加载,下面是代码   }

    public function carddata_ajax(){
      if(!empty($_POST)){
        $beginToday=strtotime($_POST['startdate']);
        $endToday=strtotime($_POST['overdate'])+86399;
        $map['cardtime'] = array(array('gt',$beginToday),array('lt',$endToday)) ;
        $timestr=$beginToday.'-'.$endToday;
        session('mapcardtime',$timestr);
        $count      =$this->attendanceModle->where($map)->count();
        $Page       = new \Think\Page($count,10);
        $show       = $Page->show();// 分页显示输出
        if($_POST['p']==0){
          $firstRow  = 0;
        }else{
          $firstRow  = 10 * ($_POST['p'] - 1);
        }
        $list = $this->attendanceModle->where($map)->order('id desc')->limit($firstRow.','.$Page->listRows)->select();
        foreach ($list as $key => $value) {
          $list[$key]['areaname']=M("doorcontroller")->where(array('controllersn'=>$value['controllersn']))->getField('areaname');
        }
        $this->assign("list",$list);
        $this->assign('page',$show);// 赋值分页输出
        $str=$this->fetch('attendance_ajax');//把页面转换成字符串
        echo json_encode(array('type'=>'yes','str'=>$str,'num'=>count($list)));
      }else{//异步ajax的分页,点击后再刷新跳转
        $timestr=$_SESSION['mapcardtime'];
        $timeArr=explode('-',$timestr);
        $map['cardtime'] = array(array('gt',$timeArr['0']),array('lt',$timeArr['1'])) ;
        $count      =$this->attendanceModle->where($map)->count();
        $Page       = new \Think\Page($count,10);
        $show       = $Page->show();// 分页显示输出
        if($_GET['p']==0){
          $firstRow  = 0;
        }else{
          $firstRow  = 10 * ($_GET['p'] - 1);
        }
        $list = $this->attendanceModle->where($map)->order('id desc')->limit($firstRow.','.$Page->listRows)->select();
        foreach ($list as $key => $value) {
          $list[$key]['areaname']=M("doorcontroller")->where(array('controllersn'=>$value['controllersn']))->getField('areaname');
        }
        $this->assign("list",$list);
        $this->assign('page',$show);// 赋值分页输出
        $this->display('index');
      }

    }

雷小天博客