服务器之家

服务器之家 > 正文

php 重写分页器 CLinkPager的实例

时间:2021-07-13 16:45     来源/作者:项象多

php 重写分页器 CLinkPager的实例

1、自定义的分页器类放在哪里?

有两个位置可以放,

第一种是放在 protected/extensions 中,在使用是import进来,或在config文件中import进来;

第二种是放在 protected/components 中,作为组件存在,不需要import

2、用派生方式是最好的

?
1
class MyPager extends CLinkPager

入口函数是:public function run() ,当显示分页器时run()被调用,里面的输出就会显示在相应位置;

其他的完全自定义,如果你不知道上一页、下一页、首页、尾页、总页数、当前页码等信息,可以参考CLinkPager的源码,yii/frameworks/web/widgets/pagers/CLinkPager.php

?
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
 
class MyPager extends CLinkPager
{
  const CSS_FIRST_PAGE='first';
  const CSS_LAST_PAGE='last';
  const CSS_PREVIOUS_PAGE='previous';
  const CSS_NEXT_PAGE='next';
  const CSS_INTERNAL_PAGE='page';
  const CSS_HIDDEN_PAGE='hidden';
  const CSS_SELECTED_PAGE='selected';
 
  /**
   * @var string the CSS class for the first page button. Defaults to 'first'.
   * @since 1.1.11
   */
  public $firstPageCssClass=self::CSS_FIRST_PAGE;
  /**
   * @var string the CSS class for the last page button. Defaults to 'last'.
   * @since 1.1.11
   */
  public $lastPageCssClass=self::CSS_LAST_PAGE;
  /**
   * @var string the CSS class for the previous page button. Defaults to 'previous'.
   * @since 1.1.11
   */
  public $previousPageCssClass=self::CSS_PREVIOUS_PAGE;
  /**
   * @var string the CSS class for the next page button. Defaults to 'next'.
   * @since 1.1.11
   */
  public $nextPageCssClass=self::CSS_NEXT_PAGE;
  /**
   * @var string the CSS class for the internal page buttons. Defaults to 'page'.
   * @since 1.1.11
   */
  public $internalPageCssClass=self::CSS_INTERNAL_PAGE;
  /**
   * @var string the CSS class for the hidden page buttons. Defaults to 'hidden'.
   * @since 1.1.11
   */
  public $hiddenPageCssClass=self::CSS_HIDDEN_PAGE;
  /**
   * @var string the CSS class for the selected page buttons. Defaults to 'selected'.
   * @since 1.1.11
   */
  public $selectedPageCssClass=self::CSS_SELECTED_PAGE;
  /**
   * @var integer maximum number of page buttons that can be displayed. Defaults to 10.
   */
  public $maxButtonCount=10;
  /**
   * @var string the text label for the next page button. Defaults to 'Next >'.
   */
  public $nextPageLabel;
  /**
   * @var string the text label for the previous page button. Defaults to '< Previous'.
   */
  public $prevPageLabel;
  /**
   * @var string the text label for the first page button. Defaults to '<< First'.
   */
  public $firstPageLabel;
  /**
   * @var string the text label for the last page button. Defaults to 'Last >>'.
   */
  public $lastPageLabel;
  /**
   * @var string the text shown before page buttons. Defaults to 'Go to page: '.
   */
  public $header;
  /**
   * @var string the text shown after page buttons.
   */
  public $footer='';
  /**
   * @var mixed the CSS file used for the widget. Defaults to null, meaning
   * using the default CSS file included together with the widget.
   * If false, no CSS file will be used. Otherwise, the specified CSS file
   * will be included when using this widget.
   */
  public $cssFile;
  /**
   * @var array HTML attributes for the pager container tag.
   */
  public $htmlOptions=array();
 
  /**
   * Initializes the pager by setting some default property values.
   */
  public function init()
  {
    if($this->nextPageLabel===null)
      $this->nextPageLabel=Yii::t('yii','Next >');
    if($this->prevPageLabel===null)
      $this->prevPageLabel=Yii::t('yii','< Previous');
    //if($this->firstPageLabel===null)
    // $this->firstPageLabel=Yii::t('yii','<< First');
    //if($this->lastPageLabel===null)
    // $this->lastPageLabel=Yii::t('yii','Last >>');
    if($this->header===null)
      $this->header=Yii::t('yii','Go to page: ');
 
    if(!isset($this->htmlOptions['id']))
      $this->htmlOptions['id']=$this->getId();
    if(!isset($this->htmlOptions['class']))
      $this->htmlOptions['class']='yiiPager';
  }
 
  /**
   * Executes the widget.
   * This overrides the parent implementation by displaying the generated page buttons.
   */
  public function run()
  {
    $this->registerClientScript();
    $buttons=$this->createPageButtons();
    if(empty($buttons))
      return;
    echo $this->header;
//   echo CHtml::tag('ul',$this->htmlOptions,implode("\n",$buttons));
    echo implode("\n",$buttons);
    echo $this->footer;
  }
 
  /**
   * Creates the page buttons.
   * @return array a list of page buttons (in HTML code).
   */
  protected function createPageButtons()
  {
    if(($pageCount=$this->getPageCount())<=1)
      return array();
 
    list($beginPage,$endPage,$ellipsis)=$this->getPageRange();
 
    $currentPage=$this->getCurrentPage(false); // currentPage is calculated in getPageRange()
    $buttons=array();
 
    // first page
    //$buttons[]=$this->createPageButton($this->firstPageLabel,0,$this->firstPageCssClass,$currentPage<=0,false);
 
    // prev page
    if(($page=$currentPage-1)<0)
      $page=0;
    if($currentPage == 0){
      $buttons[] = "<span style='background:#a3a3a3'><上一頁</span>";
    }else{
      $buttons[]=$this->createPageButton($this->prevPageLabel,$page,$this->previousPageCssClass,$currentPage<=0,false);
    }
    // internal pages start
    // first
    $buttons[]=$this->createPageButton(1,0,$this->internalPageCssClass,false,$i==$currentPage);
    //middle
    if($ellipsis == 'both'){
      $buttons[] = "<span style='background:#a3a3a3'>...</span>";
    }
    for($i=$beginPage;$i<=$endPage;++$i){
      if($ellipsis == 'left' && $i == $beginPage){
        $buttons[] = "<span style='background:#a3a3a3'>...</span>";
      }
      $buttons[]=$this->createPageButton($i+1,$i,$this->internalPageCssClass,false,$i==$currentPage);
      if($ellipsis == 'right' && $i == $endPage){
        $buttons[] = "<span style='background:#a3a3a3'>...</span>";
      }
    
    if($ellipsis == 'both'){
      $buttons[] = "<span style='background:#a3a3a3'>...</span>";
    }
    // last
    $buttons[]=$this->createPageButton($pageCount,$pageCount - 1,$this->internalPageCssClass,false,$i==$currentPage);
    // internal pages end
    // next page
    if(($page=$currentPage+1)>=$pageCount-1)
      $page=$pageCount-1;
    if($currentPage == ($pageCount-1)){
      $buttons[] = "<span style='background:#a3a3a3'>下一頁></span>";
    }else{
      $buttons[]=$this->createPageButton($this->nextPageLabel,$page,$this->nextPageCssClass,$currentPage>=$pageCount-1,false);
    }
    // last page
    //$buttons[]=$this->createPageButton($this->lastPageLabel,$pageCount-1,$this->lastPageCssClass,$currentPage>=$pageCount-1,false);
 
    return $buttons;
  }
 
  /**
   * Creates a page button.
   * You may override this method to customize the page buttons.
   * @param string $label the text label for the button
   * @param integer $page the page number
   * @param string $class the CSS class for the page button.
   * @param boolean $hidden whether this page button is visible
   * @param boolean $selected whether this page button is selected
   * @return string the generated button
   */
  protected function createPageButton($label,$page,$class,$hidden,$selected)
  {
    if($hidden || $selected)
      $class.=' '.($hidden ? $this->hiddenPageCssClass : $this->selectedPageCssClass);
    if ($selected) {
      $result = "<span>" . ++$page . "</span>";
    } else {
      $result = CHtml::link($label,$this->createPageUrl($page));
    }
    return $result;
  }
 
  /**
   * @return array the begin and end pages that need to be displayed.
   */
  protected function getPageRange()
  {
    $currentPage=$this->getCurrentPage();
    $pageCount=$this->getPageCount();
    /*$beginPage=max(0, $currentPage-(int)($this->maxButtonCount/2));
    if(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount)
    {
      $endPage=$pageCount-1;
      $beginPage=max(0,$endPage-$this->maxButtonCount+1);
    }*/
    if($pageCount > $this->maxButtonCount){
      if($currentPage > 4 && $currentPage < ($pageCount - 4)){
        // print_r('a');
        $beginPage = $currentPage - 2;
        $endPage = $currentPage + 2;
        $ellipsis = 'both';
      }else{
        $beginPage=max(1, $currentPage-(int)($this->maxButtonCount/2));
        if($beginPage == 1){
          $ellipsis = 'right';
        }else{
          $ellipsis = 'left';
        }
        if(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount)
        {
          // print_r('b');
          $endPage=$pageCount-2;
          $beginPage=max(1,$endPage-$this->maxButtonCount+1);
        }elseif(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount-2){
          // print_r('c');
          $endPage=$pageCount-2;
        }
 
      }
    }else{
      $beginPage=max(1, $currentPage-(int)($this->maxButtonCount/2));
      if(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount)
      {
        $endPage=$pageCount-2;
        $beginPage=max(1,$endPage-$this->maxButtonCount+1);
      }
    }
 
    return array($beginPage,$endPage, $ellipsis);
  }
 
  /**
   * Registers the needed client scripts (mainly CSS file).
   */
  public function registerClientScript()
  {
    if($this->cssFile!==false)
      self::registerCssFile($this->cssFile);
  }
 
  /**
   * Registers the needed CSS file.
   * @param string $url the CSS URL. If null, a default CSS URL will be used.
   */
  public static function registerCssFile($url=null)
  {
    if($url===null)
      $url=CHtml::asset(Yii::getPathOfAlias('system.web.widgets.pagers.pager').'.css');
    Yii::app()->getClientScript()->registerCssFile($url);
  }
}

3、调用方式

在View里的相应widget,定义pager的class为自定义的分页器类名即可,参考:

?
1
2
3
4
5
6
7
$this->widget('zii.widgets.CListView', array(
  'dataProvider'=>$dataProvider,
  'itemView'=>'_view_t',
  'pager'=>array(
  'class'=>'MyPager',
 )
));

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://blog.csdn.net/u012603025/article/details/50679853

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部