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,7 +68,10 @@ 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);
$items = $items->slice(0, $this->listRows); if ($this->hasMore) {
$this->nextItem = $items->slice($this->listRows, 1);
}
$items = $items->slice(0, $this->listRows);
} else { } else {
$this->total = $total; $this->total = $total;
$this->lastPage = (int) ceil($total / $listRows); $this->lastPage = (int) ceil($total / $listRows);
@@ -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,19 +362,24 @@ abstract class Paginator implements ArrayAccess, Countable, IteratorAggregate, J
public function toArray() public function toArray()
{ {
try { if ($this->simple) {
$total = $this->total(); return [
} catch (\DomainException $e) { 'per_page' => $this->listRows,
$total = null; '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,
'data' => $this->items->toArray(),
];
} }
return [
'total' => $total,
'per_page' => $this->listRows(),
'current_page' => $this->currentPage(),
'last_page' => $this->lastPage,
'data' => $this->items->toArray(),
];
} }
/** /**