I have a Listview in yii2 to display items, in this case the items are video thumbnails, and i want to display only 5, i tried to limit the query...
$dataProvider = new ActiveDataProvider([
'query' => Video::find()
->select(['video.*', 'COUNT(video_like.video_id) AS countlike'])
->joinWith(['likes'])
->groupBy(['video_like.video_id','video.video_id'])
->limit(5)
->orderBy(['countlike' => SORT_DESC])
]);
but it doesnt work...
my listview is:
<?php echo \yii\widgets\ListView::widget([
'dataProvider' => $dataProvider,
'pager' => [
'class' => \yii\bootstrap4\LinkPager::class,
],
'itemView' => '_video_item',
'layout' => '<div class="d-flex flex-wrap">{items}</div>{pager}',
'itemOptions' => [
'tag' => false,
],
]) ?>
Check this doc.
This should work for you:
$dataProvider = new ActiveDataProvider([
'query' => Video::find()
->select(['video.*', 'COUNT(video_like.video_id) AS countlike'])
->joinWith(['likes'])
->groupBy(['video_like.video_id','video.video_id'])
->orderBy(['countlike' => SORT_DESC]),
'pagination' => [
'pageSize' => 5,
]
]);
Related
Here is my view page view.php
<div class="dataTables_wrapper form-inline dt-bootstrap">
<?= GridView::widget([
'dataProvider' => $dataProvider1,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'label' =>'Date',
'value' => 'date',
],
[
'label' =>'Reminder',
'value' => function($model){
return Yii::$app->session->get('').' '.$model['type'].' '.'reminder';
}
],
[
'label' =>'To',
'value' => 'recipients',
],
]
]); ?>
</div>
This is controller
$dataProvider1 = new ActiveDataProvider([
'query' => DomainReminders::find()
->where(['domain_id'=>$model['id']])
]);
$totalCount = $dataProvider1->getTotalCount();
return $this->renderAjax('view', [
'totalCount' => $totalCount,
'dataProvider' => $dataProvider,
'dataProvider1' => $dataProvider1,
true,
true
);
This is database with column name 'type' I want print like 1st reminder, 2nd reminder, 3rd reminder in grid view
Now here is my output i want print like 1st, 2nd reminder
You may use Inflector::ordinalize() for this:
[
'label' => 'Reminder',
'value' => function ($model) {
return Inflector::ordinalize($model['type']) . ' reminder';
},
],
I've createCommand in controller
$receipts=new receipts();
$query1 = new Query;
$query1 ->select(['price','date','id'])
->from('paymentreceipts')
->join( 'INNER JOIN','patient','patient.patient_id =paymentreceipts.patient_id' )
->where('patient.patient_id=:id', array(':id'=>$id))
->orderby ('paymentreceipts.date');
$command1 = $query1->createCommand();
$dataProvider1 = $command1->queryAll();
$gridViewDataProvider3 = new \yii\data\ArrayDataProvider([
'allModels' => $dataProvider1,
'sort' => [
'attributes' => ['price','reg_date','id'],
],
]);
return $this->render('view', [
'model' => $this->findModel($id),
'gridViewDataProvider2' => $gridViewDataProvider2,
]);
in view.php
<?=
GridView::widget([
'dataProvider' => $gridViewDataProvider2,
// 'pjax'=>true,
'summary'=>'',
'showFooter' => true,
'columns' => [
['label' => 'رقم الخدمة',
'attribute' => 'id',
],
[
// 'label'=>'url',
'format' => 'raw',
'value'=>function ($data) {
return Html::a('$data->date','#',['class' => 'receipts','id'=>'id']);
},
],
]
]) ?>
what i need is : add the id in 'attribute' => 'id' number to the link that i created here ['class' => 'receipts','id'=>'id']);
i used 'id'=> $gridViewDataProvider2->id but it doesn't work !
My English is not good , so maybe my question not be clear .
JOIN statement is the reason why you don't receive "id". Because both of your tables have "id" column and it becomes ambiguous to read it. Change you query like below,
$query1 ->select(['price','date','id as receiptId'])
->from('paymentreceipts')
->join( 'INNER JOIN','patient','patient.patient_id =paymentreceipts.patient_id' )
->where('patient.patient_id=:id', array(':id'=>$id))
->orderby ('paymentreceipts.date');
$command1 = $query1->createCommand();
and then you can make the link like below.
return Html::a('$data->date','#',['class' => 'receipts','id'=>$data->receiptId]);
I Want to Add Filter With List View (Some Thing Like What Is In Grid View) but I Don Not Know How Do That
Filters I Want Are CheckBox And DropDown Options.
Here Is My Action Code In SiteController
<?php
public function actionMobiles(){
$searchModel = new MobileSearch();
//$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
//$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider = new ActiveDataProvider([
'query' => Mobile::find(),
'pagination' => [
'pageSize' => 1,
],
]);
// get the posts in the current page
//$posts = $dataProvider->getModels();
return $this->render('mobiles', ['dataProvider' => $dataProvider,'searchModel' => $searchModel]);
}
?>
In View File I Have 2 Files :
Here Is mobiles View :
<?php
use yii\widgets\ListView;
use yii\helpers\Html;
use yii\grid\GridView;
use yii\helpers\ArrayHelper ;
use app\models\Mobile;
?>
<?= ListView::widget([
'dataProvider' => $dataProvider,
//'filterModel' => $searchModel,
'itemView' => '_mobiles',
]); ?>
And In _mobiles I Have :
<?php
use yii\widgets\DetailView;
?>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
//'id',
'id',
'title',
//'ativo',
],
]) ?>
It Shows Data From DataBase Correctly With It Pagination.
But How To Add Filter To Update List(CheckBox And DropDown List Are Use to Update)??
here is the code for you, you should replace attribute and model name according to yours.
[
'attribute' => 'attribute_name',
'label' => 'Email',
'value' => function($model){
return $model->attribute_name;
},
'filterType' => GridView::FILTER_SELECT2,
'filter' => \yii\helpers\ArrayHelper::map([Your Model]::find()->asArray()->all(), 'id', 'email'),
'filterWidgetOptions' => [
'pluginOptions' => ['allowClear' => true],
],
'filterInputOptions' => ['placeholder' => 'Email', 'id' => 'grid--search-email']
],
Add this line in your view file,
Just above listview widget
echo $this->render('_search', ['model' => $searchModel]);
I try to get my model position inside dataProvider.
I have a listview with pager:
Pjax::begin();
echo ListView::widget([
'dataProvider' => $dataProvider,
'itemOptions' => ['class' => 'item'],
'itemView' => 'tree_part',
'pager' => [
'class' => ScrollPager::className(),
'enabledExtensions'=> [ScrollPager::EXTENSION_SPINNER],
'spinnerSrc'=> Yii::$app->request->baseUrl.'/images/spinner.gif',
]
]);
Pjax::end();
I know i can access$model, $widget, $index in my partial view tree_part.php, but I cannot figure out how to get current $model index inside the data.
My dataprovider:
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort'=> ['defaultOrder' => ['creation_date'=>SORT_DESC]],
'pagination' => [
'pageSize' => 12,
],
]);
So if thepagesize is 12, $index variable will only show the value of 0-11. What I need is to get "global" index out of the total count of models.
Any advice would be highly appreciated.
Possible solution:
$mindex = $index + ($widget->dataProvider->pagination->pageSize * $widget->dataProvider->pagination->page);
Anyways, looking for better one.
How can I sort with a customized gridview header?
Please give the difference between label and header in the Yii2 gridview widget dataprovider.
Here is my code:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
[
'class' => 'yii\grid\DataColumn',
'value' => function ($data) {
return $data->myTitle;
},
'headerOptions' => ['style'=>'text-align:center'],
'header' => 'Page Title',
'label' => 'Title'
],
]); ?>
Do header and label perform the same function?
How can I perform sorting in $data->myTitle?
Here my Output Screen:
I want Page Title, Status, Date Modified should be active.
Thanks in advance.
Found the answer.
Please add attributes to ActiveDataProvider in your Search Model.
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 5,
],
'sort' => ['attributes' => ['myTitle']],
]);
Add the attribute option in widget:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
[
'class' => 'yii\grid\DataColumn',
'value' => function ($data) {
return $data->myTitle;
},
'headerOptions' => ['style'=>'text-align:center'],
'attribute' => 'myTitle',
'label' => 'Page Title'
],
]); ?>
Since myTitle is a field from the database and not a custom value, you can just use attribute. The rest may be unnecessary e.g the default class is DataColumn
'columns' => [
[
'attribute' => 'myTitle',
'label' => 'Label',
]
I am not very sure I understand your question, but sorting option can be included in your modelsearch.php. So in your case you have to do like this.
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort'=> ['defaultOrder' => ['your_column'=>SORT_ASC]]
]);
if myTitle is a field in the database, why you are using such a long syntax. Just
'Columns'=>[
..
'myTitle',
..
],
should work fine and should be active for sorting as you want
if you want a different header/label for the column, use label instead of header as header is only a cell content and cannot be used for sorting, while label can. details
[
..
'attribute'=>'myTitle',
'label' => 'Page Title'
..
],