簡體   English   中英

如何在YII上設置CGridView的分頁樣式?

[英]How to styling the pagination of CGridView at YII?

如何在YII上設置CGridView的分頁樣式?

在cgridview中,它按照圖片顯示尋呼機。

我想通過以下方式顯示尋呼機位置。

首頁上一頁1 2 3 4 5 6 7 8 9 10下一頁尾頁

我想刪除“轉到頁面:”。

我應該怎么做?

在此輸入圖像描述

只需將尋呼機的header屬性設置為空字符串即可。

例:

$this->widget('zii.widgets.CGridView', array(
        // ...other properties here...
        'pager' => array('class' => 'CLinkPager', 'header' => ''),
    ));

如果您只需要更改樣式,則應編寫自己的css文件並將其應用於gridView(請參閱帖子末尾)。 但如果您的更改比您更深,則必須延長尋呼機。

我很久以前就已經這樣做了:我擴展了Link Pager以滿足我的需求。 components目錄中,我創建了一個新的尋呼機:

class PagerSA extends CLinkPager

在哪里我重寫了一些方法以適應我想要的(修改非常小)。 這是我可以作為示例的代碼。 正如我所說,我從原始尋呼機中修改了很少的東西。 如果您的尋呼機與CLinkPagerCListPager有很多不同,您應該擴展CBasePager

class PagerSA extends CLinkPager
{
    const CSS_FIRST_PAGE='first';
    const CSS_LAST_PAGE='last';
    const CSS_PREVIOUS_PAGE='previous';
    const CSS_NEXT_PAGE='next';
    const CSS_INTERNAL_PAGE='page';
    const CSS_HIDDEN_PAGE='hidden';
    const CSS_SELECTED_PAGE='selected';

    /**
     * @var integer maximum number of page buttons that can be displayed. Defaults to 10.
     */
    public $maxButtonCount=5;
    /**
     * @var string the text label for the next page button. Defaults to 'Next >'.
     */
    public $nextPageLabel;
    /**
     * @var string the text label for the previous page button. Defaults to '< Previous'.
     */
    public $prevPageLabel;
    /**
     * @var string the text label for the first page button. Defaults to '<< First'.
     */
    public $firstPageLabel;
    /**
     * @var string the text label for the last page button. Defaults to 'Last >>'.
     */
    public $lastPageLabel;
    /**
     * @var string the text shown before page buttons. Defaults to 'Go to page: '.
     */
    public $header;
    /**
     * @var string the text shown after page buttons.
     */
    public $footer='';
    /**
     * @var mixed the CSS file used for the widget. Defaults to null, meaning
     * using the default CSS file included together with the widget.
     * If false, no CSS file will be used. Otherwise, the specified CSS file
     * will be included when using this widget.
     */
    public $cssFile;
    /**
     * @var array HTML attributes for the pager container tag.
     */
    public $htmlOptions=array();

    /**
     * Initializes the pager by setting some default property values.
     */
    public function init()
    {
        if($this->nextPageLabel===null)
            $this->nextPageLabel=Yii::t('yii','Suivante >');
        if($this->prevPageLabel===null)
            $this->prevPageLabel=Yii::t('yii','< Précédente');
        if($this->firstPageLabel===null)
            $this->firstPageLabel=Yii::t('yii','<< Première');
        if($this->lastPageLabel===null)
            $this->lastPageLabel=Yii::t('yii','Dernière >>');
        if($this->header===null)
            $this->header=Yii::t('yii','');

        if(!isset($this->htmlOptions['id']))
            $this->htmlOptions['id']=$this->getId();
        if(!isset($this->htmlOptions['class']))
            $this->htmlOptions['class']='yiiPager';
    }

    /**
     * Creates the page buttons.
     * @return array a list of page buttons (in HTML code).
     */
    protected function createPageButtons()
    {
        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);

        //2 first pages
        if($currentPage==3)
        {
            $buttons[]=$this->createPageButton(1,0,self::CSS_INTERNAL_PAGE,false,0==$currentPage);
            $buttons[]= ' ... ';
        }

        if($currentPage>3)
        {
            $buttons[]=$this->createPageButton(1,0,self::CSS_INTERNAL_PAGE,false,0==$currentPage);
            $buttons[]=$this->createPageButton(2,1,self::CSS_INTERNAL_PAGE,false,1==$currentPage);
            $buttons[]= ' ... ';
        }

        // internal pages
        for($i=$beginPage;$i<=$endPage;++$i)
            $buttons[]=$this->createPageButton($i+1,$i,self::CSS_INTERNAL_PAGE,false,$i==$currentPage);

        //3 lasts pages
        if($endPage<$pageCount-2)
        {
            $buttons[]= ' ... ';
            for($i=$pageCount-2;$i<=$pageCount-1;++$i)
            $buttons[]=$this->createPageButton($i+1,$i,self::CSS_INTERNAL_PAGE,false,$i==$currentPage);
        }

        if($endPage==$pageCount-2)
        {
            $buttons[]= ' ... ';
            $buttons[]=$this->createPageButton($pageCount,$pageCount-1,self::CSS_INTERNAL_PAGE,false,$pageCount-1==$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);

        return $buttons;
    }

}

然后要在c網格視圖中應用它,你必須輸入如下內容:

'pager' => array(
    'class' => 'SaAdminPager',
    'cssFile'=>'/themes/version_3/css/widgets/adminPager.css',
    'header'=>'',
     ),
'pagerCssClass'=>'pagination pagination-centered', 
    $this->widget('bootstrap.widgets.TbGridView',array(
        'id'=>'grid-view',
        'htmlOptions'=>array('style'=>'margin-top:95px;'),
        'dataProvider'=>$model->search(),
        'type'=>'striped bordered condensed',
    'filter'=>$model,
    'columns'=>array(
    ......
.......
    ));
    echo 'Goto Page '.CHtml::textField(ucfirst(Yii::app()->controller->id)."_page",'1',
            array('id'=>'jump-to-page',
            ));
    ?>
    <script>
        $('#jump-to-page').change(function(e){
            $.fn.yiiGridView.update('grid-view', {
                data: $(this).serialize()
            });
            e.preventDefault();
        });
    </script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM