pagination.phpを使ったページング

cakePHPでページングをするです。http://bakery.cakephp.org/articles/view/paginationを見て3つのソースファイルをダウンロードしてviewとcontrollerを変更します。

簡単な説明

コントローラへの変更

<?php
class PostsController extends AppController
{
    var $name = 'Posts'; // for PHP4 installs
    var $components = array ('Pagination'); // http://bakery.cakephp.org/articles/view/67  からダウンロードしたコンポーネントを使う宣言
    var $helpers = array('Pagination'); //  http://bakery.cakephp.org/articles/view/68 からダウンロードしたヘルパーを使う宣言

    function index() {
        $criteria=NULL; // WHERE句に使う条件
        list($order,$limit,$page) = $this->Pagination->init($criteria); // コンポーネントの初期化
        $data = $this->Post->findAll($criteria, NULL, $order, $limit, $page); // コンポーネントで初期化された条件を使ってDBから読み込み

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

viewを変更

<h1>Paginated Posts Index</h1>
<table>
<?php
$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']
        ); // 1行分のtrを作成
    echo $html->tableCells($tr,array('class'=>'altRow'),array('class'=>'evenRow'));
}
?>
</table>
<? echo $this->renderElement('pagination'); // Render the pagination element http://bakery.cakephp.org/articles/view/69 からダウンロードしたelementを使ってページ番号のリンクを作成
 ?>

その他、判ったこと

  • app/webroot/img/nav/arrowleft.gifとapp/webroot/img/nav/arrowright.gifを用意すると次ページ、前ページを意味するイメージが表示される。変更したい場合は /app/views/elements/pagination.thtml を編集すればよい
  • 1ページに表示する件数を指定するにはコントローラで $this->Pagination->init($criteria) 前で $this->Pagination->show=件数 とする
  • 勝手にajaxとして動作する場合はコントローラーで $this->Pagination->ajaxAutoDetect=false とする。判断は /app/controllers/components/pagination.php の_checkAjax()が行なっている

Trackback URL

Leave a comment

Your comment