Thinkphp5.1自定义分页样式
web ThinkPHP5.1 2020-12-28 1419 0
关于本站

“最难不过坚持”

本人承接扒站仿站,php网站维护,病毒查杀,网站编辑,网站改版,html制作

有需要网站维护,改版,病毒查杀,网站编辑,网站备案,html制作等相关的工作可以联系我。
本人有多年相关工作经验,也可提供免费咨询,交个朋友。
有需要探讨问题的朋友,也可以加我微信,共同探讨!
微信:15011482830 QQ:408917339

6413645 2611 39
最新评论
https://jueru.net/
评 留言
:weixiao:
评 留言
:shuijiao: :weiqu: :zhenbang: :leng:
评 留言
:yiwen: :yiwen: :yiwen: :yiwen:
评 EasySass: could not generate CSS file. See Output panel for details.
这个业务逻辑多少都有点奇怪了,阅读浏览次数增值在新闻详情页的控制器方法里setInc,这怎么还写进模型事件里了。如果非要用onAfterRead也可以,把新闻文章的内容单独分出来一个news_content表,然后把它和news做关联,然后给news_content表的onAfterRead事件做增值处理,这样点进新闻页内查询到文章内容时才会触发它。
评 TP6模型事件-查询后onAfterRead不好用
文章标签更多
ThinkPHP (254)
Mysql (58)
DedeCms (33)
jQuery (67)
证件照 (1)
setInc (4)
setDec (4)
onclick (5)
打开边栏(ESC) 关闭边栏(ESC)

第一步、添加分页配置
在config目录下新建一个paginate.php配置文件,代码如下:

<?php
// +----------------------------------------------------------------------
// |分页设置
// +----------------------------------------------------------------------
return [
   /*'type'      => 'bootstrap',*/
    'type'      => 'page\Page',
    'var_page'  => 'page',
    'list_rows' => 15,
];
第二步、自定义page插件
extend目录下创建一个page文件夹,然后在page文件夹里新建一个Page.php文件,代码如下:
<?php
namespace page;
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | 优化版分页插件
// +----------------------------------------------------------------------
// | Author: 美奇软件开发工作室 QQ:15577969
// +----------------------------------------------------------------------
 
use think\Paginator;
 
class Page extends Paginator
{
 
    //首页
    protected function home() {
        if ($this->currentPage() > 1) {
            return "<a href='" . $this->url(1) . "' title='首页'>首页</a>";
        } else {
            return "<p class='disabled'>首页</p>";
        }
    }
 
    //上一页
    protected function prev() {
        if ($this->currentPage() > 1) {
            return "<a href='" . $this->url($this->currentPage - 1) . "' title='上一页'>上一页</a>";
        } else {
            return "<p class='disabled'>上一页</p>";
        }
    }
 
    //下一页
    protected function next() {
        if ($this->hasMore) {
            return "<a href='" . $this->url($this->currentPage + 1) . "' title='下一页'>下一页</a>";
        } else {
            return"<p class='disabled'>下一页</p>";
        }
    }
 
    //尾页
    protected function last() {
        if ($this->hasMore) {
            return "<a href='" . $this->url($this->lastPage) . "' title='尾页'>尾页</a>";
        } else {
            return "<p class='disabled'>尾页</p>";
        }
    }
 
    //统计信息
    protected function info(){
		$html='<p class="pageRemark">总共有<b>' . $this->lastPage .'</b>页,';
		$html.='<b>'. $this->total . '</b>条数据&nbsp;&nbsp;';
        $html.='<select id="pagesize"> ';
		$html.='<option value="5" selected="">5 条/页</option> ';
		$html.='<option value="10">10 条/页</option> ';
		$html.='<option value="20">20 条/页</option> ';
		$html.='<option value="30">30 条/页</option> ';
		$html.='<option value="50">50 条/页</option> ';
		$html.='</select> ';
		$html.='&nbsp;&nbsp;到第<input id="pageInput" type="text" min="1" value="1" class="page-input">页 ';
		$html.='<button type="button" href="javascript:void(0)" onclick="gotoPage()" class="page-btn">确定</button> ';
		$html.='</p>';
		return $html;
    }
 
    /**
     * 页码按钮
     * @return string
     */
    protected function getLinks()
    {
 
        $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(
                    '%s<div class="pagination">%s %s %s</div>',
                    $this->css(),
                    $this->prev(),
                    $this->getLinks(),
                    $this->next()
                );
            } else {
                return sprintf(
                    '%s<div class="pagination">%s %s %s %s %s %s</div>%s',
                    $this->css(),					
                    $this->home(),				
                    $this->prev(),
                    $this->getLinks(),
                    $this->next(),
                    $this->last(),
					$this->info(),
					$this->js()
                );
            }
        }
    }
 
    /**
     * 生成一个可点击的按钮
     *
     * @param  string $url
     * @param  int    $page
     * @return string
     */
    protected function getAvailablePageWrapper($url, $page)
    {
        return '<a href="' . htmlentities($url) . '" >' . $page . '</a>';
    }
 
    /**
     * 生成一个禁用的按钮
     *
     * @param  string $text
     * @return string
     */
    protected function getDisabledTextWrapper($text)
    {
        return '<p class="pageEllipsis">' . $text . '</p>';
    }
 
    /**
     * 生成一个激活的按钮
     *
     * @param  string $text
     * @return string
     */
    protected function getActivePageWrapper($text)
    {
        return '<a href="" class="cur">' . $text . '</a>';
    }
 
    /**
     * 生成省略号按钮
     *
     * @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);
    }
	protected function js(){
		return '<script type="text/javascript">var page=document.getElementById("pagesize"); 
		page.addEventListener("change", function() {
		var _index = this.selectedIndex;
        var size = this.options[_index].value;
		var url="'.$this->url(1).'";	
		if(url.indexOf("&size") !=-1) {
			var index=url.lastIndexOf("&size");
			url=url.substring(0,index);
		}
		url+="&size="+size;
		window.location.href=url});
		function gotoPage(){
			var input=document.getElementById("pageInput");
			var url="'.$this->url(1).'";
			if(url.indexOf("&page") !=-1) {
				var index=url.lastIndexOf("&page");
				url=url.substring(0,index);
			}
			url+="&page="+input.value;
			window.location.href=url
		}
		</script>';
	}
    /**
     * 分页样式
     */
    protected function css(){
        return '  <style type="text/css">
			.page-box,
			.page-box * {
				text-align:center;
				box-sizing: content-box;
			}
            .pagination p{
                margin:0;
                cursor:pointer
            }
            .pagination{
                height:40px;
                padding:20px 0px;
            }
            .pagination a{
                display:block;
                float:left;
                margin-right:10px;
                padding:2px 12px;
                height:24px;
                border:1px #cccccc solid;
                background:#fff;
                text-decoration:none;
                color:#808080;
                font-size:12px;
                line-height:24px;
            }
            .pagination a:hover{
                color:#077ee3;
                background: white;
                border:1px #077ee3 solid;
            }
            .pagination a.cur{
                border:none;
                background:#077ee3;
                color:#fff;
            }
			.pagination p{
                float:left;
                padding:2px 12px;
                font-size:12px;
                height:24px;
                line-height:24px;
                color:#bbb;
                margin-right:8px;
            }
            .pagination p.disabled{
                border:1px #ccc solid;
                background:#fcfcfc;
				cursor:not-allowed;
            }
            .pagination p.pageRemark{
				display:block;
                margin-right:10px;
				font-size:14px;
                padding:4px 0px;
                color:#666;
            }
			/*屏幕宽度小于992px时*/
			@media all and (max-width: 992px) {
				.pagination p.pageRemark{
					display: none;
				}
			}
            .pagination p.pageRemark b{
                color:red;
            }
			.pagination p select{
				font-size:12px;
				margin: 0 5px;
				padding: 0 10px;
				text-align: center;
				height: 28px;
				line-height: 28px;
				border: 1px solid #e2e2e2;
				border-radius: 2px;
				vertical-align: top;
				background-color: #fff;
				box-sizing: border-box;
            }
            .pagination p.pageEllipsis{
                border-style:none;
                background:none;
                padding:4px 0px;
                color:#808080;
				cursor: not-allowed;
            }
            .dates li {font-size: 14px;margin:20px 0}
            .dates li span{float:right}
			.page-input{
				display: inline-block;
				width: 40px;
				margin: 0 5px;
				padding: 0 5px;
				text-align: center;
				height: 28px;
				line-height: 28px;
				border: 1px solid #e2e2e2;
				border-radius: 2px;
				vertical-align: top;
				background-color: #fff;
				box-sizing: border-box;
			}
			.page-btn{
                font-size:12px;
				margin: 0 5px;
				padding: 0 10px;
				text-align: center;
				height: 28px;
				line-height: 28px;
				border: 1px solid #e2e2e2;
				border-radius: 2px;
				vertical-align: top;
				background-color: #fff;
				box-sizing: border-box;
            }
            .page-btn:hover{
                color:#077ee3;
                background: white;
                border:1px #077ee3 solid;
            }
        </style>';
    }
}
第三步、controller后端控制器写法

    /**
    * 菜单列表
    */
    public function index(){
		/**
		 * 分页查询
		 * paginate($listRows = null, $simple = false, $config = [])
		 * @param  $listRows 每页数量 数组表示配置参数
		 * @param  $simple   是否简洁模式或者总记录数
		 * @param  $config   配置参数
		 *                   page:当前页,
		 *                   path:url路径,
		 *                   query:url额外参数,
		 *                   fragment:url锚点,
		 *                   var_page:分页变量,
		 *                   list_rows:每页数量
		 *                   type:分页类名
		 */
		 //接受参数用param接收(可以接收post和get)
		$size = request()->param('size');
		//每页显示几条数据
		$pagesize=$size? $size : 5;	
		$list= Db::name('user')
		->order('sort,id')
		->paginate($pagesize,false,['query'=>request()->param()]);
		// 获取分页显示
		$page = $list->render();
		$this->assign('list',$list);
		$this->assign('page',$page);		
        return view();	
    }
第四步、HTML模板页面

<div class="row">
	<div class="page-box">
		{$page|raw}
	</div>
</div>





版权声明:本篇文章来源于网络。 来源链接

相关推荐
Non-static method think\Config::get() should not be called statically
ThinkPHP5.1 | 2018-12-29 10399
原来是这样use think\Config; 改成这样use think\facade\Config; 下面是官方手册的解释 配置获取 要使用Config类,首先需要在你的类文件中引入 use think\facade\Config; 或者(因为系统做了类库别名,其实就是调用think\facade\Config) u...
thinkPHP5 order多条件排序
ThinkPHP5.1 | 2017-10-24 10266
总结如下: //单一条件排序 $user = $this->where(['parentId'=>0)->field('userId,userName,userSort,isShow')->order('userSort', 'asc')->select(); //多个条件排序,可以多加一个order...
评论:0条
评论加载中...
发表评论