Skip to content

Commit 8bf919a

Browse files
committed
增加paginator
1 parent 61fe1eb commit 8bf919a

File tree

2 files changed

+280
-0
lines changed

2 files changed

+280
-0
lines changed

src/paginator/Collection.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
// +----------------------------------------------------------------------
3+
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4+
// +----------------------------------------------------------------------
5+
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
6+
// +----------------------------------------------------------------------
7+
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8+
// +----------------------------------------------------------------------
9+
// | Author: zhangyajun <448901948@qq.com>
10+
// +----------------------------------------------------------------------
11+
12+
namespace think\paginator;
13+
14+
use Exception;
15+
use think\Paginator;
16+
17+
/**
18+
* Class Collection
19+
* @package think\paginator
20+
* @method integer total()
21+
* @method integer listRows()
22+
* @method integer currentPage()
23+
* @method string render()
24+
* @method Paginator fragment($fragment)
25+
* @method Paginator appends($key, $value)
26+
* @method integer lastPage()
27+
* @method boolean hasPages()
28+
*/
29+
class Collection extends \think\Collection
30+
{
31+
32+
/** @var Paginator */
33+
protected $paginator;
34+
35+
public function __construct($items = [], Paginator $paginator = null)
36+
{
37+
$this->paginator = $paginator;
38+
parent::__construct($items);
39+
}
40+
41+
public static function make($items = [], Paginator $paginator = null)
42+
{
43+
return new static($items, $paginator);
44+
}
45+
46+
public function toArray()
47+
{
48+
if ($this->paginator) {
49+
try {
50+
$total = $this->total();
51+
} catch (Exception $e) {
52+
$total = null;
53+
}
54+
55+
return [
56+
'total' => $total,
57+
'per_page' => $this->listRows(),
58+
'current_page' => $this->currentPage(),
59+
'data' => parent::toArray(),
60+
];
61+
} else {
62+
return parent::toArray();
63+
}
64+
}
65+
66+
public function __call($method, $args)
67+
{
68+
if ($this->paginator && method_exists($this->paginator, $method)) {
69+
return call_user_func_array([$this->paginator, $method], $args);
70+
} else {
71+
throw new Exception('method not exists:' . __CLASS__ . '->' . $method);
72+
}
73+
}
74+
}

src/paginator/driver/Bootstrap.php

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
<?php
2+
// +----------------------------------------------------------------------
3+
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4+
// +----------------------------------------------------------------------
5+
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
6+
// +----------------------------------------------------------------------
7+
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8+
// +----------------------------------------------------------------------
9+
// | Author: zhangyajun <448901948@qq.com>
10+
// +----------------------------------------------------------------------
11+
12+
namespace think\paginator\driver;
13+
14+
use think\Paginator;
15+
16+
class Bootstrap extends Paginator
17+
{
18+
19+
/**
20+
* 上一页按钮
21+
* @param string $text
22+
* @return string
23+
*/
24+
protected function getPreviousButton($text = "&laquo;")
25+
{
26+
27+
if ($this->currentPage() <= 1) {
28+
return $this->getDisabledTextWrapper($text);
29+
}
30+
31+
$url = $this->url(
32+
$this->currentPage() - 1
33+
);
34+
35+
return $this->getPageLinkWrapper($url, $text);
36+
}
37+
38+
/**
39+
* 下一页按钮
40+
* @param string $text
41+
* @return string
42+
*/
43+
protected function getNextButton($text = '&raquo;')
44+
{
45+
if (!$this->hasMore) {
46+
return $this->getDisabledTextWrapper($text);
47+
}
48+
49+
$url = $this->url($this->currentPage() + 1);
50+
51+
return $this->getPageLinkWrapper($url, $text);
52+
}
53+
54+
/**
55+
* 页码按钮
56+
* @return string
57+
*/
58+
protected function getLinks()
59+
{
60+
if ($this->simple) {
61+
return '';
62+
}
63+
64+
$block = [
65+
'first' => null,
66+
'slider' => null,
67+
'last' => null,
68+
];
69+
70+
$side = 3;
71+
$window = $side * 2;
72+
73+
if ($this->lastPage < $window + 6) {
74+
$block['first'] = $this->getUrlRange(1, $this->lastPage);
75+
} elseif ($this->currentPage <= $window) {
76+
$block['first'] = $this->getUrlRange(1, $window + 2);
77+
$block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
78+
} elseif ($this->currentPage > ($this->lastPage - $window)) {
79+
$block['first'] = $this->getUrlRange(1, 2);
80+
$block['last'] = $this->getUrlRange($this->lastPage - ($window + 2), $this->lastPage);
81+
} else {
82+
$block['first'] = $this->getUrlRange(1, 2);
83+
$block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side);
84+
$block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
85+
}
86+
87+
$html = '';
88+
89+
if (is_array($block['first'])) {
90+
$html .= $this->getUrlLinks($block['first']);
91+
}
92+
93+
if (is_array($block['slider'])) {
94+
$html .= $this->getDots();
95+
$html .= $this->getUrlLinks($block['slider']);
96+
}
97+
98+
if (is_array($block['last'])) {
99+
$html .= $this->getDots();
100+
$html .= $this->getUrlLinks($block['last']);
101+
}
102+
103+
return $html;
104+
}
105+
106+
/**
107+
* 渲染分页html
108+
* @return mixed
109+
*/
110+
public function render()
111+
{
112+
if ($this->hasPages()) {
113+
if ($this->simple) {
114+
return sprintf(
115+
'<ul class="pager">%s %s</ul>',
116+
$this->getPreviousButton(),
117+
$this->getNextButton()
118+
);
119+
} else {
120+
return sprintf(
121+
'<ul class="pagination">%s %s %s</ul>',
122+
$this->getPreviousButton(),
123+
$this->getLinks(),
124+
$this->getNextButton()
125+
);
126+
}
127+
}
128+
}
129+
130+
/**
131+
* 生成一个可点击的按钮
132+
*
133+
* @param string $url
134+
* @param int $page
135+
* @return string
136+
*/
137+
protected function getAvailablePageWrapper($url, $page)
138+
{
139+
return '<li><a href="' . htmlentities($url) . '">' . $page . '</a></li>';
140+
}
141+
142+
/**
143+
* 生成一个禁用的按钮
144+
*
145+
* @param string $text
146+
* @return string
147+
*/
148+
protected function getDisabledTextWrapper($text)
149+
{
150+
return '<li class="disabled"><span>' . $text . '</span></li>';
151+
}
152+
153+
/**
154+
* 生成一个激活的按钮
155+
*
156+
* @param string $text
157+
* @return string
158+
*/
159+
protected function getActivePageWrapper($text)
160+
{
161+
return '<li class="active"><span>' . $text . '</span></li>';
162+
}
163+
164+
/**
165+
* 生成省略号按钮
166+
*
167+
* @return string
168+
*/
169+
protected function getDots()
170+
{
171+
return $this->getDisabledTextWrapper('...');
172+
}
173+
174+
/**
175+
* 批量生成页码按钮.
176+
*
177+
* @param array $urls
178+
* @return string
179+
*/
180+
protected function getUrlLinks(array $urls)
181+
{
182+
$html = '';
183+
184+
foreach ($urls as $page => $url) {
185+
$html .= $this->getPageLinkWrapper($url, $page);
186+
}
187+
188+
return $html;
189+
}
190+
191+
/**
192+
* 生成普通页码按钮
193+
*
194+
* @param string $url
195+
* @param int $page
196+
* @return string
197+
*/
198+
protected function getPageLinkWrapper($url, $page)
199+
{
200+
if ($this->currentPage() == $page) {
201+
return $this->getActivePageWrapper($page);
202+
}
203+
204+
return $this->getAvailablePageWrapper($url, $page);
205+
}
206+
}

0 commit comments

Comments
 (0)