Kohana 3.2 中最小、完整的分页示例是什么?

发布于 2024-12-28 14:49:10 字数 96 浏览 0 评论 0原文

我在 Kohana 3.2 中找到了很多有关分页的信息,但其中大部分分散在论坛评论和博客文章中,没有单一完整的来源可供参考。

(注:我本来打算自我回答这个问题)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

司马昭之心 2025-01-04 14:49:10

这对我有用:

  1. https://github.com/kloopko/kohana-pagination 下载分页模块(分页已从 Kohana 3.2 中删除,因此这是一个改编模块)。
  2. 将模块安装在modules/pagination中。
  3. 在bootstrap.php中添加模块:

    Kohana::modules(数组(
        // ...其他模块...
        '分页' => MODPATH.'分页'
    ));
    
  4. 将配置文件从modules/pagination/config/pagination.php复制到application/config/pagination.php。

  5. 将以下操作添加到您的控制器:

     public function action_index() {
         // 默认跳转到第一页
         $this->request->redirect('yourcontroller/page/?page=1');
     }
    
     公共函数action_page() {
         $orm = orm::factory('your_orm');
    
         $分页 = 分页::工厂(数组(
             'total_items'=>; $orm->count_all(),
             'items_per_page' =>; 20、
             )
         );
    
         // 将控制器和操作名称显式传递给 $pagination 对象
         $pagination->route_params(array('controller' => $this->request->controller(), 'action' => $this->request->action())); 
         // 获取数据
         $data = $orm->offset($pagination->offset)->limit($pagination->items_per_page)->find_all()->as_array();
         // 将数据和验证对象传递给视图
         echo View::factory('yourview/page', array('data' => $data, 'pagination' => $pagination));
     }
    
  6. 创建您的视图/页面,如下所示:

    <前><代码>

  7. 根据您的需要修改 application/config/pagination.php。我必须将 'view' 参数更改为 'pagination/floating',当页面列表太大时,它会显示省略号 (...),这与默认的 < code>'pagination/basic' 列出所有页面,无论长度如何。

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.

梦纸 2025-01-04 14:49:10

Kohana 3.2 最初不支持分页。幸运的是,有人更新了该模块,您可以在 https://github.com/kloopko/kohana 获取代码-分页

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文