Below is my dataProvider that I would like to order (ascending) by account_code.
$dataProvider = new ActiveDataProvider([
'query' => Accounts::find()->where('account_type=1'),
'pagination' => [
'pageSize' => 20,
],
]);
Got it!
'sort'=> ['defaultOrder' => ['account_code'=>SORT_ASC]],
Related
I have all set and filtering is working fine but facing problem in sorting columns
When I set sort for id column it works fine but when I set other columns sort, it is not working
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 50,
],
]);
$dataProvider->sort->attributes['id'] = [
'asc' => ['challan.id' => SORT_ASC],
'desc' => ['challan.id' => SORT_DESC],
];
$dataProvider->sort->attributes['studentname'] = [
'asc' => ['student.studentName' => SORT_ASC],
'desc' => ['student.studentName' => SORT_DESC],
];
$dataProvider->sort->attributes['sid'] = [
'asc' => ['challan.sid' => SORT_ASC],
'desc' => ['challan.sid' => SORT_DESC],
];
Please tell me what is wrong with this code
Thanks in advance
I have found the problem actually it is because I have used orderBy in $query variable
orderBy(['challan.id'=>SORT_DESC])
My controller:
$params = Yii::$app->request->queryParams;
$query4 = (new \yii\db\Query())
->select(['monthsubmit', 'modeler'])
->from('sku3d')
->groupBy(['monthsubmit', 'modeler'])
->orderBy(['monthsubmit'=>SORT_DESC]);
$query4->andFilterWhere(['like', 'monthsubmit', $params['monthsubmit']])
->andFilterWhere(['like', 'modeler', $params['modeler']]);
$dataProvider4 = new ActiveDataProvider([
'query' => $query4,
]);
MY view:
<?php echo GridView::widget([
'dataProvider' => $dataProvider4,
'filterModel' => true,
'pjax'=>true,
'panel' => [
'type' => GridView::TYPE_PRIMARY,
'heading' => '<h3 class="panel-title"><i class="glyphicon glyphicon-user"></i>Submitted SKU by Month</h3>',
],
'columns' => [
// 'monthsubmit',
[
'attribute'=>'monthsubmit',
'filter' => Html::input('string', 'monthsubmit')
'width'=>'310px',
'group'=>true, // enable grouping
],
[
'attribute'=>'modeler',
'width'=>'180px',
'filter' => Html::input('string', 'modeler')
'group'=>true, // enable grouping
],
]
]);
?>
I have created sqlDataProvider in my controller and its working. My problem is when i try to create a filter option since im not using the search model for my gridview, it return error Undefined index: monthsubmit.
Please tell me where I'm wrong.
Thank you.
In your controller, you should do as this
$modeler = Yii::$app->request->get('modeler');
$monthsubmit = Yii::$app->request->get('monthsubmit');
$query4 = (new \yii\db\Query())
->select(['monthsubmit', 'modeler'])
->from('sku3d')
->groupBy(['monthsubmit', 'modeler'])
->orderBy(['monthsubmit'=>SORT_DESC]);
$query4->andFilterWhere(['like', 'monthsubmit', $monthsubmit])
->andFilterWhere(['like', 'modeler', $modeler]);
$dataProvider4 = new ActiveDataProvider([
'query' => $query4,
]);
I have a table of policies that belong to chapters. When I view the policies in my grid view I want the default order be policy title within chapter title. I see how to set up sort attributes to enable this, but I can't figure out how to set the defaultOrder to be based on chapter title and then policy title. When ever I try to set policy.title as an attribute in the defaultOrder setting I get an error.
If Policy is model class of policy table that has a relation with chapter table named 'chapter' and linked by chapter_id field, such as:
public function getChapter()
{
return $this->hasOne(Chapter::className(), ['chapter_id' => 'chapter_id']);
}
Now you build query object with policy joined with chapter:
$query = Policy::find()
->joinWith(['chapter']);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort'=> ['defaultOrder' => ['chapter.title'=>SORT_ASC, 'policy.title' => SORT_ASC]]
]);
I could not get Fabrizio's answer to work, because my chapter table is policy_chapter, not just chapter and so is not the same as the relation name. When I tried to use the relation name, I got errors. I finally figured out in the sort, you have to use the names of the related tables, not the relations, and add them as attributes. e.g.:
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => ['pageSize' => 50],
'sort' => [
'defaultOrder' => ['policy_chapter.sort' => SORT_ASC,'policy_chapter.title' => SORT_ASC],
'enableMultiSort' => true,
'attributes' => [
'id' => [
'asc' => ['policy.id' => SORT_ASC],
'desc' => ['policy.id' => SORT_DESC],
'default' => SORT_ASC
],
'chapter_id' => [
'asc' => ['policy_chapter.sort' => SORT_ASC,'policy_chapter.title' => SORT_ASC],
'desc' => ['policy_chapter.sort' => SORT_DESC,'policy_chapter.title' => SORT_DESC],
'default' => SORT_ASC,
],
'reference' => [
'asc' => ['reference' => SORT_ASC],
'desc' => ['reference' => SORT_DESC],
'default' => SORT_ASC
],
'title' => [
'asc' => ['policy.title' => SORT_ASC],
'desc' => ['policy.title' => SORT_DESC],
'default' => SORT_ASC
],
'policy_chapter.sort',
'policy_chapter.title',
],
],
]);
Use the relation name or leave out the attributes and you get errors. Before I was writing
'defaultOrder' => ['policy_chapter.sort' => SORT_ASC,'policy_chapter.title' => SORT_ASC],
and Yii was not happy
In my yii2 application list view need to show only 5 product limit.
This my View page
<?= ListView::widget( [
'dataProvider' => $dataProvider,
'itemView' => '_item',
'summary' => '',
] ); ?>
and this my controller
$searchModel = new HorseAdsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
this my Model search Function
public function search($params)
{
$query = HorseAds::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'product_id' => $this->product_id,
'producttype' => $this->producttype,
'productname' => $this->productname,
]);
return $dataProvider;
}
Please help me solve this, thanks
Try somthing like this: idea is that set limit with find and set pagination to false
public function search($params)
{
$query = HorseAds::find()->limit(5);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => false
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'product_id' => $this->product_id,
'producttype' => $this->producttype,
'productname' => $this->productname,
]);
return $dataProvider;
}
i have a grid view like this
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'movie.movie_name',
'start_date',
'end_date',
'theatre.theatre_name',
'screen.screen_name',
'time.start_time',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
how can i arrange them in a descending order based on start_date?
In search model add sort attribute:
public function search($params)
{
...
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort'=> ['defaultOrder' => ['start_date' => SORT_DESC]]
]);
....
}