YII2 Extensions and ListView references for pagination classes

Source: Internet
Author: User

YII2 itself provides good paging options for users to set up, but in real-world projects we often need more complex pagination styles, such as the effects shown, and the flip-up of the pages available and unavailable are replaced with icons.

The steps are as follows:

1, view the project pagination component, usually located in the project \vendor\yiisoft\yii2\widgets\linkpager.php location,

2, new component tlinkpager.php in \vendor\yiisoft\yii2\widgets\ directory, inherit linkpager.php, code and comments are as follows:

The main rewrite is the Renderpagebuttons () method, which implements 2 functions:

1) Generate first Prev Next last four links depending on whether you can click (disabled) to load different pictures

2) Add the total number of bars and the number of currently displayed bars label

<?phpnamespace yii\widgets; UseYii; Useyii\base\invalidconfigexception; Useyii\helpers\html; UseYii\base\widget; Useyii\data\pagination;classTlinkpagerextendslinkpager{ Public $firstPageLabel= '  ';  Public $lastPageLabel= '  ';  Public $nextPageLabel= '  ';  Public $prevPageLabel= '  ';  Public $firstPageLabel 1= '  ';  Public $lastPageLabel 1= '  ';  Public $nextPageLabel 1= '  ';  Public $prevPageLabel 1= '  ';  Public $maxButtonCount= 5;  Public $options= [' class ' = ' m_pagination ']; /** * Renders the page buttons. * Generate button list, replace with custom picture * @return string The rendering result*/    protected functionrenderpagebuttons () {$pageCount=$this->pagination->Getpagecount (); if($pageCount< 2 &&$this-hideonsinglepage) {            return‘‘; }            $buttons= []; $currentPage=$this->pagination->GetPage (); //generate first Prev Next last four links depending on whether you can click (disabled) to load a different picture//the first page        if($this->firstpagelabel!==false) {            $buttons[] =$this->renderpagebutton (($currentPage<= 0)?$this->firstpagelabel1:$this->firstpagelabel, 0,$this->firstpagecssclass,$currentPage<= 0,false); }            //Prev Page        if($this->prevpagelabel!==false) {            if(($page=$currentPage-1) < 0) {                $page= 0; }            $buttons[] =$this->renderpagebutton (($currentPage<= 0)?$this->prevpagelabel1:$this->prevpagelabel,$page,$this->prevpagecssclass,$currentPage<= 0,false); }            //Internal pages        List($beginPage,$endPage) =$this-Getpagerange ();  for($i=$beginPage;$i<=$endPage; ++$i) {            $buttons[] =$this->renderpagebutton ($i+ 1,$i,NULL,false,$i==$currentPage); }            //Next Page        if($this->nextpagelabel!==false) {            if(($page=$currentPage+ 1) >=$pageCount-1) {                $page=$pageCount-1; }            $buttons[] =$this->renderpagebutton (($currentPage>=$pageCount-1)?$this->nextpagelabel1:$this->nextpagelabel,$page,$this->nextpagecssclass,$currentPage>=$pageCount-1,false); }            //Last page        if($this->lastpagelabel!==false) {            $buttons[] =$this->renderpagebutton (($currentPage>=$pageCount-1)?$this->lastpagelabel1:$this->lastpagelabel,$pageCount-1,$this->lastpagecssclass,$currentPage>=$pageCount-1,false); }                    //adding text add by 16/10/24//Get the number per page        $pagesize=$this->pagination->getpagesize (); $label=$currentPage*$pagesize." -". ($currentPage+1) *$pagesize." &nbsp; "." Total ".$this->pagination->totalcount. " Reviews; //Add the total number of bars and the number of currently displayed bars label        $buttons[]=html::tag (' Li ', Html::tag (' span '),$label)); returnHtml::tag (' ul ',implode("\ n",$buttons),$this-options); }}

3 reference in the view, first in reference to the component use Yii\widgets\vodlinkpager;

followed by the direct output of <?php echo vodlinkpager::widget ([' pagination ' = $pagination,]);? > Can

4 ListView or Girdview How to reference, first in reference to the component use Yii\widgets\vodlinkpager;

Next output when adding option ' Pager ' =>[' class ' =>vodlinkpager::classname (),], can be shown for example below

<? PHP   ListView::begin ([    ' dataprovider ' = '$dataProvider,    ' viewparams ' =>[' clist ' = >$clist],    ' itemview ' = ' vitem ',        ' layout ' = ' {items}<div class= ' T-m-pager ' >{pager} </div> ',    //Add the following sentence to    ' pager ' =>[' class ' =>vodlinkpager::classname (),],     ]);  ListView::end();

5 part of the CSS is as follows:

. M_pagination{Display:Inline-block;Padding-left:0;margin:20px 0;Border-radius:4px;width:100%;}. m_pagination Li{Display:inline;}. m_pagination > Li > A,. m_pagination > li > Span{position:relative;Margin-left:-1px;Line-height:1.42857143;Color:#1d1d1d;text-decoration:None;Background-color:#fff;}. m_pagination > li > A{padding:0px 5px;}. m_pagination > Li.next A,. m_pagination > Li.last a,.m_pagination > Li.first A,. m_pagination > Li.prev A {padding:0px;}. m_pagination > Li.active a{Color:var (--niceblue);}. M_pagination > Li:nth-last-child (1) Span{float: Right;Margin-right:5px;font-size:13px;position:Absolute; Right:5px;}. T-m-pager{ Right:0px;position:relative;text-align:Center;Color:var (--niceblue);}

YII2 Extensions and ListView references for pagination classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.