Adding Control to the Paginator

The paginator control allows you to display not only a next or previous button the user can use to move through the result set, but also provides functionality that allows you to display the total number of pages. This can be accomplished by using the paginationControl() method of Zend_Paginator. The function accepts three parameters:

• The initial parameter accepts a Zend_Paginator object.

• The second parameter accepts a string value representing the type of style to implement. Acceptable values are shown in Table 5-7.

• The third parameter accepts a string value representing the location of the view to use for the pagination control.

Table 5-7. Pagination Controller Styles

Scrolling

Description

Style

All

Displays all the pages available to the user regardless of whether you use

setPageRange().

Elastic

Displays an initial set of pages. As the user moves from page to page, the number of

pages displayed on the paginator control will expand.

Jumping

Displays a given range; moves on to the next range when the user gets to the end of

the first range.

Sliding

Displays the page range set using setPageRange(). Places the current page number as

close to the center as possible.

Each style displays the content and interacts in different ways with the user. Try out the different combinations to see which one you like.

Table 5-8. Zend_Paginator Properties

Property

Description

Return Type

first

Returns the initial page number in the set of pages.

String

firstltemNumber

Returns the index of the first record in the result set on the current page.

String

firstPagelnRange

Returns the first page in the scrolling range for the current page.

String

current

Returns the current page number in the scrolling page range.

String

currentltemCount

Returns the current number of records displayed in the current page.

String

itemCountPerPage

Returns the total number of records that can be displayed per page.

String

last

Returns the last page number in the total set of pages.

String

lastltemNumber

Returns the index of the last record on the current page.

String

lastPagelnRange

Returns the last page number in the current scrolling page range.

next

Returns the next page number to be fetched.

String pageCount

Returns the total number of pages.

String pagesInRange

Returns all the page numbers to display in the current scrolling Array page range. Key-value array: Value represents the page number.

previous

Returns the previous page number.

String totalltemCount

Returns the total number of records to paginate.

String

Using both tables, let's add a paginator control to the list.phtml view. You now need to create the view that the pagination control will implement. The view contains the look and feel for the pagination control and is the file that you need to modify to make the control look the way you want. Create a new file, paginationcontrol.phtml, in the directory includes/ and copy the code shown in Listing 5-38. The file is not required to live here; you are just saving it to this location to easily reuse the view for other paginator instances.

Listing 5-38. Pagination Control View <?php if($this->pageCount){ ?>

<!-- ADD PREVIOUS BUTTON --> <?php if(isset($this->previous)){ ?>

<td><a href="list?i=<?php echo $this->previous?>"> &lt;-

<!-- SHOW THE AVAILABLE PAGES --> <?php foreach($this->pagesInRange as $page){

<a href="list?i=<?php echo $page?>"><?php echo $page; ?></a> I

<td><?php echo $this->current; ?> I</td>

<!-- ADD NEXT BUTTON --> <?php if(isset($this->next)){ ?>

<td><a href="list?i=<?php echo $this->next?>">Next -&gt;</a></td> <?}?>

The pagination control shown in Listing 5-38 checks whether there are pages to paginate. If so, create the controls that contain a previous button and a next button. To display the number of pages the user can view at a single time, use the foreach() iteration function and iterate through each page. (Of course, you are not tied down to this layout and can design the pagination any way you want as long as you use the properties in Table 5-8 to control and display pages.)

Integrating the pagination control requires a single line to add anywhere you want to display the controls. Open the views/scripts/artist/list.phtml file and update the file as shown in Listing 5-39.

Listing5-39. list.phtml Update: Using paginationControl

<?php echo $this->doctype('XHTML1_STRICT'); ?>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang= "en" lang="en">

<?php echo $this->headTitle('LoudBite.com - Artist List'); ?> </head> <body>

<?php echo $this->render("includes/header.phtml")?>

echo " <tr><td><b>Total Artists:</b> ".

$this->paginator->getTotalItemCount()."</td></tr>";

foreach($this->paginator as $item){

echo "<tr><td>".$item."</td></tr>";

echo $this->paginationControl($this->paginator, 'Sliding',

'includes/paginationcontrol.phtml');

echo "</td></tr>"; }else{?> <tr>

<td>There were no artists present in the system. Add one now!</td> </tr>

Listing 5-39 builds on the example shown in Listing 5-38. Unlike the previous example, you append the call to render the paginator's control. You use the paginationControl() method to do so and pass in three parameters. The initial parameter is the object you created in the listAction: $paginator. The second parameter is the scrolling style from Table 5-7. The third parameter is the location of the pagination control view; in the example, it's in the includes folder. Load the URL http://localhost/artist/list to

Was this article helpful?

0 0

Post a comment