E
E
EvgMul2018-09-27 20:49:35
Yii
EvgMul, 2018-09-27 20:49:35

How to customize ListView sorting through Sort in Yii2?

Hello. I have the following problem.
In the controller, I set the dataProvider and an instance of the Sort class:

$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$sort = new Sort([
            'attributes' => [
                'id' => [
                    'default' => SORT_ASC,
                ],
                'price' => [
                    'desc' => ['lp.price' => SORT_DESC],
                    'asc' => ['lp.price' => SORT_ASC],
                    'label' => 'Цена'
                ],
            ],
        ]);

I pass them to the view.
In the view, I actually do this:
echo Dropdown::widget([
                        'items' => [
                            Html::tag('li', $sort->link('price'), ['class' => 'sort-results__dropdown-item']),
                        ],
                        'options' => ['class' => 'sort-results__dropdown']
                    ]);

 <?= ListView::widget([
                'dataProvider' => $dataProvider,
                'itemView' => '_list',
                'pager' => [
                    'class' => '\app\components\LinkPager',
                    'hideOnSinglePage' => false,
                    'maxButtonCount' => 3,
                    'prevFivePageLabel' => true,
                    'nextFivePageLabel' => true,
                    'lastPageLabel' => true,
                    'firstPageLabel' => true
                ],
                'layout' => "{items}\n{pager}",
                'emptyText' => '',
            ]); ?>

But sorting doesn't work. Please tell me, is it possible to make friends with Sort and ListView at all?
If so, what haven't I done yet?
I tried to register in the ListView config 'sorter' => $sort,, it did not help.
Thanks in advance to all who respond.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-09-27
@EvgMul

Is this how you added the sorter to the ListView?

ListView::widget([
    'layout' => "{sorter}\n{summary}\n{items}\n{pager}",
    'sorter' => [
        // ...
    ],
])

Sorter is disabled by default

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question