lbmzorx 2017-12-30 01:29:55 2640次浏览 2条评论 1 0 0

对小挂件进行功能上的拓展即可,效果如下
无标题.png

视图中设置数据页如下,修改pager使用自定义的分页挂件,\common\component\widget\JumpPager,样式也可以自己定义

 <?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'layout'=>'{items}<div><div class="page-summary">{summary}</div><div  class="page-box">{pager}</div></div>',

    'pager'=>[
        'class'=>\common\component\widget\JumpPager::className(),
        'firstPageLabel'=>Yii::t('app','首页'),
        'nextPageLabel'=>Yii::t('app','下一页'),
        'prevPageLabel'=>Yii::t('app','上一页'),
        'lastPageLabel'=>Yii::t('app','尾页'),
    ],
    'columns' => [
        ['class' => 'yii\grid\CheckboxColumn',],

        'id',
        'name',
        'parent_id',
        'level',
        'path',
        [
            'attribute'=>'add_time',
            'format'=>['date','php:Y-m-d H:i:s'],
        ],
        [
            'attribute'=>'edit_time',
            'label'=>'编辑时间',
            'format'=>['date','php:Y-m-d H:i:s'],
        ],

        ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?>

GridView中只有4项会渲染,1数据表{items},2排序小挂件{sorter},3总结项{summary}(即,“第15-21条,共22条数据.”),3 分页{pager}默认使用的是分页挂件yii\widgets\LinkPager,这几项是固定项,已经在它的基类BaseListView接口,因此拓展,只需继承挂件类并拓展功能即可。

代码如下,不足之处请指正

<?php
namespace common\component\widget;

use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\web\Request;
use yii\widgets\LinkPager;
class JumpPager extends LinkPager
{

    public $jOptions=[                  //跳转容器
        'class' => 'pagination',
        'style'=>"margin-left: 10px;vertical-align: middle;",
    ];

    public $jLiInputCssClass='';        //跳转输入框li元素类
    public $jLiInputOptions=[];         //跳转输入框li元素选项
    public $jLiButtonCssClass;          //跳转按钮li元素类
    public $jLiButtonOptions=[];        //跳转按钮li元素选项

    public $jInputOptions=[             //跳转输入框input元素选项,可以重写改线以得新的样式
        'style'=>"
        height: 35px;
        border-bottom-left-radius: 4px;
        border-top-left-radius: 4px;
        background-color: #fff;
        border: 1px solid #ddd;
        color: #337ab7;   
        line-height: 1.42857;
        margin-left: -1px;
        padding: 6px 0px 6px 6px;
        width: 60px;
        position: relative;
        text-decoration: none;",
        'onkeyup'=>'',
        'onchange'=>'',
    ];
    public $jButtonOptions = [          //跳转输入框button元素选项,可以重写改线以得新的样式
        'style'=>"float: right;"
    ];

    public $jInpuType='number';         //跳转input类型
    public $jButtonLabel='跳转';         //跳转按钮名称

    public $jInputIdHeader='ji';        //跳转输入框id的前缀
    public $jButtonIdHeader='jb';       //跳转按钮id的前缀
    public static $jCounter=0;          //当前页跳转的计数

    public $sOptions=[                  //跳转容器
        'class' => 'pagination',
        'style'=>'margin-left: 10px;vertical-align: middle;float:right',
    ];

    public $sLiInputCssClass;           //跳转输入框li元素类
    public $sLiInputOptions=[];         //跳转输入框li元素选项
    public $sLiButtonCssClass;          //跳转按钮li元素类
    public $sLiButtonOptions=[];        //跳转按钮li元素选项

    public $sInpuType='number';         //跳转input类型
    public $sButtonLabel='设置';         //跳转按钮名称

    public $sInputOptions=[             //跳转输入框input元素选项,可以重写改线以得新的样式
        'style'=>"
        height: 35px;
        border-bottom-left-radius: 4px;
        border-top-left-radius: 4px;
        background-color: #fff;
        border: 1px solid #ddd;
        color: #337ab7;   
        line-height: 1.42857;
        margin-left: -1px;
        padding: 6px 0px 6px 6px;
        width: 60px;
        position: relative;
        text-decoration: none;
        ",

    ];
    public $sButtonOptions=[            //跳转输入框button元素选项,可以重写改线以得新的样式
        'style'=>"float: right;",
    ];

    public $sInputIdHeader='si';        //跳转输入框id的前缀
    public $sButtonIdHeader='sb';       //跳转按钮id的前缀
    public static $sCounter=0;          //当前页跳转的计数

    /**
     * Executes the widget.
     * This overrides the parent implementation by displaying the generated page buttons.
     */
    public function run()
    {
        parent::run();
        echo $this->renderJButtons();
        echo $this->renderSButtons();
    }

    /**
     * 渲染跳转
     * @return string the rendering result
     */
    protected function renderJButtons()
    {
        return $this->renderTemplateButtons('j',$this->pagination->pageParam);
    }

    /**
     *渲染单页记录数
     * @return string
     */
    protected function renderSButtons(){
        return $this->renderTemplateButtons('s',$this->pagination->pageSizeParam);
    }

    /**
     * 渲染按钮
     * @param $type
     * @param $pageParams
     * @return string
     */
    protected function renderTemplateButtons($type,$pageParams){
        $pageCount = $this->pagination->getPageCount();
        if ($pageCount < 2 && $this->hideOnSinglePage) {
            return '';
        }
        if(!$this->sButtonLabel){
            return '';
        }

        $buttons = [];
        $page=$this->pagination->getPage();
        $pageSize=$this->pagination->getPageSize();
        $BaseUrl=$this->unsetUrlParams($pageParams);

        $inputParam=$type=='j'?$page:$pageSize;
        $buttons[]=$this->renderTemplateInput($type,$inputParam,$BaseUrl,$pageParams);
        $buttons[]=$this->renderTemplateButton($type,$page);

        $options = $this->{$type.'Options'};
        $tag = ArrayHelper::remove($options, 'tag', 'ul');
        return Html::tag($tag, implode("\n", $buttons), $options);
    }

    /**
     * 渲染输入框
     * @param $type
     * @param $inputParam
     * @param $baseUrl
     * @return string
     */
    protected function renderTemplateInput($type,$inputParam,$baseUrl,$jsParam){
        $options = $this->{$type.'LiInputOptions'};
        $linkWrapTag = ArrayHelper::remove($options , 'tag' , 'li');
        Html::addCssClass($options, $this->{$type.'LiInputCssClass'});

        $inputParam=$type=='j'?($inputParam+1):$inputParam;
        $linkOptions = $this->{$type.'InputOptions'};
        $counter = self::${$type.'Counter'};
        $linkOptions['min'] = 1;

        $linkOptions['max'] = $type=='j'?$this->pagination->getPageCount():$this->pagination->pageSizeLimit[1];
        $linkOptions['id'] = $this->{$type.'InputIdHeader'}.$counter;
        $linkOptions['baseurl']=$baseUrl;

        if(empty($linkOptions['js'])){           
            $this->renderJs($linkOptions['id'],$this->{$type.'ButtonIdHeader'}.$counter,$linkOptions['max'],$jsParam);
        }

        return Html::tag($linkWrapTag,Html::input($this->{$type.'InpuType'},empty($linkOptions['name'])?'name':$linkOptions['name'],$inputParam,$linkOptions),$options);

    }

    /**
     * 渲染按钮
     * @param $type
     * @param $page
     * @return string
     */
    protected function renderTemplateButton($type,$page){
        $options = $this->{$type.'LiButtonOptions'};
        $linkWrapTag = ArrayHelper::remove($options, 'tag', 'li');
        Html::addCssClass($options, $this->{$type.'LiButtonCssClass'});

        $buttonOptions = $this->{$type.'ButtonOptions'};
        $buttonOptions['id']=$this->{$type.'ButtonIdHeader'}.self::${$type.'Counter'};
        return Html::tag($linkWrapTag, Html::a($this->{$type.'ButtonLabel'},$this->pagination->createUrl($page),$buttonOptions),$options);
    }
    /**
     * 渲染页面js
     * @param $idInput
     * @param $idButton
     * @param $max
     * @param $pageName
     */
    protected function renderJs($idInput,$idButton,$max,$pageName){
        \yii::$app->view->registerJs(<<<SCRIPT
            $('#$idInput').keyup(function(){jump$idInput();});        
            $('#$idInput').change(function(){jump$idInput();});
            function jump$idInput(){
                var v=$('#$idInput').val();
                v=/^[\d]+$/.test(v)&&(v>=1)&&(v<=$max)?v:1;
                $(this).val(v);           
                $('#$idButton').attr('href',$('#$idInput').attr('baseurl')+'&$pageName='+v);
            }        
SCRIPT
        );
    }

    /**
     * 去掉url中的某个参数
     * @param $pageName
     * @return string
     */
    protected function unsetUrlParams($paraName){
        $request = Yii::$app->getRequest();
        $params = $request instanceof Request ? $request->getQueryParams() : [];

        $pageSize = $this->pagination->getPageSize();
        $params[$this->pagination->pageSizeParam] = $pageSize;
        $page    = $this->pagination->getPage();
        $params[$this->pagination->pageParam] = $page;

        unset($params[$paraName]);

        $params[0] = $this->pagination->route === null ? Yii::$app->controller->getRoute() : $this->pagination->route;
        return \yii::$app->urlManager->createUrl($params);
    }
}
  • 评论于 2018-03-30 15:51 举报

    This is a main heading

    This is a sub heading

    This is a sub sub heading

    ``> ## **### ## This is a normal Paragraph for bald headed people with no beards.

    This text is bold
    Italicized for you Italians

    and this is a Blockquote

    We have lists:

    1. Here
    2. Here
    3. and Here

    We have bullets:

    • Pew
    • Pew
    • and Pew!
    <?php
    function myFun () {
        echo 'myFun';
    }
    ?>
    
  • 评论于 2018-07-10 09:41 举报

    mark,随后学习使用

您需要登录后才可以评论。登录 | 立即注册