S
S
Stepan2015-02-20 17:59:26
Yii
Stepan, 2015-02-20 17:59:26

(Yii2) How to sort on click in ListView?

There are filters, I figured them out, but I need to change sorting by click, say by id.
So far I only understood how to set default

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

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Igor Aleksandrovich, 2015-02-20
@xoma2

Are there no examples? As far as I remember, when I last generated the code with the crud generator, fields were created where all sorts worked. And there was an example of a blog.

E
Evgeny Lavrentiev, 2015-02-21
@lavrentiev

Have a look at Yii2's search model, or the easiest way is to go to Gii with cGrud. I'm right there in the rules model and sorted even arbitrary columns.

$searchModel = new ContentSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider
        ]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question