Pagination - Phân trang trong cakephp 1.1.x

Pagination - Phân trang trong cakephp 1.1.x

Đối với cakephp 1.1.x, hiện tại chưa hỗ trợ sẵn việc phân trang giống như trong cakephp 1.2. Đối với cakephp 1.2 bạn chỉ việc dùng sẵn helper là có thể phân trang được. Với bản cakephp 1.1 việc phân trang sẽ do chúng ta tự làm. Sau đây tôi xin hướng dẫn các bạn phân trang trong cakephp bằng components và helper của Andy Dawson.

Cài đặt:

- Bạn lưu file http://i-php.net/2008/10/pagination-component/ thành /app/controllers/components/pagination.php

- Bạn lưu file http://i-php.net/2008/10/pagination-helper/ thành /app/views/helpers/pagination.php

- Bạn lưu file http://i-php.net/2008/10/pagination-element/ thành /app/views/elements/pagination.thtml

Sử dụng trong controller

Trong controller, khi nào muốn sử dụng hàm phân trang thì ta chỉ việc sử dụng components và helper pagination mà chúng ta vừa tạo ra ở trên. Ta sẽ khởi tạo chúng trước khi thực hiện lấy dữ liệu. Ví dụ như chúng ta sẽ sử dụng chúng trong controller Post như sau:

    <?php
    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);
    }
    }
    ?>

Sử dụng trong views

Ta sẽ tạo ra một views post mà sử dụng phân trang ở trên.

    <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']
    );
    echo $html->tableCells($tr,array('class'=>'altRow'),array('class'=>'evenRow'));
    }
    ?>
    </table>
    <? echo $this->renderElement('pagination'); // Render the pagination element ?>

Trong view trên ta có sử dụng hàm sortBy() trong helper Pagination, hàm này sẽ sắp xếp lại dữ liệu theo các trường trong model khi ta kích vào đấy theo thứ tự tăng dần hoặc giảm dần. Hàm setPaging() sẽ xác định xem hiện ta đang ở trang nào. Việc hiện thị các trang cũng như các tuỳ chọn của trang sẽ được thực hiện qua hàm renderElement(), hàm này sẽ gọi pagination.thml trong element ra để thực hiện hiển thị chức năng phân trang.

Tags: 
Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Bình luận (0)

 

Add Comment

Filtered HTML

  • Các địa chỉ web và email sẽ tự động được chuyển sang dạng liên kết.
  • Các thẻ HTML được chấp nhận: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Tự động ngắt dòng và đoạn văn.

Plain text

  • No HTML tags allowed.
  • Các địa chỉ web và email sẽ tự động được chuyển sang dạng liên kết.
  • Tự động ngắt dòng và đoạn văn.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Facebook lại thay đổi thuật toán, các fanpage cần chú ý ngay

Facebook lại thay đổi thuật toán, các fanpage cần chú ý ngay

Facebook vừa mới cho biết họ sẽ mang những mẫu tin chất lượng hơn đến với bảng tin News Feed 

Apple’s Black Friday Deals Revealed

Apple’s Black Friday Deals Revealed

Yesterday, we started seeing Black Friday banners pop up on Apple.com. The top of the image read “Every gift list has its day,” and was followed by a request for customers to return to Apple’s...

Bí quyết 1 - Multilingual Site trong Drupal 7

Bí quyết 1 - Multilingual Site trong Drupal 7

As we discussed in our first introductory blog post, Recipes for Building a Multilingual Site, not all Drupal installations are the same and nearly every site built with Drupal is different in one way or another

Công ty diệt chuột T&C

 

Diet con trung