完善分页

This commit is contained in:
yunwuxin
2017-01-16 14:41:39 +08:00
parent 771c0a90be
commit b4290f0d01
5 changed files with 203 additions and 165 deletions

View File

@@ -13,7 +13,6 @@ namespace think;
use think\db\Connection; use think\db\Connection;
use think\db\Query; use think\db\Query;
use think\paginator\Collection as PaginatorCollection;
/** /**
* Class Db * Class Db
@@ -25,21 +24,21 @@ use think\paginator\Collection as PaginatorCollection;
* @method Query union(mixed $union, boolean $all = false) static UNION查询 * @method Query union(mixed $union, boolean $all = false) static UNION查询
* @method Query limit(mixed $offset, integer $length = null) static 查询LIMIT * @method Query limit(mixed $offset, integer $length = null) static 查询LIMIT
* @method Query order(mixed $field, string $order = null) static 查询ORDER * @method Query order(mixed $field, string $order = null) static 查询ORDER
* @method Query cache(mixed $key = true , integer $expire = null) static 设置查询缓存 * @method Query cache(mixed $key = null , integer $expire = null) static 设置查询缓存
* @method mixed value(string $field) static 获取某个字段的值 * @method mixed value(string $field) static 获取某个字段的值
* @method array column(string $field, string $key = '') static 获取某个列的值 * @method array column(string $field, string $key = '') static 获取某个列的值
* @method Query view(mixed $join, mixed $field = null, mixed $on = null, string $type = 'INNER') static 视图查询 * @method Query view(mixed $join, mixed $field = null, mixed $on = null, string $type = 'INNER') static 视图查询
* @method mixed find(mixed $data = []) static 查询单个记录 * @method mixed find(mixed $data = null) static 查询单个记录
* @method mixed select(mixed $data = []) static 查询多个记录 * @method mixed select(mixed $data = null) static 查询多个记录
* @method integer insert(array $data, boolean $replace = false, boolean $getLastInsID = false, string $sequence = null) static 插入一条记录 * @method integer insert(array $data, boolean $replace = false, boolean $getLastInsID = false, string $sequence = null) static 插入一条记录
* @method integer insertGetId(array $data, boolean $replace = false, string $sequence = null) static 插入一条记录并返回自增ID * @method integer insertGetId(array $data, boolean $replace = false, string $sequence = null) static 插入一条记录并返回自增ID
* @method integer insertAll(array $dataSet) static 插入多条记录 * @method integer insertAll(array $dataSet) static 插入多条记录
* @method integer update(array $data) static 更新记录 * @method integer update(array $data) static 更新记录
* @method integer delete(mixed $data = []) static 删除记录 * @method integer delete(mixed $data = null) static 删除记录
* @method boolean chunk(integer $count, callable $callback, string $column = null) static 分块获取数据 * @method boolean chunk(integer $count, callable $callback, string $column = null) static 分块获取数据
* @method mixed query(string $sql, array $bind = [], boolean $fetch = false, boolean $master = false, mixed $class = false) static SQL查询 * @method mixed query(string $sql, array $bind = [], boolean $fetch = false, boolean $master = false, mixed $class = null) static SQL查询
* @method integer execute(string $sql, array $bind = [], boolean $fetch = false, boolean $getLastInsID = false, string $sequence = null) static SQL执行 * @method integer execute(string $sql, array $bind = [], boolean $fetch = false, boolean $getLastInsID = false, string $sequence = null) static SQL执行
* @method PaginatorCollection paginate(integer $listRows = 15, mixed $simple = false, array $config = []) static 分页查询 * @method Paginator paginate(integer $listRows = 15, mixed $simple = null, array $config = []) static 分页查询
* @method mixed transaction(callable $callback) static 执行数据库事务 * @method mixed transaction(callable $callback) static 执行数据库事务
* @method void startTrans() static 启动事务 * @method void startTrans() static 启动事务
* @method void commit() static 用于非自动提交状态下面的查询提交 * @method void commit() static 用于非自动提交状态下面的查询提交

View File

@@ -11,14 +11,19 @@
namespace think; namespace think;
use think\paginator\Collection as PaginatorCollection; use ArrayAccess;
use ArrayIterator;
use Countable;
use IteratorAggregate;
use JsonSerializable;
use Traversable;
abstract class Paginator abstract class Paginator implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
{ {
/** @var bool 是否为简洁模式 */ /** @var bool 是否为简洁模式 */
protected $simple = false; protected $simple = false;
/** @var PaginatorCollection 数据集 */ /** @var Collection 数据集 */
protected $items; protected $items;
/** @var integer 当前页 */ /** @var integer 当前页 */
@@ -44,7 +49,7 @@ abstract class Paginator
'fragment' => '', 'fragment' => '',
]; ];
protected function __construct($items, $listRows, $currentPage = null, $total = null, $simple = false, $options = []) public function __construct($items, $listRows, $currentPage = null, $total = null, $simple = false, $options = [])
{ {
$this->options = array_merge($this->options, $options); $this->options = array_merge($this->options, $options);
@@ -53,10 +58,11 @@ abstract class Paginator
$this->simple = $simple; $this->simple = $simple;
$this->listRows = $listRows; $this->listRows = $listRows;
if ($simple) {
if (!$items instanceof Collection) { if (!$items instanceof Collection) {
$items = Collection::make($items); $items = Collection::make($items);
} }
if ($simple) {
$this->currentPage = $this->setCurrentPage($currentPage); $this->currentPage = $this->setCurrentPage($currentPage);
$this->hasMore = count($items) > ($this->listRows); $this->hasMore = count($items) > ($this->listRows);
$items = $items->slice(0, $this->listRows); $items = $items->slice(0, $this->listRows);
@@ -66,8 +72,7 @@ abstract class Paginator
$this->currentPage = $this->setCurrentPage($currentPage); $this->currentPage = $this->setCurrentPage($currentPage);
$this->hasMore = $this->currentPage < $this->lastPage; $this->hasMore = $this->currentPage < $this->lastPage;
} }
$this->items = $items;
$this->items = PaginatorCollection::make($items, $this);
} }
/** /**
@@ -77,12 +82,11 @@ abstract class Paginator
* @param bool $simple * @param bool $simple
* @param null $total * @param null $total
* @param array $options * @param array $options
* @return PaginatorCollection * @return Paginator
*/ */
public static function make($items, $listRows, $currentPage = null, $total = null, $simple = false, $options = []) public static function make($items, $listRows, $currentPage = null, $total = null, $simple = false, $options = [])
{ {
$paginator = new static($items, $listRows, $currentPage, $total, $simple, $options); return new static($items, $listRows, $currentPage, $total, $simple, $options);
return $paginator->items;
} }
protected function setCurrentPage($currentPage) protected function setCurrentPage($currentPage)
@@ -253,4 +257,113 @@ abstract class Paginator
* @return mixed * @return mixed
*/ */
abstract public function render(); abstract public function render();
public function items()
{
return $this->items->all();
}
public function getCollection()
{
return $this->items;
}
public function isEmpty()
{
return $this->items->isEmpty();
}
/**
* Retrieve an external iterator
* @return Traversable An instance of an object implementing <b>Iterator</b> or
* <b>Traversable</b>
*/
public function getIterator()
{
return new ArrayIterator($this->items->all());
}
/**
* Whether a offset exists
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset)
{
return $this->items->offsetExists($offset);
}
/**
* Offset to retrieve
* @param mixed $offset
* @return mixed
*/
public function offsetGet($offset)
{
return $this->items->offsetGet($offset);
}
/**
* Offset to set
* @param mixed $offset
* @param mixed $value
*/
public function offsetSet($offset, $value)
{
$this->items->offsetSet($offset, $value);
}
/**
* Offset to unset
* @param mixed $offset
* @return void
* @since 5.0.0
*/
public function offsetUnset($offset)
{
$this->items->offsetUnset($offset);
}
/**
* Count elements of an object
*/
public function count()
{
return $this->items->count();
}
public function __toString()
{
return (string) $this->render();
}
public function toArray()
{
try {
$total = $this->total();
} catch (Exception $e) {
$total = null;
}
return [
'total' => $total,
'per_page' => $this->listRows(),
'current_page' => $this->currentPage(),
'data' => $this->items->toArray()
];
}
/**
* Specify data which should be serialized to JSON
*/
public function jsonSerialize()
{
return $this->toArray();
}
public function __call($name, $arguments)
{
return call_user_func_array([$this->getCollection(), $name], $arguments);
}
} }

View File

@@ -1287,7 +1287,7 @@ class Query
* var_page:分页变量, * var_page:分页变量,
* list_rows:每页数量 * list_rows:每页数量
* type:分页类名 * type:分页类名
* @return \think\paginator\Collection * @return \think\Paginator
* @throws DbException * @throws DbException
*/ */
public function paginate($listRows = null, $simple = false, $config = []) public function paginate($listRows = null, $simple = false, $config = [])
@@ -1317,9 +1317,9 @@ class Query
if (!isset($total) && !$simple) { if (!isset($total) && !$simple) {
$options = $this->getOptions(); $options = $this->getOptions();
if (isset($options['order'])) {
unset($this->options['order']); unset($this->options['order'], $this->options['limit'], $this->options['page'], $this->options['field']);
}
$bind = $this->bind; $bind = $this->bind;
$total = $this->count(); $total = $this->count();
$results = $this->options($options)->bind($bind)->page($page, $listRows)->select(); $results = $this->options($options)->bind($bind)->page($page, $listRows)->select();

View File

@@ -1,74 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: zhangyajun <448901948@qq.com>
// +----------------------------------------------------------------------
namespace think\paginator;
use Exception;
use think\Paginator;
/**
* Class Collection
* @package think\paginator
* @method integer total()
* @method integer listRows()
* @method integer currentPage()
* @method string render()
* @method Paginator fragment($fragment)
* @method Paginator appends($key, $value)
* @method integer lastPage()
* @method boolean hasPages()
*/
class Collection extends \think\Collection
{
/** @var Paginator */
protected $paginator;
public function __construct($items = [], Paginator $paginator = null)
{
$this->paginator = $paginator;
parent::__construct($items);
}
public static function make($items = [], Paginator $paginator = null)
{
return new static($items, $paginator);
}
public function toArray()
{
if ($this->paginator) {
try {
$total = $this->total();
} catch (Exception $e) {
$total = null;
}
return [
'total' => $total,
'per_page' => $this->listRows(),
'current_page' => $this->currentPage(),
'data' => parent::toArray()
];
} else {
return parent::toArray();
}
}
public function __call($method, $args)
{
if ($this->paginator && method_exists($this->paginator, $method)) {
return call_user_func_array([$this->paginator, $method], $args);
} else {
throw new Exception('method not exists:' . __CLASS__ . '->' . $method);
}
}
}