LinkPager.php
3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
namespace app\ht\widgets;
use Yii;
use yii\widgets\LinkPager as YiiLinkPager;
/**
* 分页类
*
* @author Nemo Luo <378107001@qq.com>
* @date 2015-12-24
*/
class LinkPager extends YiiLinkPager
{
public $nextPageLabel = '下一页';
public $prevPageLabel = '上一页';
public $firstPageLabel = '首页';
public $lastPageLabel = '末页';
public $showSummary = true;
public $showJumpPage = true;
/**
* Executes the widget.
* This overrides the parent implementation by displaying the generated page buttons.
*/
public function run()
{
if ($this->registerLinkTags) {
$this->registerLinkTags();
}
$summary = $this->renderSummary();
// $perPage = $this->renderPerPage();
$perPage = '';
$button = $this->renderButtonBar();
$output = $perPage . $button;
$output = $this->showSummary ? $summary . $output : $output;
echo $output;
}
/**
* @return string
*/
protected function renderSummary()
{
$totalCount = $this->pagination->totalCount;
if ($totalCount == 0) {
return '';
}
$pageCount = $this->pagination->getPageCount();
$begin = $this->pagination->getPage() * $this->pagination->pageSize + 1;
$end = $begin + $this->pagination->pageSize - 1;
if ($totalCount < $this->pagination->pageSize) {
$end = $totalCount;
}
$output = "<div class='summary'>第<b>$begin - $end</b>条,共<b>$pageCount</b>页,共<b>$totalCount</b>条数据</div>";
return $output;
}
/**
* @return string
*/
protected function renderButtonBar()
{
$buttons = $this->renderPageButtons();
if (true == $this->showJumpPage && !empty($buttons)) {
$time = time();
$pid = 'p-'.$time.'-'.mt_rand(1000,9999);
$tplURL = $this->pagination->createUrl(0);
$tplURL = str_replace('page=1','page=',$tplURL);
$buttons = '<div class="pagination-bar">'.
'<div class="jump-page"><input type="number" min="1" id="'.$pid.'" value="" class="jump-page-input">'.
'<a class="jump-btn" data-href="'.$tplURL.'" onClick="window.location.href = this.getAttribute(\'data-href\').replace(\'page=\',\'page=\'+ document.getElementById(\''.$pid.'\').value) " href="javascript:void(0)">跳转</a></div>'.
$buttons .
'</div>';
}
//window.location.href= this.getAttribute('data-href')
return $buttons;
}
/**
* @return string
*/
protected function renderPerPage()
{
$totalCount = $this->pagination->totalCount;
if ($totalCount == 0) {
return '';
}
$request = Yii::$app->getRequest();
$params = $request->getQueryParams();
$params[0] = Yii::$app->controller->getRoute();
$urlManager = Yii::$app->getUrlManager();
$params['pageSize'] = 20;
$page20 = ($request->get('pageSize') == 20) ? '<b>20</b>' : '<a href="' . $urlManager->createUrl($params) . '">20</a>';
$params['pageSize'] = 50;
$page50 = ($request->get('pageSize') == 50) ? '<b>50</b>' : '<a href="' . $urlManager->createUrl($params) . '">50</a>';
$params['pageSize'] = 100;
$page100 = ($request->get('pageSize') == 100) ? '<b>100</b>' : '<a href="' . $urlManager->createUrl($params) . '">100</a>';
$output = '<div class="page-size">每页: ' . $page20 .$page50 . $page100 . '</div>';
return $output;
}
}