Example of thinkPHP 5.1 Framework Using SemanticUI to Implement Paging Function

  • 2021-12-13 07:47:39
  • OfStack

This article illustrates how the thinkPHP 5.1 framework uses SemanticUI to implement paging. Share it for your reference, as follows:

1. Create a new paginate. php under the config directory. The following is the content of the file


<?php
// Paging configuration 
return
  [
    'type' => 'Semantic',
    'var_page' => 'page',
  ];

2. Create a new Semantic. php under thinkphp\ library\ think\ paginator\ driver\. Here is the contents of the file


<?php
/**
 * Created by alic(AlicFeng) on 17-6-15  Afternoon 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>';
  /**
   *  Upper 1 Page button 
   * @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);
  }
  /**
   *  Under 1 Page button 
   * @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);
  }
  /**
   *  Page number button 
   * @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;
  }
  /**
   *  Render paging 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;
  }
  /**
   *  Generate 1 Clickable buttons 
   *
   * @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>';
  }
  /**
   *  Generate 1 Disabled buttons 
   *
   * @param string $text
   * @return string
   */
  protected function getDisabledTextWrapper($text) {
    return '<a class="disabled item">' . $text . '</a>';
  }
  /**
   *  Generate 1 Activated buttons 
   *
   * @param string $text
   * @return string
   */
  protected function getActivePageWrapper($text) {
    return '<a class="active item">' . $text . '</a>';
  }
  /**
   *  Generate ellipsis button 
   *
   * @return string
   */
  protected function getDots() {
    return $this->getDisabledTextWrapper('...');
  }
  /**
   *  Batch Page Number Button .
   *
   * @param array $urls
   * @return string
   */
  protected function getUrlLinks(array $urls) {
    $html = '';
    foreach ($urls as $page => $url) {
      $html .= $this->getPageLinkWrapper($url, $page);
    }
    return $html;
  }
  /**
   *  Generate normal page number button 
   *
   * @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. Done

Readers who are interested in thinkPHP can check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".

I hope this article is helpful to the PHP programming based on ThinkPHP framework.


Related articles: