Laravel 自定义分页、可以调整、显示数目

{
   {-- 增加输入框,跳转任意页码和显示任意条数 --}}
<ul class="pagination pagination-sm">
    <li>
        <span data-toggle="tooltip" data-placement="bottom" title="输入页码" style=" padding: 3.5px; margin-top: 3px;"><input type="text" class="text-center no-padding"  name="{
   { $name or 'page' }}" form="{
   { $formId or '' }}"  value="{
   { $data->currentPage() or '1' }}" id="{
   { $id or 'customPage'}}" data-total-page="{
   { $data->lastPage() }}" style="width: 50px;"> 页 / 共 {
   { $data->lastPage() }} 页
        </span>
    </li>
    <li style="">
       
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Laravel 中,我们可以通过自定义 Paginator 类来实现自定义分页数据结构。 首先,我们需要创建一个新的 Paginator 类,可以在 `app` 目录下创建一个 `CustomPaginator.php` 文件。在这个类中,我们需要继承 Laravel 内置的 Paginator 类,并实现 `__construct` 和 `toArray` 方法。例如: ```php <?php namespace App; use Illuminate\Pagination\Paginator; class CustomPaginator extends Paginator { public function __construct($items, $perPage, $currentPage, $options = []) { parent::__construct($items, $perPage, $currentPage, $options); } public function toArray() { return [ 'data' => $this->items->toArray(), 'pagination' => [ 'total' => $this->total(), 'count' => $this->count(), 'per_page' => $this->perPage(), 'current_page' => $this->currentPage(), 'total_pages' => $this->lastPage() ] ]; } } ``` 在上面的代码中,我们在 `toArray` 方法中定义了自定义分页数据结构,包括数据和分页信息。其中,`data` 表示当前页的数据,`pagination` 表示分页信息。你可以根据自己的需求修改这个数据结构。 然后,在使用自定义分页数据结构的时候,可以使用 `CustomPaginator` 类来实现。例如: ```php use App\CustomPaginator; $items = DB::table('users')->paginate(15); $customPaginator = new CustomPaginator($items->items(), $items->perPage(), $items->currentPage()); $result = $customPaginator->toArray(); ``` 在上面的代码中,我们先使用内置的 `paginate` 方法来获取分页数据,然后将其转换成自定义分页数据结构。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值