thinkme/pagination

Laravel分页增强版

dev-master 2019-01-26 08:08 UTC

This package is not auto-updated.

Last update: 2024-09-28 18:00:01 UTC


README

如果使用laravel4.0/laravel4.1/laravel4.2,请点击此处: https://packagist.org.cn/packages/desmart/pagination

此包是Laravel5分页模块的扩展。基于desmart / pagination修改,感谢radmen https://github.com/DeSmart/pagination

它提供了新的功能

  • 基于路由的URL生成器
  • 模板渲染辅助函数

安装

composer.json中添加:"thinkme/pagination": "dev-master",然后运行composer update thinkme/pagination

兼容性

此包不应破坏与Laravel分页模块的兼容性。

Laravel 5.0/5.1/5.2/5.3/5.4/5.5

方法概述

通用用法

  • withQuery() - 将查询参数绑定到URL生成器(默认情况下包含查询参数)。仅适用于从路由生成URL。
  • withoutQuery() - 不绑定查询参数
  • route($route[, array $parameters]) - 使用给定的路由生成页面URL(可以是路由名称或Illuminate\Routing\Route实例)

对于模板

  • pagesProximity($proximity) - 设置页面邻近度
  • getPagesRange() - 获取要在模板中显示的页面列表(包括邻近度)
  • canShowFirstPage() - 检查是否可以显示第一页(当第一页不在由getPagesRange()生成的列表中时返回TRUE
  • canShowLastPage() - 检查是否可以显示最后一页(当最后一页不在由getPagesRange()生成的列表中时返回TRUE

示例用法

在路由器中

// example route (routes.php)
Route::get('list-{page}.html', ['as' => 'list.page', 'uses' => 'PhotoController@index']);

在控制器中

// IF use the current route
$list = new Paginator();
$list = list->make($item, $count, 1, $page, [
            'path' => Paginator::resolveCurrentPath(),
        ]);
$list->route('list.page');
$list->pagesProximity(3);

// or quick paginator
$query = User::select($columns);
$paginate = new Paginator();
$paginate->paginate($query, 10);//or $paginate->paginate($query, $perPage, $currentPage);
//or $list = $paginate ^#*&!^#*!^&*(!&#) You know;
//return $list
return $paginate;

//IF you do not want to use the default page name. Example: http://test.com?page=1 to http://test.com?p=1
$paginate->paginate($query, $perPage, $currentPage, ['pageName' => 'p']);//http://test.com?p=1
//or
$paginate->setPageName('p');

// IF use yourself Presenter Class
Paginator::presenter(function() use ($list) {
            return new MyPresenter($list);
        });

在视图中

// list.blade.php

@foreach ($list as $item)
{{-- show item --}}
@endforeach

{!! $list->links('paginator') !!}

// if use yourself Presenter Class

{!! $list->render() !!}

// paginator.blade.php

@if ($paginator->lastPage() > 1)
  @foreach ($paginator->getPagesRange() as $page)
    {{ $page }}
  @endforeach
@endif

// or this

@if ($paginator->lastPage() > 1)
    <div class="pagination">
        @if($paginator->currentPage()>1)
            <a href="{{$paginator->url($paginator->currentPage()-1)}}" class="first"></a>
        @else
            <a href="javascript:;" class="first-none"></a>
        @endif
            @foreach ($paginator->getPagesRange() as $page)
                @if($paginator->currentPage()==$page)
                    <span class="current">{{$page}}</span>
                @else
                    <a href="{{$paginator->url($page)}}">{{$page}}</a>
                @endif
            @endforeach
            @if($paginator->currentPage()<$paginator->lastPage())
                <a href="{{$paginator->url($paginator->currentPage()+1)}}" class="last last-none"></a>
            @else
                <a href="javascript:;" class="last-none"></a>
            @endif
    </div>
@endif

许可证

此软件包是开源软件,根据MIT许可证授权。