Thursday, April 3, 2008

cakePHP : Pagination

If part of your application includes displaying lots of results, it's a good idea to give the user the possibility to view the results in digestable chunks and possibly to be able to sort the presented data. This tutorial will explain how, after copying a few files into your application, you can achieve this in very few lines of code.
You need almost no knowledge of cake to be able to make use of this tutorial :).

If you already have a table in mind that you want to add pagination to, read on; otherwise run through http://manual.cakephp.org/appendix/blog_tutorial to have some code to play with.

Setting Up

All of the files necessary are available here in the bakery.

Save this file http://bakery.cakephp.org/articles/view/67 as /app/controllers/components/pagination.php

Save this file http://bakery.cakephp.org/articles/view/68 as /app/views/helpers/pagination.php

Save this file http://bakery.cakephp.org/articles/view/69 as /app/views/elements/pagination.thtml


Create/modify the Controller

The only change necessary to use pagination is to include the component, the helper and call the component method "init" before the relavent find.
Controller Class:


class PostsController extends AppController
{
var $name = 'Posts'; // for PHP4 installs
var $components = array ('Pagination'); // Added
var $helpers = array('Pagination'); // Added

function index() {
$criteria=NULL;
list($order,$limit,$page) = $this->Pagination->init($criteria); // Added
$data = $this->Post->findAll($criteria, NULL, $order, $limit, $page); // Extra parameters added

$this->set('data',$data);
}
}
?>

Create/modify the View



To make use of pagination, include the element, and optionally modify your table headers to allow changing the sort order of results:
View Template:


Paginated Posts Index



$pagination->setPaging($paging); // Initialize the pagination variables
$th = array (
$pagination->sortBy('id'),
$pagination->sortBy('title'),
$pagination->sortBy('created')
); // Generate the pagination sort links
echo $html->tableHeaders($th); // Create the table headers with sort links if desired

foreach ($data as $output)
{
$tr = array (
$output['Post']['id'],
$html->link($output['Post']['title'], "/Posts/View/{$output['Post']['id']}"),
$output['Post']['created']
);
echo $html->tableCells($tr,array('class'=>'altRow'),array('class'=>'evenRow'));
}
?>

renderElement('pagination'); // Render the pagination element ?>


Adding Ajax updates



If you include the RequestHandler component, the AJAX helper in your controller and the prototype js file is loaded in your view - you get your updates by ajax. Yes, it's that simple. The div that will be updated by default is the "content" div, you can change this by specifying in the component (either directly, or at run time) which div to update. And yes, you can disable this automatic behaviour if required.
How to add Prototype

So how do you add the prototype library..? Well...
Modify your layout

PHP Snippet:

The Prototype JavaScript library is availble at http://prototype.conio.net/
put prototype.js in /app/webroot/js/
Add the JavaScript code inside the head tag

if(isset($javascript)):
echo $javascript->link('prototype.js');
endif;
?>

2 comments:

Anonymous said...

When you think of Hogan scarpe , what is the first thing that comes to mind? That's right, you are probably thinking about sports apparel. That is because Hogan scarpe donna has done a very good job of promoting its brand. We often seen sports celebrities, sponsored by hogan donna , performing on national sports events and displaying the Hogan scarpe uomo logo prominently on their sports wear.

There are two basic types of portable THIRTYSOMETHING player that you will find available. One type looks similar to a laptop computer and is a single screen, all in one unit. The other choice you have when it comes to THIRTYSOMETHING DVD players for on the go is a dual screen portable THIRTYSOMETHING DVD COLLECTION player.

The Coach handbags handbags are products that will last a very long time if taken care of. I have gotten Coach Bonnie that have fallen apart on me in one year. After 2 years now my Coach Wristlet is still in great condition. Maybe a little stained inside from all the chocolate wrappers, but Coach Ergo still look great!

Anonymous said...

Moreover, there are adored stones anchored to the brownish allotment of the hermes handbag
, which afresh adds to the cost. Kelly bag
add adorableness and adroitness to the personality of women. The Kelly bags
accept its different appearance of absorption the appearance account of the person.
Clinical trials of lida daidaihua
demonstrate encouraging results of this being overweight cure drugs. You get great discounts on your box of daidaihua
, the entire details and instructions to take these effective lida slimming
is clearly mentioned on the site, slimming capsule
that are easily available on the online stores. slimming capsules
are mostly safe and able but several affected and abortive articles actualize a bad name for all lida
So uk Tiffany
can be a perfect presentation of love. No matter ten years or thirty years, silver earrings
is as new as first seen. He ruled in July 2010 that eBay was not liable for trademark infringement by allowing Tiffany necklaces
to be sold on the website by individuals. silver necklaces
then appealed to the higher court.
However, you will accept to allow Chanel Wallet
of dollars to amuse your adult with Chanel handbags
and if you absolutely deceit allow to absorb such a huge amount. Alligator derma is admired as the toughest actual you can use for a replica Chanel handbags
.