如何在 Yii 2 的 GridView 小部件中对自定义列进行排序? [英] How to sort custom columns in GridView widget in Yii 2?

查看:22
本文介绍了如何在 Yii 2 的 GridView 小部件中对自定义列进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 GridView 中有一个自定义列.实际上它是模型属性,但我需要对其进行自定义,以便以更方便的方式呈现数据.如何添加对此列进行排序的功能?

I have a custom column in GridView. Actually it's model attribute but I needed to customize it for presenting data in more convenience way. How to add an ability to sort this column?

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterPosition'=>  GridView::FILTER_POS_HEADER,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

        'id',
        'username',
        'about:ntext',
         'birthdate',
        ['attribute'=>'sex',
         'header'=>'Sex',
         'content'=>  function($model){
          return $model->sex==0?'female':'male';  
         },
         'label'=>'Sex',
         'enableSorting'=>TRUE       

        ],

         'email:email',

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

推荐答案

您丢失了排序链接,因为您在列配置中明确设置了 'header'='Sex',请将其删除和排序链接应该会出现.

You lost your sort link because you are explicitly set 'header'=>'Sex' in your column configuration, remove it and sort link should appear.

这篇关于如何在 Yii 2 的 GridView 小部件中对自定义列进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆