Home » Php » php – Yii2 Listview Sorting issue

php – Yii2 Listview Sorting issue

Posted by: admin February 25, 2020 Leave a comment

Questions:

I want to create a sort in yii2 with the price.Something like price in dsc and asc order
my sort definition is like follows

$sort = new Sort([
        'attributes' => [
            'price' => [
                'asc' => ['price' => SORT_ASC],
                'desc' => ['price' => SORT_DESC],
                'default' => SORT_DESC,
                'label' => Inflector::camel2words('price'),
            ],
            ],
        ]);

And my dataProvider is

 $dataProvider = new ActiveDataProvider([
            'query' => Accessories::find()->orderBy($sort->orders)->all(),
        ]);

And then i am passing the sort to the view like

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

In view page i am creating a sort link like

<?= $sort->link('price');?>

But the sort is not working.I am new to yii2 and need a help

How to&Answers: