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() ?>
Paginator的更多相关文章
- bootstrap分页插件--Bootstrap Paginator的使用&AJAX版备份(可单独使用)
html部分: <ul class="pagination"></ul> <!--bootstrap3版本用ul包裹--> <div cl ...
- Bootstrap Paginator 分页插件参数介绍及使用
Bootstrap Paginator是一款基于Bootstrap的js分页插件,功能很丰富,个人觉得这款插件已经无可挑剔了.它提供了一系列的参数用来支持用户的定制,提供了公共的方法可随时获得插件状态 ...
- Bootstrap Paginator分页插件的使用
今天,我为大家带来的一款做得非常优秀的分页插件BootStrap Paginator,他是一款js插件,由于本人也是才刚刚搞出来的,所以暂时对它也不是特别了解,只能大楖告诉大家怎么使用.我这里使用的是 ...
- Bootstrap-分页插件Paginator
Bootstrap Paginator是一款基于Bootstrap的js分页插件,功能很丰富,个人觉得这款插件已经无可挑剔了.它提供了一系列的参数用来支持用户的定制,提供了公共的方法可随时获得插件状态 ...
- bootstrap paginator 与 bootstrap3兼容
bootstrap paginator可支持bootstrap2 和bootstrap3. 默认的下载包中支持2,需要手动修改才能支持bootstrap3.具体方法:找到bootstrap-pagin ...
- Bootstrap Paginator分页插件
Bootstrap Paginator分页插件使用示例 最近做的asp.netMVC项目中需要对数据列表进行分类,这个本来就是基于bootstrap开发的后台,因此也就想着bootstrap是否有分页 ...
- CakePHP下使用paginator需要对多个字段排序的做法
原文:http://blog.csdn.net/kunshan_shenbin/article/details/7644603 CakePHP下使用paginator需要对多个字段排序的做法 2 ...
- Django中扩展Paginator实现分页
Reference:https://my.oschina.net/kelvinfang/blog/134342 Django中已经实现了很多功能,基本上只要我们需要的功能,都能够找到相应的包.要在Dj ...
- Bootstrap Paginator分页插件+ajax 实现动态无刷新分页
之前做分页想过做淘宝的那个,但是因为是后台要求不高,就Bootstrap Paginator插件感觉还蛮容易上手,所以就选了它. Bootstrap Paginator分页插件下载地址: Downlo ...
随机推荐
- 在PowerDesigner中设计物理模型2——约束
唯一约束 唯一约束与创建唯一索引基本上是一回事,因为在创建唯一约束的时候,系统会创建对应的一个唯一索引,通过唯一索引来实现约束.不过唯一约束更直观的表达了对应列的唯一性,使得对应索引的目的更加清晰,所 ...
- Win10 查看IE的临时目录
C:\Users\YOUR_NAME\AppData\Local\Microsoft\Windows\INetCache
- BZOJ2038: [2009国家集训队]小Z的袜子(hose) 莫队算法
要使用莫队算法前提 ,已知[l,r]的答案,要能在logn或者O(1)的时间得到[l+1,r],[l-1,r],[l,r-1],[l,r+1],适用于一类不修改的查询 优美的替代品——分块将n个数分成 ...
- 怎么保存退出vi编辑
按ESC键 跳到命令模式,然后: :w 保存文件但不退出vi:w file 将修改另外保存到file中,不退出vi:w! 强制保存,不推出vi:wq 保存文件并退出vi:wq! 强制保存文件,并退出v ...
- 机器学习总结之逻辑回归Logistic Regression
机器学习总结之逻辑回归Logistic Regression 逻辑回归logistic regression,虽然名字是回归,但是实际上它是处理分类问题的算法.简单的说回归问题和分类问题如下: 回归问 ...
- mysql 清空表的两种方法
一.Delete DELETE FROM `table`; 二.Truncate TRUNCATE `table`; 第一种方法其实就是去掉where条件,没有了条件,也就是删除掉表里面的所有记录了: ...
- Struts2 url传递中文出现乱码
项目所有的编码都改为了utf-8.在tomcat的 server.xml中修改下面这段 <Connector port="8080" protocol="HTTP/ ...
- emWin -- 模拟器系列1 - 如何建立模拟器开发环境
面对如此强大的emWin,大家是否都有跃跃欲试的冲动呢?但是没有硬件可以调试的童鞋,难道只能望洋兴叹?非也.非也.Segger公司早就考虑到了.Segger推出模拟器的目的不仅仅是为了解决没有硬件的烦 ...
- 已知有一个Worker 类如下: public class Worker { private int age; private String name; private double salary; public Worker (){} public Worker (String nam
package homework006; public class Worker { private int age; private String name; private double sala ...
- Jsp中的pageContext对象
这个对象代表页面上下文.组要用于访问页面共享数据.使用pageContext可以直接访问request,session,application范围的属性,看看这些jsp的页面: JSP 页面使用 pa ...