当前位置: 移动技术网 > IT编程>开发语言>PHP > laravel实现分页样式替换示例代码(增加首、尾页)

laravel实现分页样式替换示例代码(增加首、尾页)

2017年12月08日  | 移动技术网IT编程  | 我要评论

前言

本文主要给大家介绍了关于laravel分页样式替换的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

方法如下:

一、自定义一个类(代码如下),位置随你放,注意命名空间。

二、模板输出调用 {!! $data->render(new \app\http\controllers\shmilythreepresenter($data)) !!}

最终样式

实现代码

<?php

//创建继承自 illuminate\pagination\bootstrapthreepresenter 类,这里我把类放在了controllers下面,需要修改bootstrapthreepresenter 类的哪些方法就重写哪个方法。如果觉得默认的bootstrap样式和你项目的样式不符,可以自定义样式。
namespace app\http\controllers;
use illuminate\contracts\pagination\paginator as paginatorcontract;
use illuminate\contracts\pagination\presenter as presentercontract;

class shmilythreepresenter extends \illuminate\pagination\bootstrapthreepresenter
{
 /**
  * convert the url window into bootstrap html.
  *
  * @return string
  */
 public function render()
 {
  if ($this->haspages()) {
   return sprintf(
    '<ul class="am-pagination">%s %s %s %s %s</ul>',//自定义class样式
    $this->firstpage(),//添加首页方法
    $this->getpreviousbutton('上一页'),
    $this->getlinks(),
    $this->getnextbutton('下一页'),
    $this->last()//添加尾页方法
   );
  }

  return '';
 }

 /**
  * get html wrapper for an available page link.
  *
  * @param string $url
  * @param int $page
  * @param string|null $rel
  * @return string
  */
 protected function getavailablepagewrapper($url, $page, $rel = null)
 {
  $rel = is_null($rel) ? '' : ' rel="'.$rel.'"';

  return '<li><a href="'.htmlentities($url).'" rel="external nofollow" '.$rel.'>'.$page.'</a></li>';
  //这里li标签可以添加你自己的class样式
 }

 /**
  * get html wrapper for disabled text.
  *
  * @param string $text
  * @return string
  */
 protected function getdisabledtextwrapper($text)
 {
  return '<li class="disabled"><span>'.$text.'</span></li>';
 }

 /**
  * get html wrapper for active text.
  *
  * @param string $text
  * @return string
  */
 protected function getactivepagewrapper($text)
 {
  return '<li class="active"><span>'.$text.'</span></li>';
 }


 /**
  * get the next page pagination element.
  *
  * @param string $text
  * @return string
  */
  //新建首页方法
 public function firstpage($text = '首页')
 {
  // if the current page is greater than or equal to the last page, it means we
  // can't go any further into the pages, as we're already on this last page
  // that is available, so we will make it the "next" link style disabled.
  if ($this->paginator->currentpage() <= 1) {
   return $this->getdisabledtextwrapper($text);
  }
  $url = $this->paginator->url(1);

  return $this->getpagelinkwrapper($url, $text, 'first');
 }

 /**
  * get the next page pagination element.
  *
  * @param string $text
  * @return string
  */
  //新建尾页方法
 public function last($text = '尾页')
 {
  // if the current page is greater than or equal to the last page, it means we
  // can't go any further into the pages, as we're already on this last page
  // that is available, so we will make it the "next" link style disabled.

  $url = $this->paginator->url($this->paginator->lastpage());

  return $this->getpagelinkwrapper($url, $text, 'last');
 }

}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网