Home Backend Development PHP Tutorial Yii paging using CLinkPager

Yii paging using CLinkPager

Jun 15, 2018 pm 03:40 PM
yii Pagination

This article mainly introduces the method of using CLinkPager for paging in Yii. Friends who need it can refer to it

This article mainly explains the method of using CLinkPager for paging in YII. Here we use the form of objects to define paging:

First customize LinkPager in components and inherit CLinkPager

The specific code is as follows:

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

/**

 * CLinkPager class file.

 *

 * @link http://www.yiiframework.com/

 * @copyright Copyright © 2008-2011 Yii Software LLC

 * @license http://www.yiiframework.com/license/

 */

 

/**

 * CLinkPager displays a list of hyperlinks that lead to different pages of target.

 *

 * @version $Id$

 * @package system.web.widgets.pagers

 * @since 1.0

 */

class LinkPager extends CLinkPager

{

 const CSS_TOTAL_PAGE=&#39;total_page&#39;;

 const CSS_TOTAL_ROW=&#39;total_row&#39;;

  

 /**

 * @var string the text label for the first page button. Defaults to &#39;<< First&#39;.

 */

 public $totalPageLabel;

 /**

 * @var string the text label for the last page button. Defaults to &#39;Last >>&#39;.

 */

 public $totalRowLabel;

  

 /**

 * Creates the page buttons.

 * @return array a list of page buttons (in HTML code).

 */

 protected function createPageButtons()

 {

  

 

    $this->maxButtonCount=8;

    $this->firstPageLabel="首页";

    $this->lastPageLabel=&#39;末页&#39;;

    $this->nextPageLabel=&#39;下一页&#39;;

    $this->prevPageLabel=&#39;上一页&#39;;

    $this->header="";

  

 if(($pageCount=$this->getPageCount())<=1)

  return array();

  

 list($beginPage,$endPage)=$this->getPageRange();

 $currentPage=$this->getCurrentPage(false); // currentPage is calculated in getPageRange()

 $buttons=array();

  

 // first page

 $buttons[]=$this->createPageButton($this->firstPageLabel,0,self::CSS_FIRST_PAGE,$currentPage<=0,false);

 

 // prev page

 if(($page=$currentPage-1)<0)

  $page=0;

 $buttons[]=$this->createPageButton($this->prevPageLabel,$page,self::CSS_PREVIOUS_PAGE,$currentPage<=0,false);

 

 // internal pages

 for($i=$beginPage;$i<=$endPage;++$i)

  $buttons[]=$this->createPageButton($i+1,$i,self::CSS_INTERNAL_PAGE,false,$i==$currentPage);

 

 // next page

 if(($page=$currentPage+1)>=$pageCount-1)

  $page=$pageCount-1;

 $buttons[]=$this->createPageButton($this->nextPageLabel,$page,self::CSS_NEXT_PAGE,$currentPage>=$pageCount-1,false);

 

 // last page

 $buttons[]=$this->createPageButton($this->lastPageLabel,$pageCount-1,self::CSS_LAST_PAGE,$currentPage>=$pageCount-1,false);

  

 // 页数统计

 $buttons[]=$this->createTotalButton(($currentPage+1)."/{$pageCount}",self::CSS_TOTAL_PAGE,false,false);

  

 // 条数统计

 $buttons[]=$this->createTotalButton("共{$this->getItemCount()}条",self::CSS_TOTAL_ROW,false,false);

 

 return $buttons;

 }

  

 protected function createTotalButton($label,$class,$hidden,$selected)

 {

 if($hidden || $selected)

  $class.=&#39; &#39;.($hidden ? self::CSS_HIDDEN_PAGE : self::CSS_SELECTED_PAGE);

 return &#39;<li class="&#39;.$class.&#39;">&#39;.CHtml::label($label,false).&#39;</li>&#39;;

 }

  

 /**

 * Registers the needed client scripts (mainly CSS file).

 */

 public function registerClientScript()

 {

 if($this->cssFile!==false)

  self::registerCssFile($this->cssFile);

 }

  

 /**

 * Registers the needed CSS file.

 * @param string $url the CSS URL. If null, a default CSS URL will be used.

 */

 public static function registerCssFile($url=null)

 {

 if($url===null)

  $url=CHtml::asset(Yii::getPathOfAlias(&#39;application.components.views.LinkPager.pager&#39;).&#39;.css&#39;);

 Yii::app()->getClientScript()->registerCssFile($url);

 }

}

Copy after login

Definition CSS style

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

/**

 * 翻页样式

 */

.page_blue{

 margin: 3px;

 padding: 3px;

 text-align: center;

 font: 12px verdana, arial, helvetica, sans-serif;

}

ul.bluePager,ul.yiiPager

{

 font-size:11px;

 border:0;

 margin:0;

 padding:0;

 line-height:100%;

 display:inline;

 text-aligin:center;

}

 

ul.bluePager li,ul.yiiPager li

{

 display:inline;

}

 

ul.bluePager a:link,ul.yiiPager a:link,

ul.bluePager a:visited,ul.yiiPager a:visited,

ul.bluePager .total_page label,ul.yiiPager .total_page label,

ul.bluePager .total_row label,ul.yiiPager .total_row label

{

 border: #ddd 1px solid;

 color: #888888 !important;

 padding:2px 5px;

 text-decoration:none;

}

 

ul.bluePager .page a,ul.yiiPager .page a

{

 font-weight:normal;

}

 

ul.bluePager a:hover,ul.yiiPager a:hover

{

 color:#FFF !important; border:#156a9a 1px solid; background-color:#2b78a3

}

 

ul.bluePager .selected a,ul.yiiPager bluePager .selected a

{

 color:#3aa1d0 !important;

 border: 1px solid #3aa1d0;

}

 

ul.bluePager .selected a:hover,ul.yiiPager .selected a:hover

{

 color:#FFF !important;

}

 

ul.bluePager .hidden a,ul.yiiPager .hidden a

{

 border:solid 1px #DEDEDE;

 color:#888888;

}

 

ul.bluePager .hidden,ul.yiiPager .hidden

{

 display:none;

}

Copy after login

Operation in controller:

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

//分页操作

$criteria=new CDbCriteria;

$criteria->order=&#39;id DESC&#39;;

$criteria->select=array(&#39;id&#39;,&#39;uid&#39;,&#39;username&#39;,&#39;title&#39;,&#39;thumb&#39;,&#39;url&#39;,&#39;clicks&#39;,&#39;time&#39;,&#39;dateline&#39;,&#39;countfavorite&#39;,&#39;quality&#39;);

$criteria->condition=$sql;

$total = Video::model()->count($criteria);

 

$pages = new CPagination($total); 

$pages->pageSize=self::PAGE_SIZE;

$pages->applyLimit($criteria);

   

$list = Video::model()->findAll($criteria);

 

$title = CommonClass::model()->find(array(

 &#39;select&#39;=>array(&#39;cname&#39;),

 &#39;condition&#39;=>&#39;id = &#39;.$id,

)); 

 

$this->render(&#39;application.views.video.list&#39;,array(

 &#39;array&#39;=>$array,

 &#39;arr&#39;=>$arr,

 &#39;result&#39;=>$result,

 &#39;list&#39;=>$list,

 &#39;pages&#39;=>$pages,

 &#39;title&#39;=>$title,

));

Copy after login

Quoted in views/video/list.php:

1

2

3

<?php

 $this->widget(&#39;LinkPager&#39;, array(&#39;pages&#39; => $pages,));

 ?>

Copy after login

The above is the entire article Content, I hope it will be helpful to everyone’s learning. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

yii2 component implements drop-down box search function

Analysis of Yii2.0 table association query

How to use yii to remove asterisks from required fields

##

The above is the detailed content of Yii paging using CLinkPager. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24
PHP development: How to implement table data sorting and paging functions PHP development: How to implement table data sorting and paging functions Sep 20, 2023 am 11:28 AM

PHP development: How to implement table data sorting and paging functions In web development, processing large amounts of data is a common task. For tables that need to display a large amount of data, it is usually necessary to implement data sorting and paging functions to provide a good user experience and optimize system performance. This article will introduce how to use PHP to implement the sorting and paging functions of table data, and give specific code examples. The sorting function implements the sorting function in the table, allowing users to sort in ascending or descending order according to different fields. The following is an implementation form

How to create custom pagination in CakePHP? How to create custom pagination in CakePHP? Jun 04, 2023 am 08:32 AM

CakePHP is a powerful PHP framework that provides developers with many useful tools and features. One of them is pagination, which helps us divide large amounts of data into several pages, making browsing and manipulation easier. By default, CakePHP provides some basic pagination methods, but sometimes you may need to create some custom pagination methods. This article will show you how to create custom pagination in CakePHP. Step 1: Create a custom pagination class First, we need to create a custom pagination class. this

Using JavaScript to implement paging display of table data Using JavaScript to implement paging display of table data Jun 16, 2023 am 10:00 AM

As data continues to grow, tabular display becomes more difficult. Most of the time, the amount of data in a table is so large that it becomes slow to load and users need to constantly browse the page to find the data they want. This article will introduce how to use JavaScript to realize paginated display of table data, making it easier for users to find the data they want. 1. Dynamically create tables. In order to make the paging function more controllable, tables need to be created dynamically. In the HTML page, add a table element similar to the one below.

How to use JavaScript to implement table paging function? How to use JavaScript to implement table paging function? Oct 20, 2023 pm 06:19 PM

How to use JavaScript to implement table paging function? With the development of the Internet, more and more websites use tables to display data. In some cases where the amount of data is large, the data needs to be displayed in pages to improve user experience. This article will introduce how to use JavaScript to implement table paging function and provide specific code examples. 1. HTML structure First, we need to prepare an HTML structure to host tables and paging buttons. We can use &lt;tab

Detailed explanation of the principle of MyBatis paging plug-in Detailed explanation of the principle of MyBatis paging plug-in Feb 22, 2024 pm 03:42 PM

MyBatis is an excellent persistence layer framework. It supports database operations based on XML and annotations. It is simple and easy to use. It also provides a rich plug-in mechanism. Among them, the paging plug-in is one of the more frequently used plug-ins. This article will delve into the principles of the MyBatis paging plug-in and illustrate it with specific code examples. 1. Paging plug-in principle MyBatis itself does not provide native paging function, but you can use plug-ins to implement paging queries. The principle of paging plug-in is mainly to intercept MyBatis

How to use Layui to develop a data display page with paging function How to use Layui to develop a data display page with paging function Oct 24, 2023 pm 01:10 PM

How to use Layui to develop a data display page with paging function Layui is a lightweight front-end UI framework that provides simple and beautiful interface components and a rich interactive experience. During development, we often encounter situations where we need to display large amounts of data and perform paging. The following is an example of a data display page with paging function developed using Layui. First, we need to introduce Layui related files and dependencies. Add the following code to the &lt;head&gt; tag of the html page

Vue component practice: paging component development Vue component practice: paging component development Nov 24, 2023 am 08:56 AM

Vue component practice: Introduction to paging component development In web applications, the paging function is an essential component. A good paging component should be simple and clear in presentation, rich in functions, and easy to integrate and use. In this article, we will introduce how to use the Vue.js framework to develop a highly customizable paging component. We will explain in detail how to develop using Vue components through code examples. Technology stack Vue.js2.xJavaScript (ES6) HTML5 and CSS3 development environment

How to implement paging function in Vue technology development How to implement paging function in Vue technology development Oct 09, 2023 am 09:06 AM

Vue is a popular JavaScript framework for building user interfaces. In the development of Vue technology, implementing paging function is a common requirement. This article will introduce how to use Vue to implement paging function and provide specific code examples. Before we start, we need to prepare some basic knowledge in advance. First, we need to understand the basic concepts and syntax of Vue. Secondly, we need to know how to use Vue components to build our application. Before we start, we need to install a paging plug-in in the Vue project,

See all articles