Can someone give me an example of how the LinkSorter works?
<?= yii\widgets\LinkSorter::widget([
'sort' => ['name'],
'attributes' => [],
]) ?>
The documentation is not clear
Can someone give me an example of how the LinkSorter works?
<?= yii\widgets\LinkSorter::widget([
'sort' => ['name'],
'attributes' => [],
]) ?>
The documentation is not clear
Controller:
$categoryId = Yii::$app->request->get('category-id');
$category = Category::findOne($categoryId);
$query = Product::find()->joinWith([
'categories',
'translations'
])->where([
'language' => Yii::$app->language,
Category::tableName() . '.id' => $categoryId,
]);
$countQuery = clone $query;
$sort = new Sort([
'attributes' => [
'name' => [
'asc' => ['name' => SORT_ASC],
'desc' => ['name' => SORT_DESC],
'default' => SORT_DESC,
'label' => 'Name',
],
'price' => [
'asc' => ['price' => SORT_ASC],
'desc' => ['price' => SORT_DESC],
'default' => SORT_DESC,
'label' => 'Price',
],
],
]);
$pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => 2]);
$products = $query->offset($pages->offset)
->orderBy($sort->orders)
->limit($pages->limit)
->all();
return $this->render('category', [
'category' => $category,
'products' => $products,
'pages' => $pages,
'sort' => $sort,
]);
View:
<?= LinkPager::widget([
'pagination' => $pages,
'options' => ['class' => 'pagination pull-right'],
]) ?>
<?= yii\widgets\LinkSorter::widget([
'sort' => $sort,
'attributes' => [
'name',
'price',
]
]) ?>