Paginator

Paginator

There are several ways to paginate items. The simplest is by using the paginate method on the query builder.

Paginating Database Results

$users = DB::table(‘users‘)->paginate(15);

The argument passed to the paginate method is the number of items you wish to display per page. Once you have retrieved the results, you may display them on your view, and create the pagination links using the links method:

<div class="container">
    <?php foreach ($users as $user): ?>
        <?php echo $user->name; ?>
    <?php endforeach; ?>
</div>

<?php echo $users->links(); ?>

You may also access additional pagination information via the following methods:

  • getCurrentPage
  • getLastPage
  • getPerPage
  • getTotal
  • getFrom
  • getTo
  • count

"Simple Pagination"

If you are only showing "Next" and "Previous" links in your pagination view, you have the option of using the simplePaginate method to perform a more efficient query. This is useful for larger datasets when you do not require the display of exact page numbers on your view:

$someUsers = DB::table(‘users‘)->where(‘votes‘, ‘>‘, 100)->simplePaginate(15);

Creating A Paginator Manually

Sometimes you may wish to create a pagination instance manually, passing it an array of items. You may do so using the Paginator::make method:

$paginator = Paginator::make($items, $totalItems, $perPage);

Appending To Pagination Links

You can add to the query string of pagination links using the appends method on the Paginator:

<?php echo $users->appends(array(‘sort‘ => ‘votes‘))->links(); ?>

This will generate URLs that look something like this:

http://example.com/something?page=2&sort=votes

If you wish to append a "hash fragment" to the paginator‘s URLs, you may use the fragment method:

<?php echo $users->fragment(‘foo‘)->links(); ?>

This method call will generate URLs that look something like this:

http://example.com/something?page=2#foo

Full Example Usage

Users Model

namespace App\Models;

use Database\Model;

class Users extends Model
{
    /**
     * The table associated with the Model.
     *
     * @var string
     */
    protected $table = ‘users‘;

    /**
     * The primary key for the Model.
     *
     * @var string
     */
    protected $primaryKey = ‘id‘;

    /**
     * The number of Records to return for pagination.
     *
     * @var int
     */
    protected $perPage = 25;
}

Users Controller

namespace App\Controllers;

use Core\View;
use Core\Controller;

class Users extends Controller
{

    private $model;

    public function __construct()
    {
        parent::__construct();

        $this->model = new \App\Models\Users();
    }

    public function dashboard()
    {
        $users = $this->model->paginate();

        return View::make(‘Users/Dashboard‘)
            ->shares(‘title‘, ‘Dashboard‘)
            ->with(‘users‘, $users);
    }
}

Users Dashboard View

<?php foreach ($users->getItems() as $user): ?>
    <?php echo $user->username; ?>
<?php endforeach ?>

<?php echo $users->links() ?>
时间: 2024-10-05 23:31:14

Paginator的相关文章

django分页技术Paginator(进阶篇)

一.概述 在之前的另一篇博文中介绍了在django中进行分页的两种方法,可是说基本上实现了分页刷新的功能,但存留一个问题,那就是当页数多的时候,会出现所有页码排开的情况,美观性不好不说,更主要的是当页码多的时候,分页栏会变得很长.所以对之前的做法进行重构的同时,也实现了定义分页栏显示页码个数的功能. 最终效果: 本博文旨在深化理解Paginator的使用,当然熟悉后也可以实现"跳转","下5页"类似的功能. 二.Paginator 博文提到,在一个页面中使用多个表,

Django分页:使用django.core.paginator模块

#导入模块 from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage #获取使用paginator函数分页集中sql_result,每25条为一页 #sql_result 是元组数据 paginator = Paginator(sql_result, 25) #获取分页的数量 page_sum = paginator.num_pages after_range_num = 3 before_range_num

django分页技术django-pagination和Paginator

转载前还请注明出处:http://blog.csdn.net/gugugujiawei 一.概述 几乎所有的web应用,都需要分页功能,但分页技术总体来说分两种,一种是全部加载,存于浏览器的缓存中,一种是分页访问,部分加载.前一种方法优点是实现简单,在一些对项目要求不多的特定应用上用得较多,可以利用的插件譬如datatables,而后一种方法优点是不会占用服务器太多的缓存,对于数据量大的列表项必须用到这种技术.由于我现在的项目属于管理后台,管理来自成千上万用户的数据,因而只能用到第二种方法.初期

Django--分页器(paginator)、Django的用户认证、Django的FORM表单

分页器(paginator) >>> from django.core.paginator import Paginator >>> objects = ['john', 'paul', 'george', 'ringo'] >>> p = Paginator(objects, 2) >>> p.count #数据总数 4 >>> p.num_pages #总页数 2 >>> type(p.page

django分页技术paginator

分页是Web应用常用的手法,Django提供了一个分页器类Paginator(django.core.paginator.Paginator),可以很容易的实现分页的功能.该类有两个构造参数,一个是数据的集合,另一个是每页放多少条数据. 分页对象Paginator: 只需提供两个必要的参数,第一个就是用于展示的数据(object_list),第二参数就是每页显示的大小(per_page),提供了这两个参数,其他的都好说: Paginator.count:数据总量 Paginator.num_pa

Bootstrap Paginator分页插件使用示例

最近做的asp.netMVC项目中需要对数据列表进行分类,这个本来就是基于bootstrap开发的后台,因此也就想着bootstrap是否有分页插件呢,或者说是基于jquery支持的分页功能,这样整体的网站后台风格便能够统一,又不用自己去写一套分页的功能. 首先便是要下载Bootstrap Paginator了,github上便有这个的开源项目提供下载: https://github.com/lyonlai/bootstrap-paginator 首先视图的上面应该需要引入js和css文件,主要

javascript翻页小控件paginator

$(container).paginator({  totalrecords : totalRecords,  recordsperpage : recordsPerpage,  pagebtncount : pageBtnCount,  initval : currentPage,  next : '次へ',  prev : '前へ',  first : '',  last : '',  theme : '',  controlsalways : false,  onchange : func

PageHelper+Bootstrap Paginator实现分页效果

最近需要做一个分页,找了挺多的前端分页效果,发现Bootstrap Paginator分页插件效果不错,而我页面也是用BootStrap做的响应式页面,就做了实现一个简单的分页效果. PageHelper地址:https://github.com/pagehelper/Mybatis-PageHelper Bootstrap Paginator:https://github.com/lyonlai/bootstrap-paginator 在SpringBoot+Mybatis做分页 1.首先添加

CakePHP下使用paginator需要对多个字段排序的做法

原文:http://blog.csdn.net/kunshan_shenbin/article/details/7644603 CakePHP下使用paginator需要对多个字段排序的做法 2012-06-08 11:03 448人阅读 评论(0) 收藏 举报 有的时候在进行翻页时需要对多个字段进行排序. 但是CakePHP的paginator的sort方法默认只能对一个字段进行排序. 解决的办法有2种: 1. 在model中追加一个虚拟字段: http://book.cakephp.org/

jq.paginator分页插件稍加修改

样式一: 样式二: 此分页功能在jq.paginator分页插件上稍加修改. 必加模板html: <div id="jqPaginator"> <div id=""></div> </div> js: $.jqPaginator('#id', { totalPages: , visiblePages:, pagesize:, onPageChange: function (index) { //此为回调函数,在点击每个