This commit is contained in:
thinkphp
2017-08-02 18:12:47 +08:00

View File

@@ -49,6 +49,9 @@ abstract class Paginator implements ArrayAccess, Countable, IteratorAggregate, J
'fragment' => '', 'fragment' => '',
]; ];
/** @var mixed simple模式下的下个元素 */
protected $nextItem;
public 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);
@@ -65,6 +68,9 @@ abstract class Paginator implements ArrayAccess, Countable, IteratorAggregate, J
if ($simple) { if ($simple) {
$this->currentPage = $this->setCurrentPage($currentPage); $this->currentPage = $this->setCurrentPage($currentPage);
$this->hasMore = count($items) > ($this->listRows); $this->hasMore = count($items) > ($this->listRows);
if ($this->hasMore) {
$this->nextItem = $items->slice($this->listRows, 1);
}
$items = $items->slice(0, $this->listRows); $items = $items->slice(0, $this->listRows);
} else { } else {
$this->total = $total; $this->total = $total;
@@ -135,9 +141,9 @@ abstract class Paginator implements ArrayAccess, Countable, IteratorAggregate, J
*/ */
public static function getCurrentPage($varPage = 'page', $default = 1) public static function getCurrentPage($varPage = 'page', $default = 1)
{ {
$page = Request::instance()->request($varPage); $page = (int) Request::instance()->request($varPage);
if (filter_var($page, FILTER_VALIDATE_INT) !== false && (int) $page >= 1) { if (filter_var($page, FILTER_VALIDATE_INT) !== false && $page >= 1) {
return $page; return $page;
} }
@@ -356,21 +362,26 @@ abstract class Paginator implements ArrayAccess, Countable, IteratorAggregate, J
public function toArray() public function toArray()
{ {
try { if ($this->simple) {
$total = $this->total();
} catch (\DomainException $e) {
$total = null;
}
return [ return [
'total' => $total, 'per_page' => $this->listRows,
'per_page' => $this->listRows(), 'current_page' => $this->currentPage,
'current_page' => $this->currentPage(), 'has_more' => $this->hasMore,
'next_item' => $this->nextItem,
'data' => $this->items->toArray(),
];
} else {
return [
'total' => $this->total,
'per_page' => $this->listRows,
'current_page' => $this->currentPage,
'last_page' => $this->lastPage, 'last_page' => $this->lastPage,
'data' => $this->items->toArray(), 'data' => $this->items->toArray(),
]; ];
} }
}
/** /**
* Specify data which should be serialized to JSON * Specify data which should be serialized to JSON
*/ */