服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - PHP教程 - laravel实现分页样式替换示例代码(增加首、尾页)

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

2021-06-28 16:48回眸淡然笑 PHP教程

这篇文章主要给大家介绍了关于laravel实现分页样式替换的相关资料,实现了增加首、尾页的功能,文章通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧。

前言

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

方法如下:

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

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

最终样式

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

实现代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?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');
 }
 
}

总结

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

原文链接:https://segmentfault.com/a/1190000011288059

延伸 · 阅读

精彩推荐