thinkphpのカスタムページング

リリース: 2020-04-10 09:02:17
転載
3018 人が閲覧しました

ThinkPHP5.1 にはページング実装が組み込まれています。データにページング出力関数を追加するのは非常に簡単です。Db クラスをクエリするときに、paginate メソッドを直接呼び出すことができます。この記事では、thinkphp でページング スタイルをカスタマイズする方法を紹介します。

thinkphpのカスタムページング

thinkphp5.1 には非常に便利なページング クラスがあります。render メソッドを使用してページング HTML コードをレンダリングできます。

ただし、「<<」 「>>」のような、あるページと次のページでは、プロジェクトの変化するニーズに対応できない場合があります。

ホームページ 前ページ 1 2 3 のように、ページング表示を自分で定義する必要があります。 ... 7 8 次のページ 最後のページ

このように、公式マニュアルにはページングのスタイルをカスタマイズする方法が記載されていないので、最初はページングのhtmlを先ほどのテキストに置き換えただけでした。ページと次のページ

後で、この要件を完了するために自分でクラスを定義できることがわかりました。まず、config ディレクトリに paginate.php を作成する必要があります。ファイルの内容は

'app\index\pager\gcudPager'//自己的分页类可以随便放,只要命名空间写对 ];
ログイン後にコピー

次に、「プロジェクト ディレクトリ\thinkphp\library\think\ paginator\driver\Bootstrap.php」を任意の場所にコピーし、名前空間を変更して、paginate.php の種類を対応する名前空間に変更します。たとえば、ファイルを「」にコピーしました。プロジェクト ディレクトリ\application\index\pager\gcudPager.php" の場合、上記の型もこのパスに対応します。その後、名前空間を "app\index\pager" に変更し、対応するクラス名を gcudPager に変更して、次のように定義できます。自分でページングする形式

前のページをベースにホームページを実装し、そのコードをコピーして少し修正しました##

/**首页按钮 * @param string $text * @return string */ protected function GetFirstButton($text='首页'){ if ($this->currentPage() <= 1) { return $this->getDisabledTextWrapper($text); } $url = $this->url(1); return $this->getPageLinkWrapper($url, $text); }
ログイン後にコピー
##ロジックは非常に単純です。つまり、現在のページを決定するということです。ページ番号を入力し、ページ番号変数を手動で 1 に設定すると同時に、次のページのコードをコピーして最後のページに変更できます

/**末页按钮 * @param string $text * @return string */ protected function GetLastButton($text='末页'){ if (!$this->hasMore) { return $this->getDisabledTextWrapper($text); } $url = $this->url($this->lastPage()); return $this->getPageLinkWrapper($url, $text); }
ログイン後にコピー

他の前のページと次のページは単純すぎますテキストを変更するには、レンダリング関数部分にホームページ ボタンと最後のページ ボタンを追加する必要があります

/** * 渲染分页html * @return mixed */ public function render() { if ($this->hasPages()) { if ($this->simple) { return sprintf( '
    %s %s
', $this->getPreviousButton(), $this->getNextButton() ); } else { return sprintf( '
%s %s %s %s %s
', $this->GetFirstButton(), $this->getPreviousButton(), $this->getLinks(), $this->getNextButton(), $this->GetLastButton() ); } } }
ログイン後にコピー

以上です。呼び出し部分を変更する必要はまったくありません。最後に、完全なコードを入力します##

         // +---------------------------------------------------------------------- namespace app\index\pager; use think\Paginator; class gcudPager extends Paginator { /**首页按钮 * @param string $text * @return string */ protected function GetFirstButton($text='首页'){ if ($this->currentPage() <= 1) { return $this->getDisabledTextWrapper($text); } $url = $this->url(1); return $this->getPageLinkWrapper($url, $text); } /** * 上一页按钮 * @param string $text * @return string */ protected function getPreviousButton($text = "上一页") { if ($this->currentPage() <= 1) { return $this->getDisabledTextWrapper($text); } $url = $this->url( $this->currentPage() - 1 ); return $this->getPageLinkWrapper($url, $text); } /**末页按钮 * @param string $text * @return string */ protected function GetLastButton($text='末页'){ if (!$this->hasMore) { return $this->getDisabledTextWrapper($text); } $url = $this->url($this->lastPage()); return $this->getPageLinkWrapper($url, $text); } /** * 下一页按钮 * @param string $text * @return string */ protected function getNextButton($text = '下一页') { if (!$this->hasMore) { return $this->getDisabledTextWrapper($text); } $url = $this->url($this->currentPage() + 1); return $this->getPageLinkWrapper($url, $text); } /** * 页码按钮 * @return string */ protected function getLinks() { if ($this->simple) { return ''; } $block = [ 'first' => null, 'slider' => null, 'last' => null, ]; $side = 3; $window = $side * 2; if ($this->lastPage < $window + 6) { $block['first'] = $this->getUrlRange(1, $this->lastPage); } elseif ($this->currentPage <= $window) { $block['first'] = $this->getUrlRange(1, $window + 2); $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage); } elseif ($this->currentPage > ($this->lastPage - $window)) { $block['first'] = $this->getUrlRange(1, 2); $block['last'] = $this->getUrlRange($this->lastPage - ($window + 2), $this->lastPage); } else { $block['first'] = $this->getUrlRange(1, 2); $block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side); $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage); } $html = ''; if (is_array($block['first'])) { $html .= $this->getUrlLinks($block['first']); } if (is_array($block['slider'])) { $html .= $this->getDots(); $html .= $this->getUrlLinks($block['slider']); } if (is_array($block['last'])) { $html .= $this->getDots(); $html .= $this->getUrlLinks($block['last']); } return $html; } /** * 渲染分页html * @return mixed */ public function render() { if ($this->hasPages()) { if ($this->simple) { return sprintf( '
    %s %s
', $this->getPreviousButton(), $this->getNextButton() ); } else { return sprintf( '
%s %s %s %s %s
', $this->GetFirstButton(), $this->getPreviousButton(), $this->getLinks(), $this->getNextButton(), $this->GetLastButton() ); } } } /** * 生成一个可点击的按钮 * * @param string $url * @param int $page * @return string */ protected function getAvailablePageWrapper($url, $page) { return '' . $page . ''; } /** * 生成一个禁用的按钮 * * @param string $text * @return string */ protected function getDisabledTextWrapper($text) { return '' . $text . ''; } /** * 生成一个激活的按钮 * * @param string $text * @return string */ protected function getActivePageWrapper($text) { return '' . $text . ''; } /** * 生成省略号按钮 * * @return string */ protected function getDots() { return $this->getDisabledTextWrapper('...'); } /** * 批量生成页码按钮. * * @param array $urls * @return string */ protected function getUrlLinks(array $urls) { $html = ''; foreach ($urls as $page => $url) { $html .= $this->getPageLinkWrapper($url, $page); } return $html; } /** * 生成普通页码按钮 * * @param string $url * @param int $page * @return string */ protected function getPageLinkWrapper($url, $page) { if ($this->currentPage() == $page) { return $this->getActivePageWrapper($page); } return $this->getAvailablePageWrapper($url, $page); } }
ログイン後にコピー
推奨チュートリアル:

thinkphp チュートリアル

以上がthinkphpのカスタムページングの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:oschina.net
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!