glarkou
glarkou

Reputation: 7101

Yii Sortable Attributes

I have the following talbes but when I define firstname as sortable it is not working (not showing firstname as link where I can click and sort the List View). Despite that if I user username is working just fine.

| User
| - userid
| - username

| Profile
| - userid
| - firstname
| - lastname

I have in controller:

    $criteria->with=array(
        'profile',
    );
    $criteria->addCondition('status = 1 or status = 2 or status = 3');

    if($search) 
        $criteria->addCondition("firstname = '{$search}'");

    $dataProvider=new CActiveDataProvider('YumUser', array(
                'criteria' => $criteria, 
                'pagination'=>array(
                    'pageSize'=>50,
                    )));

In view:

$this->widget('zii.widgets.CListView', array(
    'dataProvider'=>$dataProvider,
    'itemView'=>'_view', 
        'template' => '{summary} {sorter} {items} <div style="clear:both;"></div> {pager}',
    'sortableAttributes'=>array(
        'firstname',
    ),
));

Upvotes: 0

Views: 2615

Answers (1)

adamors
adamors

Reputation: 2656

This only works if the sort property of the dataProvider is set explicitly (in these cases, where you're sorting by an attribute that is in another model)

Try this

 $dataProvider =  new CActiveDataProvider('YumUser', array(
                'criteria' => $criteria, 
                'sort'=>array(
                    'attributes'=>array(
                         'firstname'=>array(
                              'asc'=>'firstname',
                              'desc'=>'firstname DESC',
                          ),
                     ),
                 ),
                'pagination'=>array(
                    'pageSize'=>50,
                    )));

Upvotes: 4

Related Questions