Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 限制使用Laravel分页显示的链接数量_Php_Laravel_Pagination - Fatal编程技术网

Php 限制使用Laravel分页显示的链接数量

Php 限制使用Laravel分页显示的链接数量,php,laravel,pagination,Php,Laravel,Pagination,有没有简单的方法来限制使用Laravels分页显示多少链接 目前它最多显示13个链接(上一页,123457879下一页) 然而,这对于移动设备来说太多了,变成了双线导航。。。有没有办法将链接设置为仅显示10 我已经搞乱了分页演示者,但实际上似乎什么都不管用 谢谢我创建了一个新的自定义演示者,只显示10个链接。它包括3个步骤: 创建自己的自定义演示者 use Illuminate\Pagination\BootstrapPresenter; class CustomPresenter exten

有没有简单的方法来限制使用Laravels分页显示多少链接

目前它最多显示13个链接(上一页,123457879下一页)

然而,这对于移动设备来说太多了,变成了双线导航。。。有没有办法将链接设置为仅显示10

我已经搞乱了分页演示者,但实际上似乎什么都不管用


谢谢

我创建了一个新的自定义演示者,只显示10个链接。它包括3个步骤:

创建自己的自定义演示者

use Illuminate\Pagination\BootstrapPresenter;

class CustomPresenter extends BootstrapPresenter{
    protected function getPageSlider()
    {
        // Changing the original value from 6 to 3 to reduce the link count
        $window = 3;

        // If the current page is very close to the beginning of the page range, we will
        // just render the beginning of the page range, followed by the last 2 of the
        // links in this list, since we will not have room to create a full slider.
        if ($this->currentPage <= $window)
        {
            $ending = $this->getFinish();

            return $this->getPageRange(1, $window + 2).$ending;
        }

        // If the current page is close to the ending of the page range we will just get
        // this first couple pages, followed by a larger window of these ending pages
        // since we're too close to the end of the list to create a full on slider.
        elseif ($this->currentPage >= $this->lastPage - $window)
        {
            $start = $this->lastPage - 8;

            $content = $this->getPageRange($start, $this->lastPage);

            return $this->getStart().$content;
        }

        // If we have enough room on both sides of the current page to build a slider we
        // will surround it with both the beginning and ending caps, with this window
        // of pages in the middle providing a Google style sliding paginator setup.
        else
        {
            $content = $this->getAdjacentRange();

            return $this->getStart().$content.$this->getFinish();
        }
    }

} 
通过进行以下更改,您将能够获得10链接分页器


希望有帮助:D

我知道这是一个老问题,但仍然无法使用函数links()参数进行设置。我做了简单的jQuery代码,也许它会帮助别人。这比泽斯基的答案简单得多。我不是说更好,只是更简单:)

JavaScript:

(function($) {
    $('ul.pagination li.active')
        .prev().addClass('show-mobile')
        .prev().addClass('show-mobile');
    $('ul.pagination li.active')
        .next().addClass('show-mobile')
        .next().addClass('show-mobile');
    $('ul.pagination')
        .find('li:first-child, li:last-child, li.active')
        .addClass('show-mobile');
})(jQuery);
CSS:


此代码仅使少数li元素可见。激活、两个之前、两个之后、上一个/下一个箭头。这使得最多只有7个可见元素,而不是15个。

定义自定义演示者的旧方法不适用于Laravel 5.3+,显示的链接数量似乎是在
照明/分页/UrlWindow::make()
$onEachSide
参数中硬编码的:

我最终只是编写了自己的render()函数,从
LengthAwarePaginator

/**
     * Stole come code from LengthAwarePaginator::render() and ::elements() to allow for a smaller UrlWindow
     *
     * @param LengthAwarePaginator $paginator
     * @param int $onEachSide
     * @return string
     */
    public static function render(LengthAwarePaginator $paginator, $onEachSide = 2)
    {
        $window = UrlWindow::make($paginator, $onEachSide);

        $elements = array_filter([
            $window['first'],
            is_array($window['slider']) ? '...' : null,
            $window['slider'],
            is_array($window['last']) ? '...' : null,
            $window['last'],
        ]);

        return LengthAwarePaginator::viewFactory()->make(LengthAwarePaginator::$defaultView, [
            'paginator' => $paginator,
            'elements' => $elements,
        ])->render();
    }
}

我们使用Twig,所以我将其注册为Twig筛选器,我想可以为Blade做类似的事情。

我的表太窄,所以我决定使用jQuery filter()只显示分页的第一个和最后一个li(下一个和后一个箭头)。您可以进一步自定义它

$('ul.pagination li').hide().filter(':lt(1), :nth-last-child(1)').show();

请确保将其添加到body标记的末尾。

这是一个老问题,但我想与大家分享一下我最近在Laravel 5.2应用程序中如何减少分页链接的数量:

在您的
ViewServiceProvider
中:

 \Illuminate\Pagination\AbstractPaginator::presenter(function($paginator) {
      return new \App\Pagination\BootstrapPresenter($paginator);
 });
App\Pagination\bootstraptresenter.php
有很多方法可以实现,具体取决于您希望它的行为方式

为了满足我的需要,我想要一个快速的解决方案来缩短它,这样它就不会破坏我的手机布局

拉威尔5.3

编辑此文件:(或您在paginator调用中使用的文件)

/vendor/pagination/bootstrap-4.blade.php

我在两个链接后添加了一条线以打断链接渲染循环

{{-- Array Of Links --}}
    @if (is_array($element))
        @foreach ($element as $page => $url)
             @if ($page == $paginator->currentPage())
                 <li class="page-item active"><span class="page-link">{{ $page }}</span></li>
              @else
                  <li class="page-item"><a class="page-link" href="{{ $url }}">{{ $page }}</a></li>
                  {{--Add the line below to limit rendered links--}}
                  <?php if($loop->count > 2) break; ?>
              @endif
         @endforeach
     @endif
{{--链接数组--}
@if(is_数组($element))
@foreach($page=>$url形式的元素)
@如果($page==$paginator->currentPage())
  • {{$page}
  • @否则
  • {{--添加下面的行以限制呈现的链接--} @恩迪夫 @endforeach @恩迪夫
    使用以下代码在视图文件夹的新分页文件夹中创建一个文件default.blade.php。这意味着您已经用我们的新分页文件覆盖了核心分页文件。这将在分页链接中设置一些限制

    @if ($paginator->hasPages())
        <ul class="pagination pagination">
            {{-- Previous Page Link --}}
            @if ($paginator->onFirstPage())
                <li class="disabled"><span>«</span></li>
            @else
                <li><a href="{{ $paginator->previousPageUrl() }}" rel="prev">«</a></li>
            @endif
    
            @if($paginator->currentPage() > 3)
                <li class="hidden-xs"><a href="{{ $paginator->url(1) }}">1</a></li>
            @endif
            @if($paginator->currentPage() > 4)
                <li><span>...</span></li>
            @endif
            @foreach(range(1, $paginator->lastPage()) as $i)
                @if($i >= $paginator->currentPage() - 2 && $i <= $paginator->currentPage() + 2)
                    @if ($i == $paginator->currentPage())
                        <li class="active"><span>{{ $i }}</span></li>
                    @else
                        <li><a href="{{ $paginator->url($i) }}">{{ $i }}</a></li>
                    @endif
                @endif
            @endforeach
            @if($paginator->currentPage() < $paginator->lastPage() - 3)
                <li><span>...</span></li>
            @endif
            @if($paginator->currentPage() < $paginator->lastPage() - 2)
                <li class="hidden-xs"><a href="{{ $paginator->url($paginator->lastPage()) }}">{{ $paginator->lastPage() }}</a></li>
            @endif
    
            {{-- Next Page Link --}}
            @if ($paginator->hasMorePages())
                <li><a href="{{ $paginator->nextPageUrl() }}" rel="next">»</a></li>
            @else
                <li class="disabled"><span>»</span></li>
            @endif
        </ul>
    @endif
    

    这是一个较老的问题,但这里的答案都不是最新的Laravel文档。对于较新版本的Laravel(Laravel 5.6+),这个问题有一个简单的解决方案,但它确实需要发布供应商分页视图,如所示。假设尚未发布或更改默认分页视图,则运行的命令为:

    php artisan vendor:publish --tag=laravel-pagination
    
    视图发布后,您将在resources/views/vendor/pagination目录中找到bootstrap-4.blade.php。您可以编辑此文件,并根据需要显示分页链接。为了减少默认显示的链接数量,我只需使用一点内联php设置索引并限制要显示的链接数量,如下所示:

    {{-- Array Of Links --}}
    @if (is_array($element))
        <?php $index = 0; ?>
        @foreach ($element as $page => $url)
            @if($index<4)
                @if ($page == $paginator->currentPage())
                    <li class="page-item active" aria-current="page"><span class="page-link">{{ $page }}</span></li>
                @else
                    <li class="page-item"><a class="page-link" href="{{ $url }}">{{ $page }}</a></li>
                @endif
            @endif
            <?php $index++ ?>
        @endforeach
    @endif
    
    {{--链接数组--}
    @if(is_数组($element))
    @foreach($page=>$url形式的元素)
    @如果($indexcurrentPage())
    
  • {{{$page}
  • @否则
  • @恩迪夫 @恩迪夫 @endforeach @恩迪夫
    这段代码的大部分在bootstrap-4.blade.php的默认刀片视图中,但我为Laravel 5.6添加了$index=0,$index+

    发布供应商模板:

    php artisan vendor:publish --tag=laravel-pagination
    
    编辑
    bootstrap-4.blade.php
    ,如下所示:

    @if ($paginator->hasPages())
    <ul class="pagination" role="navigation">
        {{-- Previous Page Link --}}
        @if ($paginator->onFirstPage())
            <li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.previous')">
                <span class="page-link" aria-hidden="true">&lsaquo;</span>
            </li>
        @else
            <li class="page-item">
                <a class="page-link" href="{{ $paginator->previousPageUrl() }}" rel="prev" aria-label="@lang('pagination.previous')">&lsaquo;</a>
            </li>
        @endif
    
        <?php
            $start = $paginator->currentPage() - 2; // show 3 pagination links before current
            $end = $paginator->currentPage() + 2; // show 3 pagination links after current
            if($start < 1) {
                $start = 1; // reset start to 1
                $end += 1;
            } 
            if($end >= $paginator->lastPage() ) $end = $paginator->lastPage(); // reset end to last page
        ?>
    
        @if($start > 1)
            <li class="page-item">
                <a class="page-link" href="{{ $paginator->url(1) }}">{{1}}</a>
            </li>
            @if($paginator->currentPage() != 4)
                {{-- "Three Dots" Separator --}}
                <li class="page-item disabled" aria-disabled="true"><span class="page-link">...</span></li>
            @endif
        @endif
            @for ($i = $start; $i <= $end; $i++)
                <li class="page-item {{ ($paginator->currentPage() == $i) ? ' active' : '' }}">
                    <a class="page-link" href="{{ $paginator->url($i) }}">{{$i}}</a>
                </li>
            @endfor
        @if($end < $paginator->lastPage())
            @if($paginator->currentPage() + 3 != $paginator->lastPage())
                {{-- "Three Dots" Separator --}}
                <li class="page-item disabled" aria-disabled="true"><span class="page-link">...</span></li>
            @endif
            <li class="page-item">
                <a class="page-link" href="{{ $paginator->url($paginator->lastPage()) }}">{{$paginator->lastPage()}}</a>
            </li>
        @endif
    
        {{-- Next Page Link --}}
        @if ($paginator->hasMorePages())
            <li class="page-item">
                <a class="page-link" href="{{ $paginator->nextPageUrl() }}" rel="next" aria-label="@lang('pagination.next')">&rsaquo;</a>
            </li>
        @else
            <li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.next')">
                <span class="page-link" aria-hidden="true">&rsaquo;</span>
            </li>
        @endif
    </ul>
    @endif
    
    @if($paginator->hasPages())
    
      {{--上一页链接--} @如果($paginator->onFirstPage())
    • &伊萨库;
    • @否则
    • @恩迪夫 @如果($start>1)
    • @如果($paginator->currentPage()!=4) {{--“三点”分隔符--}
    • @恩迪夫 @恩迪夫 @对于($i=$start;$i) @结束 @如果($end<$paginator->lastPage()) @如果($paginator->currentPage()+3!=$paginator->lastPage()) {{--“三点”分隔符--}
    • @恩迪夫
    • @恩迪夫 {{--下一页链接--} @如果($paginator->hasMorePages())
    • @否则
    • &rsaquo;
    • @恩迪夫
    @恩迪夫
    此示例以正确的方式处理新的CSS类、活动链接、三个点(正确,而不是1..2 3 4)和可自定义(要显示的页数)@if ($paginator->hasPages()) <ul class="pagination pagination"> {{-- Previous Page Link --}} @if ($paginator->onFirstPage()) <li class="disabled"><span>«</span></li> @else <li><a href="{{ $paginator->previousPageUrl() }}" rel="prev">«</a></li> @endif @if($paginator->currentPage() > 3) <li class="hidden-xs"><a href="{{ $paginator->url(1) }}">1</a></li> @endif @if($paginator->currentPage() > 4) <li><span>...</span></li> @endif @foreach(range(1, $paginator->lastPage()) as $i) @if($i >= $paginator->currentPage() - 2 && $i <= $paginator->currentPage() + 2) @if ($i == $paginator->currentPage()) <li class="active"><span>{{ $i }}</span></li> @else <li><a href="{{ $paginator->url($i) }}">{{ $i }}</a></li> @endif @endif @endforeach @if($paginator->currentPage() < $paginator->lastPage() - 3) <li><span>...</span></li> @endif @if($paginator->currentPage() < $paginator->lastPage() - 2) <li class="hidden-xs"><a href="{{ $paginator->url($paginator->lastPage()) }}">{{ $paginator->lastPage() }}</a></li> @endif {{-- Next Page Link --}} @if ($paginator->hasMorePages()) <li><a href="{{ $paginator->nextPageUrl() }}" rel="next">»</a></li> @else <li class="disabled"><span>»</span></li> @endif </ul> @endif
     $data->links('pagination.default')
    
    php artisan vendor:publish --tag=laravel-pagination
    
    {{-- Array Of Links --}}
    @if (is_array($element))
        <?php $index = 0; ?>
        @foreach ($element as $page => $url)
            @if($index<4)
                @if ($page == $paginator->currentPage())
                    <li class="page-item active" aria-current="page"><span class="page-link">{{ $page }}</span></li>
                @else
                    <li class="page-item"><a class="page-link" href="{{ $url }}">{{ $page }}</a></li>
                @endif
            @endif
            <?php $index++ ?>
        @endforeach
    @endif
    
    php artisan vendor:publish --tag=laravel-pagination
    
    @if ($paginator->hasPages())
    <ul class="pagination" role="navigation">
        {{-- Previous Page Link --}}
        @if ($paginator->onFirstPage())
            <li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.previous')">
                <span class="page-link" aria-hidden="true">&lsaquo;</span>
            </li>
        @else
            <li class="page-item">
                <a class="page-link" href="{{ $paginator->previousPageUrl() }}" rel="prev" aria-label="@lang('pagination.previous')">&lsaquo;</a>
            </li>
        @endif
    
        <?php
            $start = $paginator->currentPage() - 2; // show 3 pagination links before current
            $end = $paginator->currentPage() + 2; // show 3 pagination links after current
            if($start < 1) {
                $start = 1; // reset start to 1
                $end += 1;
            } 
            if($end >= $paginator->lastPage() ) $end = $paginator->lastPage(); // reset end to last page
        ?>
    
        @if($start > 1)
            <li class="page-item">
                <a class="page-link" href="{{ $paginator->url(1) }}">{{1}}</a>
            </li>
            @if($paginator->currentPage() != 4)
                {{-- "Three Dots" Separator --}}
                <li class="page-item disabled" aria-disabled="true"><span class="page-link">...</span></li>
            @endif
        @endif
            @for ($i = $start; $i <= $end; $i++)
                <li class="page-item {{ ($paginator->currentPage() == $i) ? ' active' : '' }}">
                    <a class="page-link" href="{{ $paginator->url($i) }}">{{$i}}</a>
                </li>
            @endfor
        @if($end < $paginator->lastPage())
            @if($paginator->currentPage() + 3 != $paginator->lastPage())
                {{-- "Three Dots" Separator --}}
                <li class="page-item disabled" aria-disabled="true"><span class="page-link">...</span></li>
            @endif
            <li class="page-item">
                <a class="page-link" href="{{ $paginator->url($paginator->lastPage()) }}">{{$paginator->lastPage()}}</a>
            </li>
        @endif
    
        {{-- Next Page Link --}}
        @if ($paginator->hasMorePages())
            <li class="page-item">
                <a class="page-link" href="{{ $paginator->nextPageUrl() }}" rel="next" aria-label="@lang('pagination.next')">&rsaquo;</a>
            </li>
        @else
            <li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.next')">
                <span class="page-link" aria-hidden="true">&rsaquo;</span>
            </li>
        @endif
    </ul>
    @endif
    
    User::paginate(10)->onEachSide(2);
    
    @media screen and ( max-width: 400px ){
    
        li.page-item {
    
            display: none;
        }
    
        .page-item:first-child,
        .page-item:nth-child( 2 ),
        .page-item:nth-last-child( 2 ),
        .page-item:last-child,
        .page-item.active,
        .page-item.disabled {
    
            display: block;
        }
    }
    
    {{ $posts->onEachSide(1)->links() }}