佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > thinkphp5+layui實現(xiàn)的分頁樣式示例

thinkphp5+layui實現(xiàn)的分頁樣式示例

熱門標簽:Linux服務(wù)器 Mysql連接數(shù)設(shè)置 服務(wù)器配置 電子圍欄 科大訊飛語音識別系統(tǒng) 銀行業(yè)務(wù) 阿里云 團購網(wǎng)站

本文實例講述了thinkphp5+layui實現(xiàn)的分頁樣式。分享給大家供大家參考,具體如下:

tp5之layui分頁樣式

1.分頁類

路徑:\thinkphp\library\think\paginator\driver

Layui.php

?php
namespace think\paginator\driver;
use think\Paginator;
class Layui extends Paginator
{
  /**
   * 上一頁按鈕
   * @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 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(
          'ul class="pager">%s %s/ul>',
          $this->getPreviousButton(),
          $this->getNextButton()
        );
      } else {
        return sprintf(
          '%s %s %s',
          $this->getPreviousButton(),
          $this->getLinks(),
          $this->getNextButton()
        );
      }
    }
  }
  /**
   * 生成一個可點擊的按鈕
   *
   * @param string $url
   * @param int  $page
   * @return string
   */
  protected function getAvailablePageWrapper($url, $page)
  {
    return 'a href="' . htmlentities($url) . '" rel="external nofollow" >' . $page . '/a>';
  }
  /**
   * 生成一個禁用的按鈕
   *
   * @param string $text
   * @return string
   */
  protected function getDisabledTextWrapper($text)
  {
    return 'a class="layui-laypage-prev" >' . $text . '/a>';
  }
  /**
   * 生成一個激活的按鈕
   *
   * @param string $text
   * @return string
   */
  protected function getActivePageWrapper($text)
  {
    return 'span class="layui-laypage-curr"> em class="layui-laypage-em">/em>em>' . $text . '/em>/span>';
  }
  /**
   * 生成省略號按鈕
   *
   * @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 ($page == $this->currentPage()) {
      return $this->getActivePageWrapper($page);
    }
    return $this->getAvailablePageWrapper($url, $page);
  }
}

2.配置文件

paginate.php

?php
/**
 * @auther: xxf
 * Date: 2019/9/2
 * Time: 10:24
 */
//分頁配置
return [
  'type' => 'Layui',
  'var_page' => 'page',
];

3.模型查詢

public function getUserShowList($size = 20, $where = null)
{
    $res = $this
      ->field('id,title,list_order,is_top,create_time,create_time time')
      ->where($where)
      ->order(['is_top' => 'desc', 'list_order' => 'desc', 'id' => 'desc'])
      ->paginate($size);
    return $res;
}

4.模板渲染

div class="layui-box layui-laypage layui-laypage-molv">{$list|raw}/div>

效果

更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。

希望本文所述對大家基于ThinkPHP框架的PHP程序設(shè)計有所幫助。

您可能感興趣的文章:
  • ThinkPHP5.1+Ajax實現(xiàn)的無刷新分頁功能示例
  • thinkphp5框架前后端分離項目實現(xiàn)分頁功能的方法分析
  • ThinkPHP5&5.1框架關(guān)聯(lián)模型分頁操作示例
  • thinkPHP5框架分頁樣式類完整示例
  • thinkPHP5框架實現(xiàn)基于ajax的分頁功能示例
  • thinkPHP5分頁功能實現(xiàn)方法分析
  • ThinkPHP5分頁paginate代碼實例解析

標簽:大理 棗莊 江蘇 蚌埠 衡水 廣元 衢州 萍鄉(xiāng)

巨人網(wǎng)絡(luò)通訊聲明:本文標題《thinkphp5+layui實現(xiàn)的分頁樣式示例》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    三江| 宿迁市| 黎城县| 金乡县| 东光县| 平湖市| 临江市| 克什克腾旗| 上犹县| 琼海市| 镇江市| 集安市| 聊城市| 上蔡县| 遂溪县| 孟津县| 韩城市| 凌源市| 新干县| 盐源县| 内江市| 丰宁| 潼南县| 乌拉特前旗| 呼玛县| 南安市| 原平市| 台湾省| 青神县| 西乡县| 喀什市| 平顺县| 曲麻莱县| 招远市| 钟山县| 托克托县| 昭通市| 勃利县| 赣榆县| 黄浦区| 田林县|