欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

thinkPHP5.1框架使用SemanticUI实现分页功能示例

程序员文章站 2023-10-31 15:33:52
本文实例讲述了thinkphp5.1框架使用semanticui实现分页功能。分享给大家供大家参考,具体如下: 1、config目录下新建paginate.php,下面是...

本文实例讲述了thinkphp5.1框架使用semanticui实现分页功能。分享给大家供大家参考,具体如下:

1、config目录下新建paginate.php,下面是文件的内容

<?php
//分页配置
return
  [
    'type' => 'semantic',
    'var_page' => 'page',
  ];

2、thinkphp\library\think\paginator\driver\下新建semantic.php,下面是文件的内容

<?php
/**
 * created by alic(alicfeng) on 17-6-15 下午9:17 from phpstorm.
 * email is alic@samego.com
 */
namespace think\paginator\driver;
use think\paginator;
class semantic extends paginator
{
  private static $previousbuttonhtml = '<i class="icon left arrow"></i>';
  private static $nextbuttonhtml = '<i class="icon right arrow"></i>';
  /**
   * 上一页按钮
   * @return string
   */
  protected function getpreviousbutton() {
    if ($this->currentpage() <= 1) {
      return $this->getdisabledtextwrapper(semantic::$previousbuttonhtml);
    }
    $url = $this->url(
      $this->currentpage() - 1
    );
    return $this->getpagelinkwrapper($url, semantic::$previousbuttonhtml);
  }
  /**
   * 下一页按钮
   * @return string
   */
  protected function getnextbutton() {
    if (!$this->hasmore) {
      return $this->getdisabledtextwrapper(semantic::$nextbuttonhtml);
    }
    $url = $this->url($this->currentpage() + 1);
    return $this->getpagelinkwrapper($url, semantic::$nextbuttonhtml);
  }
  /**
   * 页码按钮
   * @return string
   */
  protected function getlinks() {
    $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(
          '<div style="text-align: center"><div class="ui pagination menu">%s %s</div></div>',
          $this->getpreviousbutton(),
          $this->getnextbutton()
        );
      }else{
        return sprintf(
          '<div style="text-align: center"><div class="ui pagination menu">%s %s %s</div></div>',
          $this->getpreviousbutton(),
          $this->getlinks(),
          $this->getnextbutton()
        );
      }
    }
    return null;
  }
  /**
   * 生成一个可点击的按钮
   *
   * @param string $url
   * @param int $page
   * @return string
   */
  protected function getavailablepagewrapper($url, $page) {
    return '<a href="' . htmlentities($url) . '" rel="external nofollow" class="item">' . $page . '</a>';
  }
  /**
   * 生成一个禁用的按钮
   *
   * @param string $text
   * @return string
   */
  protected function getdisabledtextwrapper($text) {
    return '<a class="disabled item">' . $text . '</a>';
  }
  /**
   * 生成一个激活的按钮
   *
   * @param string $text
   * @return string
   */
  protected function getactivepagewrapper($text) {
    return '<a class="active item">' . $text . '</a>';
  }
  /**
   * 生成省略号按钮
   *
   * @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);
  }
}

3、搞定