thinkphp分页paginate跳转并带上原有搜索参数

thinkphp默认的分页并不带搜索参数,所以这里简单处理一下。

这里处理后的结果:

分页链接将携带搜索参数:

search.html?page=5&keywords=漫画

首先接收搜索关键字应该这里处理:

$keywords=input('post.keywords') ?? input('get.keywords'); //推荐优先post
或者
$keywordsinput('keywords')

分页组件参数这样搞:

这里,我们添加了['query'=>request()->param()],其中request()->param()为你的提交参数post/get的数据

$list = Db::name('books')->alias('a')->field('a.id,a.book_name,a.thumbs)->where($where)->order('a.id desc')->paginate(25, false, ['query'=>request()->param()]);
$page = $list->render();

接下来,修改thinkphp的一个文件,路径为

/thinkphp/library/think/paginator/driver/Bootstrap.php

添加如下代码:

//跳转到哪页
protected  function goPage()
{
	$curpage=$this->currentPage();
	$param=$this->url($curpage);
	return $gotohtml=<<<EOF
<li>
<form action='{$param}' method='get' style="position: relative;
float: left;
margin-left: -1px;
line-height: 1.42857143;
padding:0;
color: #1992FC;
text-decoration: none;
background-color: #fff;
border: 1px solid #ddd;">
<input style='height:100%; border:0; text-align: center; width: 33px;' type='text' value='{$curpage}' name='page' placeholder=''>
<input style='height:100%; padding:6px; border:0;background-color:#1992FC;color:#FFF;' type='submit' value='GO'>
</form>
</li>
EOF;
	return $gotohtml;
}

修改 render 方法内的代码为如下:

public function render()
{
	if ($this->hasPages()) {
		if ($this->simple) {
			return sprintf(
				'<ul class="pager">%s %s</ul>',
				$this->getPreviousButton(),
				$this->getNextButton()
			);
		} else {
			return sprintf(
				'<ul class="pagination">%s %s %s %s</ul>', //添加add
				$this->getPreviousButton(),
				$this->getLinks(),
				$this->getNextButton(),
				$this->goPage() //添加add
			);
		}
	}
}

这样就行了。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注