Answer the question
In order to leave comments, you need to log in
Why is column sorting disabled?
Hello. I am implementing a table through Gridview and the 2 columns I need do not activate sorting, I don’t understand why.
Widget code:
\yii\grid\GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
[
'attribute' => 'id',
'options' => ['style' => 'width: 20px;'],
],
[
'attribute' => 'count_days',
'value' => 'countDays',
'header' => 'Дни',
'enableSorting' => true
],
[
'attribute' => 'count_orders',
'header' => 'Заявок',
'value' => 'countOrders',
],
[
'attribute' => 'expire_date',
'value' => function ($model) {
/** @var \app\models\NewTours $model */
if (!$model->expire_date) {
return 'Не оплачено';
}
return date('d.m.Y', $model->expire_date);
}
],
],
])
<?php
namespace app\components\administrator\models;
use app\models\NewTours;
use app\models\RequestsToursGuides;
use app\models\ToursPlan;
use yii\data\ActiveDataProvider;
use yii\base\Model;
class ToursSearchAdmin extends NewTours
{
public $is_active;
public $author;
public $count_days;
public $count_orders;
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id', 'is_active', 'count_days', 'count_orders'], 'integer'],
[['title', 'author'], 'string']
];
}
/**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
public function search($params)
{
$subQuery = ToursPlan::find()->select('tour_id, COUNT(tour_id) as count_plan')->groupBy('tour_id');
$query = self::find();
$query->leftJoin(['count_days' => $subQuery], 'count_days.tour_id = new_tours.id');
$subQuery = RequestsToursGuides::find()->select('object_id, COUNT(object_id) as count_items')->where(['object_type' => 'tour'])->groupBy('object_id');
$query->leftJoin(['count_orders' => $subQuery], 'count_orders.object_id = new_tours.id');
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 30,
],
'sort' => [
'defaultOrder' => ['count_orders' => SORT_DESC],
'attributes' => ['id', 'expire_date',
'count_days' => [
'asc' => [
'count_plan' => SORT_ASC
],
'desc' => [
'count_plan' => SORT_DESC
]
],
'count_orders' => [
'asc' => [
'count_items' => SORT_ASC
],
'desc' => [
'count_items' => SORT_DESC
]
],
]
]
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'count_days' => $this->count_days,
'count_orders' => $this->count_orders
]);
if ($this->author) {
$query->leftJoin('user_info', 'user_info.user_id = new_tours.user_id');
$query->leftJoin('company_info', 'company_info.user_id = new_tours.user_id');
$query->andWhere('company_info.name like "%'.$this->author.'%" OR concat(user_info.name," ",user_info.last_name) like "%'.$this->author.'%"');
}
if ($this->is_active == '0') {
$query->andWhere('active = 0 or expire_date < '.time());
}
elseif ($this->is_active == '1') {
$query->andWhere('active = 1 and expire_date > '.time());
}
$query->andFilterWhere(['like','title', $this->title]);
return $dataProvider;
}
}
?sort=-count_days
sorting in the url, it will be applied as needed. Tell me, please, what am I doing wrong? Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question