C
C
Crash2017-05-19 14:41:19
Yii
Crash, 2017-05-19 14:41:19

Can the ActiveDataProvider be configured to sort by the string length of a particular field?

I use GridView together with ActiveDataProvider. When displaying a table, it is necessary to configure sorting: by the length of the strings of values ​​of a certain field.

$dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'defaultOrder' => [
                    'name' => ???
                ]
            ]
        ]);

Those. values ​​for example with '11' must be displayed before '111', because line length is shorter here. I hope I explained clearly)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2017-05-19
@qonand

something like this

$dataProvider->sort->attributes['name'] = [
    'asc' => [
        'LENGTH(name)' => SORT_ASC
    ],
    'desc' => [
        'LENGTH(name)' => SORT_DESC
    ],
];

PS the code is written on the knee, but dig in this direction

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question