CLinkPager를 사용한 Yii 페이징

不言
풀어 주다: 2023-04-01 11:14:02
원래의
1545명이 탐색했습니다.

이 글은 주로 Yii에서 페이징을 위해 CLinkPager를 사용하는 방법을 소개합니다. 필요하신 분들은 참고하세요

이 글은 주로 YII에서 페이징을 위해 CLinkPager를 사용하는 방법을 설명합니다. 여기서는 페이징을 정의하기 위해 객체 형식을 사용합니다.

첫 번째 구성 요소에서 LinkPager를 사용자 정의하고 CLinkPager를 상속합니다.

구체 코드는 다음과 같습니다.

<?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);
 }
}
로그인 후 복사

CSS 스타일 정의

/**
 * 翻页样式
 */
.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;
}
로그인 후 복사

컨트롤러에서의 작업:

rreee

views/video /list.php에 인용:

//分页操作
$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,
));
로그인 후 복사

위 내용은 모두의 학습에 도움이 되기를 바랍니다. PHP 중국어 웹사이트에 주목하세요! Related Related 권장 사항 :

yii2 구성 요소는 검색 기능을 갖춘 드롭 다운 상자를 구현합니다.


위 내용은 CLinkPager를 사용한 Yii 페이징의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!