thinkphp5+layui實(shí)現(xiàn)的分頁樣式示例
本文實(shí)例講述了thinkphp5+layui實(shí)現(xiàn)的分頁樣式。分享給大家供大家參考,具體如下:
tp5之layui分頁樣式
1.分頁類
路徑:\thinkphp\library\think\paginator\driver
Layui.php
<?php namespace think\paginator\driver; use think\Paginator; class Layui extends Paginator { /** * 上一頁按鈕 * @param string $text * @return string */ protected function getPreviousButton($text = "上一頁") { if ($this->currentPage() <= 1) { return $this->getDisabledTextWrapper($text); } $url = $this->url( $this->currentPage() - 1 ); return $this->getPageLinkWrapper($url, $text); } /** * 下一頁按鈕 * @param string $text * @return string */ protected function getNextButton($text = '下一頁') { if (!$this->hasMore) { return $this->getDisabledTextWrapper($text); } $url = $this->url($this->currentPage() + 1); return $this->getPageLinkWrapper($url, $text); } /** * 頁碼按鈕 * @return string */ protected function getLinks() { if ($this->simple) return ''; $block = [ 'first' => null, 'slider' => null, 'last' => null ]; $side = 3; $window = $side * 2; if ($this->lastPage < $window + 6) { $block['first'] = $this->getUrlRange(1, $this->lastPage); } elseif ($this->currentPage <= $window) { $block['first'] = $this->getUrlRange(1, $window + 2); $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage); } elseif ($this->currentPage > ($this->lastPage - $window)) { $block['first'] = $this->getUrlRange(1, 2); $block['last'] = $this->getUrlRange($this->lastPage - ($window + 2), $this->lastPage); } else { $block['first'] = $this->getUrlRange(1, 2); $block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side); $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage); } $html = ''; if (is_array($block['first'])) { $html .= $this->getUrlLinks($block['first']); } if (is_array($block['slider'])) { $html .= $this->getDots(); $html .= $this->getUrlLinks($block['slider']); } if (is_array($block['last'])) { $html .= $this->getDots(); $html .= $this->getUrlLinks($block['last']); } return $html; } /** * 渲染分頁html * @return mixed */ public function render() { if ($this->hasPages()) { if ($this->simple) { return sprintf( '<ul class="pager">%s %s</ul>', $this->getPreviousButton(), $this->getNextButton() ); } else { return sprintf( '%s %s %s', $this->getPreviousButton(), $this->getLinks(), $this->getNextButton() ); } } } /** * 生成一個(gè)可點(diǎn)擊的按鈕 * * @param string $url * @param int $page * @return string */ protected function getAvailablePageWrapper($url, $page) { return '<a href="' . htmlentities($url) . '" rel="external nofollow" >' . $page . '</a>'; } /** * 生成一個(gè)禁用的按鈕 * * @param string $text * @return string */ protected function getDisabledTextWrapper($text) { return '<a class="layui-laypage-prev" >' . $text . '</a>'; } /** * 生成一個(gè)激活的按鈕 * * @param string $text * @return string */ protected function getActivePageWrapper($text) { return '<span class="layui-laypage-curr"> <em class="layui-laypage-em"></em><em>' . $text . '</em></span>'; } /** * 生成省略號(hào)按鈕 * * @return string */ protected function getDots() { return $this->getDisabledTextWrapper('...'); } /** * 批量生成頁碼按鈕. * * @param array $urls * @return string */ protected function getUrlLinks(array $urls) { $html = ''; foreach ($urls as $page => $url) { $html .= $this->getPageLinkWrapper($url, $page); } return $html; } /** * 生成普通頁碼按鈕 * * @param string $url * @param int $page * @return string */ protected function getPageLinkWrapper($url, $page) { if ($page == $this->currentPage()) { return $this->getActivePageWrapper($page); } return $this->getAvailablePageWrapper($url, $page); } }
2.配置文件
paginate.php
<?php /** * @auther: xxf * Date: 2019/9/2 * Time: 10:24 */ //分頁配置 return [ 'type' => 'Layui', 'var_page' => 'page', ];
3.模型查詢
public function getUserShowList($size = 20, $where = null) { $res = $this ->field('id,title,list_order,is_top,create_time,create_time time') ->where($where) ->order(['is_top' => 'desc', 'list_order' => 'desc', 'id' => 'desc']) ->paginate($size); return $res; }
4.模板渲染
<div class="layui-box layui-laypage layui-laypage-molv">{$list|raw}</div>
效果
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
laravel解決遷移文件一次刪除創(chuàng)建字段報(bào)錯(cuò)的問題
今天小編就為大家分享一篇laravel解決遷移文件一次刪除創(chuàng)建字段報(bào)錯(cuò)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10php實(shí)現(xiàn)簡(jiǎn)單的權(quán)限管理的示例代碼
本篇文章主要介紹了php實(shí)現(xiàn)簡(jiǎn)單的權(quán)限管理的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08PHP設(shè)計(jì)模式之工廠模式(Factory)入門與應(yīng)用詳解
這篇文章主要介紹了PHP設(shè)計(jì)模式之工廠模式(Factory),結(jié)合實(shí)例形式詳細(xì)分析了PHP工廠模式的概念、原理、基本應(yīng)用與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-12-12PHP+Mysql無刷新問答評(píng)論系統(tǒng)(源碼)
自己寫的一個(gè)評(píng)論系統(tǒng)源碼分享給大家,包括有表情,還有評(píng)論機(jī)制,代碼簡(jiǎn)單易懂,需要的朋友參考下2016-12-12PHP表單數(shù)據(jù)寫入MySQL數(shù)據(jù)庫(kù)的代碼
這篇文章主要介紹了PHP表單數(shù)據(jù)寫入MySQL數(shù)據(jù)庫(kù)的相關(guān)資料非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-05-05