无法对CGridView小部件的相关列进行排序,Yii 1.1

时间:2016-10-27 15:20:21

标签: yii cgridview

我正在使用此DataProvider来提供CGridView小部件,但列仍然不显示为可点击,当然也不允许排序。
这有什么不对?

控制器:

$dataProvider = new CActiveDataProvider( 'SubscriberAuthor', [
    'criteria' => [
        'with' => [
            'subscriber' => [ 'select' => 'name, email' ]
        ],
        'condition' => "author_id = {$author_id} And verified = 1"
    ],
    'sort' => [
        'attributes' => [
            'name'=> [
                'asc'=>'subscriber.name Asc',
                'desc'=>'subscriber.name Desc',
            ],
            'email'=> [
                'asc'=>'subscriber.email Asc',
                'desc'=>'subscriber.email Desc'
            ]
        ]
    ],
    'countCriteria' => [ 'condition' => "author_id = {$author_id} And verified = 1" ]
]);

查看:

$this->widget( 'zii.widgets.grid.CGridView', [
    'dataProvider' => $dataProvider,
    'enableSorting' => true,
    'columns' => [ 'subscriber.name', 'subscriber.email' ]
]);

2 个答案:

答案 0 :(得分:1)

确保在列名中使用的是表名而不是关系或型号名称

        'attributes' => [
        'name'=> [
            'asc'=>'subscriber_table.name Asc',
            'desc'=>'subscriber_table.name Desc',
        ],
        'email'=> [
            'asc'=>'subscriber_table.email Asc',
            'desc'=>'subscriber_table.email Desc'
        ]

答案 1 :(得分:0)

当然,问题是我没有使用列的完全限定名称:

'sort' => [
    'attributes' => [
        'subscriber.name'=> [
            'asc'=>'subscriber.name Asc',
            'desc'=>'subscriber.name Desc',
        ],
        'subscriber.email'=> [
            'asc'=>'subscriber.email Asc',
            'desc'=>'subscriber.email Desc'
        ]
    ]
]