php+ajax异步无刷新分页

目的:实现ajax异步分页

相关:query-1.9.1.min.js

在这里插入图片描述

html部分(主要用js拼接渲染)

js部分(js拼接渲染)

 //ajax分页
	 var status_html = "";
	function page_ajax(id){

		$(this).attr('class','current');
		var key_status=$("[name='se1'] option:selected").attr("value");
		var key=$("[name='se2'] option:selected").attr("value");
		var key_value=$("[name='se3']").val();
		$.get('__URL__/ajax_search', {'p':id,'type':1,'key':key,'key_value':key_value}, function(data){   // 用get方法发送信息到url里
			//console.log(data);
			var html = "";
			$('#listdata').html("");
			if(data[0]!=''){
			$.each(data[0],function(k,v){
					
					 status_html = '<span class="title-10" style="color:green;">检测已完成</span>';
                     html+= '<tr value="'+v.id+'" name="search"><td><div class="layui-unselect layui-form-checkbox" lay-skin="primary" data-id="2"><i class="layui-icon">&#xe605;</i>fff</div></td><td>'+v.identifier+'</td><td>'+v.entrust_id+'</td><td>'+v.testing_name+'</td><td>'+v.entrust_unit+'</td><td>'+v.engineering_name+'</td><td>'+v.account+'</td><td>'+v.engineering_code+'</td><td>'+v.send_sample_person+'</td><td>'+v.tel+'</td><td>'+v.sample_name+'</td><td>'+v.time+'</td>';
					 html+= "<td>"+status_html+"</td>";
					 html+= '<td><a title="查看"  οnclick="form_view('+v.id+','+v.status+','+v.cost+')" href="javascript:;"><i class="layui-icon">&#xe63c;</i></a><a title="删除" οnclick="member_del(this,'+v.id+')" href="javascript:;"><i class="layui-icon">&#xe640;</i></a></td></tr>'
                })
				$('#listdata').html(html);
				$("#show_page").html(data[1]);
			}
		});
	}

php部分(tp3.2)

加载页面时使用分页类,为了生成分页页码。$this->PAGESUM为每一页显示的数据。目前private $PAGESUM=3;

public function entrustmanage_status2(){
		$where = 'display = 1 and status in ("2","3","4")';
		$order = 'id desc';
		$data=page("testing_currency",$where,'page_ajax',$this->PAGESUM,$get,'*',$order);
		$this->assign('testing_info',$data[0]);			//分页数据
		
		$this->display();
	}

可以直接使用分页类 import(‘Org.Util.Mypage’);再实例化KaTeX parse error: Expected 'EOF', got '\Mypage' at position 12: Page = new \̲M̲y̲p̲a̲g̲e̲(count, s u m , sum, sum,Fun,$_GET_DATA);
在page方法中参数:
$dbName对应表名,
$Where 对应条件,
$Fun 对应函数名称(也就是页面a标签中的page_ajax(‘我是页码’)),
$sum 对应显示条数,
$_GET_DATA 对应get的数据 (没有则传空),
$FieldName 对应所需要的字段名称,
$NameSort 对应排序字段

/**
     * 分页链接 ajax分页
     * @param string $dbName  表名
     * @param string or array $Where 条件
     * @param string $Fun 函数名称(html页面需要点击的页码函数)
     * @param string $sum 显示条数
     * @param string $_GET_DATA get的数据 (没有则传空)
     * @param string $FieldName 所需要的字段名称
     * @param string $NameSort 排序字段
     */
	function page($dbName,$Where,$Fun,$sum,$_GET_DATA,$FieldName='*',$NameSort='id desc'){
		$count = M($dbName)->where($Where)->count();
		//dump(M()->getLastSql());die();
		import('Org.Util.Mypage');
        $Page = new \Mypage($count,$sum,$Fun,$_GET_DATA);             // 实例化分页类 传入总记录数和每页显示的记录数
        $limit_value = $Page->firstRow . "," . $Page->listRows;
		$page_show = $Page->show();      
		$list = M($dbName)->where($Where)->field($FieldName)->order($NameSort)->limit($limit_value)->select();
		
		$data[1]=$page_show;
		$data[0]=$list;
		return $data;
	}

php分页类

<?php
//namespace Admin\Controller;
//use Think\Controller;
class Mypage {
    public $firstRow; // 起始行数
    public $listRows; // 列表每页显示行数
    public $parameter; // 分页跳转时要带的参数
    public $totalRows; // 总行数
    public $totalPages; // 分页总页面数
    public $rollPage   = 11;// 分页栏每页显示的页数
    public $lastSuffix = true; // 最后一页是否显示总页数

    private $p       = 'p'; //分页参数名
    private $url     = ''; //当前链接URL
    private $nowPage = 1;

    // 分页显示定制
    private $config  = array(
        'header' => '<span class="rows">共 %TOTAL_ROW% 条记录</span>',
        'prev'   => '上一页',
        'next'   => '下一页',
        'first'  => '1...',
        'last'   => '...%TOTAL_PAGE%',
        'theme'  => '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%',
    );

    /**
     * 架构函数
     * @param array $totalRows  总的记录数
     * @param array $listRows  每页显示记录数
     * @param array $parameter  分页跳转的参数
     * @param array $_GET_DATA  因为是函数引用,无法直接获取get参数,所以已传参方式进来
     */
    public function __construct($totalRows, $listRows=20,$ajax_func,$_GET_DATA,$parameter = array()) {
        C('VAR_PAGE') && $this->p = C('VAR_PAGE'); //设置分页参数名称
        /* 基础设置 */
        $this->totalRows  = $totalRows; //设置总记录数
        $this->ajax_func = $ajax_func;
        $this->listRows   = $listRows;  //设置每页显示行数
        $this->parameter  = empty($parameter) ? $_GET_DATA : $parameter;
        $this->nowPage    = empty($_GET_DATA[$this->p]) ? 1 : intval($_GET_DATA[$this->p]);
        $this->nowPage    = $this->nowPage>0 ? $this->nowPage : 1;
        $this->firstRow   = $this->listRows * ($this->nowPage - 1);
    }

    /**
     * 定制分页链接设置
     * @param string $name  设置名称
     * @param string $value 设置值
     */
    public function setConfig($name,$value) {
        if(isset($this->config[$name])) {
            $this->config[$name] = $value;
        }
    }

    /**
     * 生成链接URL
     * @param  integer $page 页码
     * @return string
     */
    private function url($page){
        return str_replace(urlencode('[PAGE]'), $page, $this->url);
    }

    /**
     * 组装分页链接
     * @return string
     */
    public function show() {
        if(0 == $this->totalRows) return '';

        /* 生成URL */
        $this->parameter[$this->p] = '[PAGE]';
        $a = u(__SELF__);
        $b = substr($a,15,strlen($a));
		
        $this->url = U($b, $this->parameter);
		
        /* 计算分页信息 */
        $this->totalPages = ceil($this->totalRows / $this->listRows); //总页数
        if(!empty($this->totalPages) && $this->nowPage > $this->totalPages) {
            $this->nowPage = $this->totalPages;
        }

        /* 计算分页临时变量 */
        $now_cool_page      = $this->rollPage/2;
        $now_cool_page_ceil = ceil($now_cool_page);
        $this->lastSuffix && $this->config['last'] = $this->totalPages;
		
		
        //上一页
        $up_row  = $this->nowPage - 1;
        $up_page = $up_row > 0 ? '<li class="prev"><a href="javascript:' . $this->ajax_func.'('.$up_row.')">' . $this->config['prev'] . '</a></li>' : '<li class="prev disabled"><a>' . $this->config['prev'] . '</a></li>';

        //下一页
        $down_row  = $this->nowPage + 1;
        $down_page = ($down_row <= $this->totalPages) ? '<li class="next"><a href="javascript:' . $this->ajax_func.'('.$down_row.')">' . $this->config['next'] . '</a></li>' : '<li class="next disabled"><a>' . $this->config['next'] . '</a></li>';

        //第一页
        $the_first = '';
        if($this->totalPages > $this->rollPage && ($this->nowPage - $now_cool_page) >= 1){
            $the_first = '<li><a href="javascript:' . $this->ajax_func . '(1)">' . $this->config['first'] . '</a></li>';
        }

        //最后一页
        $the_end = '';
        if($this->totalPages > $this->rollPage && ($this->nowPage + $now_cool_page) < $this->totalPages){
            $the_end = '<li><a href="javascript:' . $this->ajax_func . '('.$this->totalPages.')">' . $this->config['last'] . '</a></li>';
        }

        //数字连接
        $link_page = "";
		
		for($i = 1; $i <= $this->rollPage; $i++){
			if(($this->nowPage - $now_cool_page) <= 0 ){
				$page = $i;
			}elseif(($this->nowPage + $now_cool_page - 1) >= $this->totalPages){
				$page = $this->totalPages - $this->rollPage + $i;
			}else{
				$page = $this->nowPage - $now_cool_page_ceil + $i;
			}
			
            if($page > 0 && $page != $this->nowPage){

                if($page <= $this->totalPages){
					
                    $link_page .= '<li><a href="javascript:' . $this->ajax_func . '(' . $page . ')">' . $page . '</a></li>';
					//dump($page);
				}else{
                    break;
                }
            }else{
                if($page > 0 && $this->totalPages != 1){
					//dump($page);
                    $link_page .= '<li class="active"><a href="javascript:' . $this->ajax_func . '(' . $page . ')">' . $page . '</a></li>';
                }
            }
        }
        //替换分页内容
        $page_str = str_replace(
            array('%HEADER%', '%NOW_PAGE%', '%UP_PAGE%', '%DOWN_PAGE%', '%FIRST%', '%LINK_PAGE%', '%END%', '%TOTAL_ROW%', '%TOTAL_PAGE%'),
            array($this->config['header'], $this->nowPage, $up_page, $down_page, $the_first, $link_page, $the_end, $this->totalRows, $this->totalPages),
            $this->config['theme']);
        return "<ul class='paging_ul'>{$page_str}<div class='clear'></div></ul>";
    }
}
	}

这里的分页与搜索用同一个方法 ajax_search() 接收异步传过来的值,再拼接渲染前端页面

 //ajax条件搜索or分页
   public function ajax_search(){
	   $key=I("key");
	   $type = I('type');
	   if(!empty($key)){
		   switch(I("key")){
			   case '1':$data['id']=I("key_value");break;
			   case '2':$data['entrust_id']=I("key_value");break;
		    } 
	   }
	  switch ($type)
		{
		case 1:
		
			$data['status'] = '1' ;
			break;
		case 2:
			 $data['status'] = array(array('EQ','2'),array('EQ','3'),array('EQ','4'), 'or') ;
			break;
		case 3:
			$data['status'] = '5' ;
			break;
		default:
			return false;
	  }   
	   $data['display'] = 1;
	   $this->result_pages(false,$data);
   }
   public function result_pages($type=false,$where=false){
		$get=I("get.");
		$get=!empty($get)?$get:'';
		$order = 'concat(action_time, id) desc';
		$data=page("testing_currency",$where,'page_ajax',$this->PAGESUM,$get,'*',$order);
		$this->ajaxReturn($data);
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值