Some time ago, I was puzzled by the search for a solution that would replace the standard "Padzhinator" (CLinkPager) Yii with one that, instead of page numbers, would write something more distinct. Let's say the first characters of the field values on the following pages. I did not find it and decided to write my own.

It is extremely inconvenient to switch between numbered pages, for example, in the list of users. Especially when the list is large. Each switch when searching for a user of 5-10 pages, like “fortune telling on the coffee grounds”. Then skip the desired user, then on the contrary too cautious and you will not reach. Of course, searching helps, but only if you know for sure the last name or other attributes. Even knowing the name does not help, because Different Sash, Mash, Dash, Van can be in the table dozens!
The search for a turnkey solution, as I wrote above, did not give anything. Maybe I was looking bad, correct, if not right.
Task
I had to study the structure of
CLinkPager and write my class "Padzhinator".
The requirements for it were simple:
')
- display of values, not page numbers
- display of whole values or fragment
- display values of only certain fields.
Displaying values, not page numbers - means that you need to display the first value of the field of each page. In other words, the field value of the current page record, then the field value of the next page record (with an offset equal to the page size), etc.
The display of values in whole or a fragment means that it is inconvenient to display, for example, the whole surname in the padzhinator. It is much more convenient to write, say, the first letter, and preferably two, in general, as much as necessary.
The display of the values of only certain fields means that it will not add pieces of field values such as number or date of convenience to the Padzhinator, though, to whom, how. Therefore, it is necessary to specify a list of fields, the values of which will be displayed in the “padzhinator”, and for the rest, to show the usual page numbers.
Decision
I found out that the function createPageButtons () of the CLinkPager class is responsible for drawing the buttons. It is impossible to selectively block the creation code of any buttons, so I decided to inherit the class and supplement the code.
It is interesting that the base class CLinkPager is obtained as if it is universal “for itself” for any data provider. I also had to handle two data provider classes:
CActiveDataProvider ,
CArrayDataProvider . Each separately.
When working with CActiveDataProvider, I get the values from each page using offset:
if ($this->owner->dataProvider instanceof CActiveDataProvider) { $pagesize = $this->owner->dataProvider->pagination->pagesize; $criteria = $this->owner->dataProvider->criteria; $this->owner->dataProvider->sort->applyOrder($criteria); $criteria->limit = 1; for($i=$beginPage;$i<=$endPage;++$i) { $criteria->offset = $i*$pagesize; $m = $this->owner->dataProvider->model->find($criteria); $buttons[]=$this->createPageButton(is_null($this->length) ? $m->{$field} : mb_substr($m->{$field}, 0, $this->length, 'utf-8'), $i,$this->internalPageCssClass,false,$i==$currentPage); } }
When working with CArrayDataProvider, getting values from each page I get by the index:
if ($this->owner->dataProvider instanceof CArrayDataProvider) { $pagesize = $this->owner->dataProvider->pagination->pagesize; $data = $this->owner->dataProvider->rawData; for($i=$beginPage;$i<=$endPage;++$i) { $m = $data[$i*$pagesize]; $buttons[]=$this->createPageButton(is_null($this->length) ? $m[$field] : mb_substr($m[$field], 0, $this->length, 'utf-8'), $i,$this->internalPageCssClass,false,$i==$currentPage); } }
In the class declared two variables:
fields - an array of field names for which values should be displayed, not page numbers;
length - the length of the trim value, if null, then the whole.
You can use a class, for example, in a CGridView like this:
'pager'=>array( 'class' => 'AlxdTitlePager', 'fields'=>array('lastname','firstname','middlename','email'), 'length'=>'2', 'maxButtonCount'=>10, 'firstPageLabel'=>'<<', 'header'=>'', 'hiddenPageCssClass'=>'disabled', 'lastPageLabel'=>'>>', 'nextPageLabel'=>'>', 'selectedPageCssClass'=>'active', 'prevPageLabel'=>'<', 'htmlOptions'=>array('class'=>'pagination') )
In general, having run a bit, the code decided to share it. Who
cares , can get the source code of my class
AlxdTitlePager for free.