0
votes

Looking at this article http://www.yiiframework.com/doc/api/1.1/CSort

It shows that you can sort columns that are a 'virtial'

In my GridView I have

'columns' => [
                [
                    'label' => 'Name',
                    'attribute' => 'displaynamehtml',
                    'format' => 'raw'
                ],

'displaynameashtml' is an attribute that combines a first_name and last_name and creates a clickable URL.

To sort this I have:

$dataProvider->setSort([
            'attributes' => [
                'displaynamehtml' => [
                    'asc' => 'first_name, last_name',
                    'desc' => 'first_name DESC, last_name DESC',
                    'label' => 'Name'
                ],

However this does not work and gives me the error 'Invalid argument supplied for foreach()'

Any ideas what is wrong?

2

2 Answers

0
votes

Try something like

$dataProvider->setSort([
            'attributes' => [
                'displaynamehtml' => [
                    'asc' => [
                        'first_name' => SORT_ASC,
                        'last_name' => SORT_ASC,

                    ],
                    'desc' => [
                        'first_name' => SORT_DESC,
                        'last_name' => SORT_DESC,

                    ],
                    'label' => 'Name'
                ],
0
votes

I suggest a little different:

  $dataProvider->sort->attributes['displaynamehtml'] = [           
        'asc' => [
                    'first_name' => SORT_ASC,
                    'last_name' => SORT_ASC,
                 ],
        'desc' => [
                    'first_name' => SORT_DESC,
                    'last_name' => SORT_DESC,
                  ],
  ];

Put this in the model search