自定义无刷新分页

来源:互联网 发布:易顺佳进销存软件 编辑:程序博客网 时间:2024/06/06 01:35
用不惯框架的分页自己搞了一个分页,可以用AJAX做无刷新分页,也可以做普通分页区分是调用的方法
<?php/* * ********************************************* * @类名:   page * @参数:   $myde_total - 总记录数 *          $myde_size - 一页显示的记录数 *          $myde_page - 当前页 *          $myde_url - 获取当前的url * @功能:   分页实现 * @作者:   宋海阁 */namespace App\libs\page;class page {    private $myde_total;          //总记录数    private $myde_size;           //一页显示的记录数    private $myde_page;           //当前页    private $myde_page_count;     //总页数    private $myde_i;              //起头页数    private $myde_en;             //结尾页数    private $myde_url;            //获取当前的url    /*     * $show_pages     * 页面显示的格式,显示链接的页数为2*$show_pages+1。     * 如$show_pages=2那么页面上显示就是[首页] [上页] 1 2 3 4 5 [下页] [尾页]      */    private $show_pages;    public function __construct($myde_total = 1, $myde_size = 1, $myde_page = 1, $myde_url, $show_pages = 2) {        $this->myde_total = $this->numeric($myde_total);        $this->myde_size = $this->numeric($myde_size);        $this->myde_page = $this->numeric($myde_page);        $this->myde_page_count = ceil($this->myde_total / $this->myde_size);        $this->myde_url = $myde_url;        if ($this->myde_total < 0)            $this->myde_total = 0;        if ($this->myde_page < 1)            $this->myde_page = 1;        if ($this->myde_page_count < 1)            $this->myde_page_count = 1;        if ($this->myde_page > $this->myde_page_count)            $this->myde_page = $this->myde_page_count;        $this->limit = ($this->myde_page - 1) * $this->myde_size;        $this->myde_i = $this->myde_page - $show_pages;        $this->myde_en = $this->myde_page + $show_pages;        if ($this->myde_i < 1) {            $this->myde_en = $this->myde_en + (1 - $this->myde_i);            $this->myde_i = 1;        }        if ($this->myde_en > $this->myde_page_count) {            $this->myde_i = $this->myde_i - ($this->myde_en - $this->myde_page_count);            $this->myde_en = $this->myde_page_count;        }        if ($this->myde_i < 1)            $this->myde_i = 1;    }    //检测是否为数字    private function numeric($num) {        if (strlen($num)) {            if (!preg_match("/^[0-9]+$/", $num)) {                $num = 1;            } else {                $num = substr($num, 0, 11);            }        } else {            $num = 1;        }        return $num;    }    //地址替换    private function page_replace($page) {        return str_replace("{page}", $page, $this->myde_url);    }    //首页    private function myde_home() {        if ($this->myde_page != 1) {            return "<a href='" . $this->page_replace(1) . "' title='首页'>首页</a>";        } else {            return "<a class='first'>首页</a>";        }    }    private function ajaxmyde_home() {        if ($this->myde_page != 1) {            return "<i data='" . $this->page_replace(1) . "' title='首页'>首页</i>";        } else {            return "<i class='first'>首页</i>";        }    }    //上一页    private function myde_prev() {        if ($this->myde_page != 1) {            return "<a href='" . $this->page_replace($this->myde_page - 1) . "' title='上一页'>上一页</a>";        } else {            return "<a class='prev'>上一页</a>";        }    }    private function ajaxmyde_prev() {        if ($this->myde_page != 1) {            return "<li class='disabled' id='beforepage' data='" . ($this->myde_page - 1) . "' title='上一页'><span>«</span></li>";        } else {            return "<li class='disabled'><span>«</span></li>";        }    }    //下一页    private function myde_next() {        if ($this->myde_page != $this->myde_page_count) {            return "<a href='" . $this->page_replace($this->myde_page + 1) . "' title='下一页'>下一页</a>";        } else {            return"<a class='next'>下一页</a>";        }    }    private function ajaxmyde_next() {        if ($this->myde_page != $this->myde_page_count) {            return "<li id='nextpage' rel='next' class='disabled' data='" . ($this->myde_page + 1) . "' title='下一页'><span>»</span></i>";        } else {            return"<li rel='next' class='disabled'><span>»</span></li>";        }    }    //尾页    private function myde_last() {        if ($this->myde_page != $this->myde_page_count) {            return "<a href='" . $this->page_replace($this->myde_page_count) . "' title='尾页'>尾页</a>";        } else {            return "<a class='end'>尾页</a>";        }    }    private function ajaxmyde_last() {        if ($this->myde_page != $this->myde_page_count) {            return "<i data='" . $this->page_replace($this->myde_page_count) . "' title='尾页'>尾页</i>";        } else {            return "<i class='end'>尾页</i>";        }    }    //输出    public function myde_write($id = 'page') {        $str = "<div id=" . $id . ">";        $str.=$this->myde_home();        $str.=$this->myde_prev();        if ($this->myde_i > 1) {            $str.="<span class='pageEllipsis'>...</span>";        }        for ($i = $this->myde_i; $i <= $this->myde_en; $i++) {            if ($i == $this->myde_page) {                $str.="<a href='" . $this->page_replace($i) . "' title='第" . $i . "页' class='cur' style='background-color: #337ab7;border-color: #337ab7;cursor: default;color: #fff;'>$i</a>";            } else {                $str.="<a href='" . $this->page_replace($i) . "' title='第" . $i . "页'>$i</a>";            }        }        if ($this->myde_en < $this->myde_page_count) {            $str.="<span class='pageEllipsis'>...</span>";        }        $str.=$this->myde_next();        $str.=$this->myde_last();        $str.="<a class='pageRemark'>共<b>" . $this->myde_page_count .                "</b>页<b>" . $this->myde_total . "</b class='rows'>条数据</a>";        $str.="</div>";        return $str;    }    public function ajaxmyde_write($id = 'page') {        $str = "<div id=" . $id . ">";//        $str.=$this->ajaxmyde_home();        $str.=$this->ajaxmyde_prev();        if ($this->myde_i > 1) {            $str.="<span class='pageEllipsis'>...</span>";        }        for ($i = $this->myde_i; $i <= $this->myde_en; $i++) {            if ($i == $this->myde_page) {                $str.="<li class='active' data='" .$i . "' title='第" . $i . "页' class='cur' style='background-color: #337ab7;border-color: #337ab7;cursor: default;color: #fff;pointer-events: none;'><span>$i</span></li>";            } else {                $str.="<li id='page' class='active'  data='" . $i . "' title='第" . $i . "页'><span>$i</span></li>";            }        }        if ($this->myde_en < $this->myde_page_count) {            $str.="<span class='pageEllipsis'>...</span>";        }        $str.=$this->ajaxmyde_next();//        $str.=$this->ajaxmyde_last();//        $str.="<i class='pageRemark'>共<b>" . $this->myde_page_count .//            "</b>页<b>" . $this->myde_total . "</b class='rows'>条数据</i>";        $str.="</div>";        return $str;    }}?>

然后是调用,我写成了公共方法方便多地方用

1是AJAX无刷新分页方法

function pageAjax($str,$curpage,$showrow,$total){        $url = $str;        if (!empty($_GET['page']) && $total != 0 && $curpage > ceil($total / $showrow)){            $curpage = ceil($total / $showrow); //当前页数大于最后页数,取最后一页        }        $p="";        if ($total > $showrow) {//总记录数大于每页显示数,显示分页            $page = new page($total, $showrow, $curpage, $url, 2);            $p = $page->ajaxmyde_write();        }        return $p;    }
方法调用
$p = page3("?page={page}",$page,2,$count);

页面

$(document).on('click','#beforepage',function(){                var date = storage.getItem('date');                var page = $(this).attr('data');                                   familybill(date,page)                                return false;            });            $(document).on('click','#nextpage',function(){                var date = storage.getItem('date');                var page = $(this).attr('data');                                    familybill(date,page)                                return false;            });            $(document).on('click','#page',function(){                                             familybill(date,page)                                return false;            });

2 刷新分页

function page($uid,$curpage,$showrow,$total){        $url = "?uid=$uid&page={page}";//该行参数可以自己修改        if (!empty($_GET['page']) && $total != 0 && $curpage > ceil($total / $showrow)){            $curpage = ceil($total / $showrow); //当前页数大于最后页数,取最后一页        }        $p="";        if ($total > $showrow) {//总记录数大于每页显示数,显示分页            $page = new page($total, $showrow, $curpage, $url, 2);            $p = $page->myde_write();        }        return $p;    }

方法调用
$p = page1($input['order'],$input['by'],$input['page'],10,$count->num);

页面

<?php echo $p >


0 0
原创粉丝点击