Yii gridView对相关列进行排序

时间:2015-03-30 11:38:53

标签: php gridview yii

这是我的gridview:

 $this->widget('GridView', array(
    'id'=>'subscribers',
    'enableExport' => true,
    'export'=>'payments',
    'dataProvider'=>$model->search(),
    'columns'=>array(
            array(
                'name'=>'name',
                'type'=>'html',
                'value'=>'$data->user->name',
            ),
            array(
                'name'=>'city',
                'type'=>'html',
                'value'=>'is_object($data->user->location) ? $data->user->location->city->name : ""',
            ),
)
);

这是模特的内容:

$criteria=new CDbCriteria;
    $criteria->with = array('subgym');
    $criteria->compare('id',$this->id);
    $criteria->compare('user_id',$this->user_id);
    $criteria->compare('contribution',$this->contribution);
    $criteria->compare('abtype_id',$this->abtype_id);
    $criteria->compare('gym_id',$gym_id);
    $criteria->compare('has_companion',$this->has_companion,true);
    $criteria->compare('companion',$this->companion,true);
    $criteria->compare('companion_abtype',$this->companion_abtype);
    $criteria->compare('subgym.city_id', $this->cityId);

    if ($this->location != '') {
        $criteria->join = "left join users usr on usr.id = t.user_id  join locations loc on loc.id = usr.location_id";
        $criteria->compare('loc.id', $this->location);
        //$criteria->compare('loc.city_id', $this->city, true);
    }

    $criteria->compare('value_paid',$this->value_paid);
    $criteria->compare('month',$sub_month);
    $criteria->compare('year',$sub_year);
    $criteria->compare('paid',$this->paid);
   $criteria->order = 't.id desc';
        $sort = new CSort;
        $sort->defaultOrder = 'user_id ASC';

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
            'pagination' => array(    
                 'pageSize' => 20,
            ),   
            'sort' => $sort,
        ));

这是控制器:

$model = new FitnessSubscriber('search');
 $this->render('index', array(
            'model' => $model,
        ));

我想对列名称和列city desc或asc进行排序。我怎样才能做到这一点 ?

更新:我更新了条件字段。请再次检查。

1 个答案:

答案 0 :(得分:0)

这是一个很好的典型问题。开发人员总是需要对CGridView中相关表格的列进行排序。下面我详细解释了你如何做到这一点。另外,我建议您修改问题的标题,以便搜索引擎更好地编制索引: Yii gridView对相关列进行排序

首先,您需要在FitnessSubscriber模型的开头添加这些行:

public $user_name;
public $user_location;

之后,修改$model->search(),如下所示:

$criteria=new CDbCriteria;
    $criteria->with = array('subgym');
    $criteria->compare('id',$this->id);
    $criteria->compare('user_id',$this->user_id);
    $criteria->compare('contribution',$this->contribution);
    .......

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
        'pagination' => array(    
             'pageSize' => 20,
        ),   
        'sort' => array(
            'defaultOrder' => 't.id ASC',
            'attributes' => array(
                'user_name' => array(
                    'asc' => 'user.name',
                    'desc' => 'user.name DESC',
                ),
                'user_location' => array(
                    'asc' => 'user.location',
                    'desc' => 'user.location DESC',
                ),
                '*',
            ),
        ),
    ));

最后修改你的观点如下:

'columns'=>array(
        array(
            'header'="Username"
            'name'=>'user_name',
            'type'=>'html',
            'value'=>'$data->user->name',
        ),
        array(
            'header'=>"City"
            'name'=>'user_location',
            'type'=>'html',
            'value'=>'is_object($data->user->location) ? $data->user->location->city->name : ""',
        ),