方法提供了對當前頁面附近顯示的分頁鏈路的顆粒狀控制,從而增強了導航的清晰度。 您可以定制顯示以適合應用程序的特定需求,而不是依靠默認鏈接計數。
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中文網其他相關文章!