Frage

I found much information about pagination in Kohana 3.2 but most of it is scattered across forum comments and blog posts with no single complete source to refer to.

(note: I intended to self answer this question)

War es hilfreich?

Lösung

This is what worked for me:

  1. Download the pagination module from https://github.com/kloopko/kohana-pagination (pagination was removed from Kohana 3.2, so this is an adapted module).
  2. Install the module in modules/pagination.
  3. Add the module in bootstrap.php:

    Kohana::modules(array(
        // ... other modules ...
        'pagination' => MODPATH.'pagination'
    ));
    
  4. Copy the configuration file from modules/pagination/config/pagination.php to application/config/pagination.php.

  5. Add the following actions to your controller:

     public function action_index() {
         // Go to first page by default
         $this->request->redirect('yourcontroller/page/?page=1');
     }
    
     public function action_page() {
         $orm = orm::factory('your_orm');
    
         $pagination = Pagination::factory(array(
             'total_items' => $orm->count_all(),
             'items_per_page' => 20,
             )
         );
    
         // Pass controller and action names explicitly to $pagination object
         $pagination->route_params(array('controller' => $this->request->controller(), 'action' => $this->request->action())); 
         // Get data
         $data = $orm->offset($pagination->offset)->limit($pagination->items_per_page)->find_all()->as_array();
         // Pass data and validation object to view
         echo View::factory('yourview/page', array('data' => $data, 'pagination' => $pagination));
     }
    
  6. Create yourview/page as follows:

    <?php
    foreach($data as $item) {
        // ...put code to list items here 
    }
    
    // Show links
    echo $pagination;
    
  7. Modify application/config/pagination.php according to your needs. I had to change the 'view' parameter to 'pagination/floating' which displays ellipses (...) when the list of pages is too large, unlike the default 'pagination/basic' which lists all pages regardless of length.

Andere Tipps

Pagination wasn't originally working/supported in Kohana 3.2. Luckily, somebody has updated the module and you can get the code at https://github.com/kloopko/kohana-pagination

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top