How to change CLinkPager <a>text</a>

I need to change the anchor text of generated by CLinkPager.

Change from this



<?php


<li class="page selected"><a href="/project/index.php?r=bin/list">1</a></li>


<li class="page"><a href="/project/index.php?r=bin/list&amp;page=2">2</a></li>


?>


to this



<?php


<li class="page selected"><a href="/project/index.php?r=bin/list">12x102</a></li>


<li class="page"><a href="/project/index.php?r=bin/list&amp;page=2">12x114</a></li>


?>


The text will vary depending on the type of timber dimensions that gets passed to the view

How do I do this?

I’m a bit afraid, this seems to be hardcoded in Yii. I did my investigation: CLinkPager calls CBasePager which calls CPagination which calls CController which calls Yii::app()->createurl() finally. I wasn’t able to figure out what modul this represents since it is magically set.

However, instead of digging more deeper in the framework source, I recommend you to extend CLinkPager or CBasePager or CPagination or CController or Yii, whichever fits your needs. :D But being serious, I’m looking forward to seeing the results.

(I believe the core method ultimately calls CHtml. You may see via debug messages if you wish.)

Seems like I have a little homework to do  :P

I'll have a look into it, it might take some time though

qiang, are you interested in giving us some advice?  :D

You need to extend CLinkPager and override its createPageButton() method.

Thought it wasn’t gonna be easy  :D How could I passed parameters to this function and implement it from a view.

You don’t have to do all the work qiang, just point me in the right direction  ;)

It's easier than you thought. Write a new class MyLinkPager by extending from CLinkPager and save the class in protected/components/MyLinkPager.php

In MyLinkPager, override createPageButton method as follows:



protected function createPageButton($label,$page,$class,$hidden,$selected)


{


    // refer to CLinkPager::createPageButton implementation


}


qianq…you are the man  ;D Thanx alot

Hooray, I managed to crack this little sucker  ;D

Here's my scenario

I have a model called Bin, which holds bundles of Rawmaterial (timber). A Bin has a particular size which is stored in the the bin table as sizeId, every sizeId has a text value, for example 50x114, which is the dimensions for a piece of timber. I wanted the text value of the size to be the internal pages' label attribute value. qiang, I know you said to override the createPageButton(…) method, but I ended up overriding the createPageButtons() method so that the section about the internal pages could be exposed.

I'll explain in more detail below, exactly how I did this

Controller[Bin]

To get a all Bins, with sizes order from small to large, I set the $criteria->order. Even though, I already have $bins object with sizes returned from the db, it unfortunately does not give me all the sizes back because of the limit that is being applied to CPagination, so I created another CDbCriteria object, called $criteriaSizes, and also ordered it in ascending order, the same as $criteria's. I stored the results from $criteriaSizes in a $sizes array with numbered array keys and the corresponding sizeText value from a helper class I've written, following the Yii Blog example.

The $sizes array looked something like this

I then passed the sizes array to the list view, and then passed it on to the widget that creates the pagination

View[bin/list]

Then I extended the CLinkPager class and override the createPageButtons() method and declared a public variable called sizes, of type array.

Class[MyLinkPager]

I passed the $sizes array as $label parameter to the createPageButon() method, with the for loop iterator to match the correct size text from the $sizes array, and voila, new labels  ;)

NOTE: This will only work if PAGE_SIZE in your controller is set to 1

Here's a screenshot below

Nice job!

I’m a little bit concerned because of the estimated amount of queries. How does this piece of code perform? I’m afraid its performance is going to degrade as data is added to the table.

Quote

I'm a little bit concerned because of the estimated amount of queries. How does this piece of code perform? I'm afraid its performance is going to degrade as data is added to the table.

Here's my Application Log

Convincing. Although, I’d like to see the performance with a few thousand rows. :) The SQL in the foreach seems to demand more resource with increasing number of iterations.

pestaa, I totally agree with you, but for my specific use case there can only be 30 sizes, so there will be 30 parent rows, and I don't foresee the child rows to be more than 10-15 for each parent

Oh, yes, I understand your calmness now, forgive my concerns; I had thought only in theory. :) Thanks for the investigation.