Yii2 GridView更改排序图标

时间:2014-11-14 10:01:14

标签: yii2

我已经安装了yii2,我想更改GridView排序图标和类。 我怎么能这样做?我在yii2文档中搜索但没有找到。

Yii2网格代码:

<?php echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
    ['class' => 'yii\grid\SerialColumn'],

        [ 
            'attribute' => 'createdate',
            'label' => Yii::t('app', 'Created'),                
            'value' => function ($data) { return '4 September 2013'; }, 
        ],

    ...

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

3 个答案:

答案 0 :(得分:4)

按升序排列web/css/site.css如下所示(使用desc降序排列)。使用您自己的图标网址。

a.asc:after {
content: url(link/to/images/img.jpg);
}

使用以下内容为您的单元格添加课程

'contentOptions' => ['class' => 'come-class']

答案 1 :(得分:0)

尝试创建自己的类扩展ActionColumn

class myActionColumn extends ActionColumn {
    public function initDefaultButtons()
    {
        if (!isset($this->buttons['view'])) {
            $this->buttons['view'] = function ($url, $model, $key) {
                $options = array_merge([
                    'title' => Yii::t('yii', 'View'),
                    'aria-label' => Yii::t('yii', 'View'),
                    'data-pjax' => '0',
                ], $this->buttonOptions);
                return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, $options);
            };
        }
        if (!isset($this->buttons['update'])) {
            $this->buttons['update'] = function ($url, $model, $key) {
                $options = array_merge([
                    'title' => Yii::t('yii', 'Update'),
                    'aria-label' => Yii::t('yii', 'Update'),
                    'data-pjax' => '0',
                ], $this->buttonOptions);
                return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, $options);
            };
        }
        if (!isset($this->buttons['delete'])) {
            $this->buttons['delete'] = function ($url, $model, $key) {
                $options = array_merge([
                    'title' => Yii::t('yii', 'Delete'),
                    'aria-label' => Yii::t('yii', 'Delete'),
                    'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
                    'data-method' => 'post',
                    'data-pjax' => '0',
                ], $this->buttonOptions);
                return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, $options);
            };
        }
    }
}

并更改您需要的内容并使用此类而不是ActionColumn类

答案 2 :(得分:0)

**您可以使用此脚本简单地添加一个始终可见的排序图标-如果未根据**修改函数调用,请检查是否使用了pjax **

$script = <<< JS
    var removeSortIcon = function(event) {
            $('[data-sort]').each(function() {
                var element = $(this);
                if(element.hasClass('desc') || element.hasClass('asc') ){
                    element.remove('span');
                }else{
                    element.append(' <span class="glyphicon glyphicon-sort"></span>');
                }
            });
    }    
    $(document).ready(removeSortIcon);
    $(document).on('ready pjax:success', removeSortIcon);
JS;
$this->registerJs($script);

第二种方式:

 $dataProvider = new ActiveDataProvider([
        'query' => $query,
        'sort' => [
            'defaultOrder' => ['name' => SORT_ASC,'email' => SORT_ASC,'username' => SORT_ASC,],
        ],
  ]);

enter image description here