方法提供了对当前页面附近显示的分页链路的颗粒状控制,从而增强了导航的清晰度。 您可以定制显示以适合应用程序的特定需求,而不是依靠默认链接计数。
onEachSide
这对于处理不同尺寸的数据集特别有益。 对于较大的数据集,您可能需要显示更多的导航选项;对于较小的,更干净,较少的界面是可取的。
以下是实现自适应分页的方法:
在您的刀片模板(
<?php namespace AppHttpControllers; use AppModelsArticle; use IlluminateHttpRequest; class ArticleController extends Controller { public function index(Request $request) { $perPage = $this->determinePerPage($request); $articles = Article::latest() ->withCount('comments') ->paginate($perPage); $linksToShow = $this->calculateVisibleLinks($articles); return view('articles.index', compact('articles', 'linksToShow')); } private function determinePerPage(Request $request): int { return $request->query('show', 10); } private function calculateVisibleLinks($paginator): int { $totalPages = $paginator->lastPage(); // Dynamically adjust visible links based on total pages return match(true) { $totalPages > 20 => 3, $totalPages > 10 => 2, default => 1 }; } })中:
>
articles.index.blade.php
{{ $articles->onEachSide($linksToShow)->links() }}
<code>// ~50 articles (5 pages) « 1 2 3 4 5 » // ~150 articles (15 pages) « 1 ... 4 5 6 7 8 ... 15 » // ~250 articles (25 pages) « 1 ... 4 5 6 7 8 9 10 ... 25 »</code>方法与动态链接计算结合使用,创建了更具用户友好和适应性的分页体验。
以上是Laravel中的微调分页链接的详细内容。更多信息请关注PHP中文网其他相关文章!