Yii2 Listview how to display data in view - php

Greetings
I want to display data from my Atividades model in Yii2
My AtividadesController has the following code for actionView2
public function actionView2()
{
$query = new Query;
$dataProvider = new ActiveDataProvider([
'query' => $query->from('Atividades'),
'pagination' => [
'pageSize' => 20,
],
]);
// get the posts in the current page
$posts = $dataProvider->getModels();
return $this->render('view2', ['dataProvider' => $dataProvider, 'posts' => $posts]);
}
And in my View 2 i have the following List View that appears with the message showing 4 of 4 items, but doesn't shows the items
<?= ListView::widget([
'dataProvider' => $dataProvider,
]); ?>
In Y1.xx i had a property called 'attributes' for display the model fields
How can i display the model fileds in Yii2 inside this Listview
Many thanks in advance

I have solved it by myself :)
It was not hard
In my View2 Written the following code
<?= ListView::widget([
'dataProvider' => $dataProvider,
'itemView' => '_view2',
]); ?>
And then duplicated an original view file, changed its name to _view2, put it in the same folder, and style it has needed
In my ActividadesController changed the actionView2 code to:
public function actionView2()
{
$dataProvider = new ActiveDataProvider([
'query' => Atividades::find(),
'pagination' => [
'pageSize' => 20,
],
]);
// get the posts in the current page
$posts = $dataProvider->getModels();
return $this->render('view2', ['dataProvider' => $dataProvider]);
}
_View2 code
<?= DetailView::widget([
'model' => $model,
'attributes' => [
//'id',
'atividade',
'descricao:ntext',
//'ativo',
],
]) ?>
SOLVED

Related

Yii2- Select2 button as Grid view filter doesn't filter

I want to use Select2 dr as filter in Yii Grid view, but it doesn't filter at all, only refreshes the page.
I take the data from 2 tables - from accounts I take only user_id, and from credits I take everything else. And every filter works, except And the 'user_id' one.
<?php
echo GridView::widget(
[
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
[
'attribute' => 'wp_id',
'value' => 'accounts.user_id',
'filter' => Select2::widget(
[
'model' => $searchModel,
'attribute' => 'wp_id',
'data' => ArrayHelper::map(Accounts::find()->all(), 'wp_id', 'user_id'),
'options' => ['placeholder' => ' --Filter by user id-- '],
'language' => 'en',
'pluginOptions' => [
'allowClear' => true,
],
]
),
],
],
]
); ?>
Here is the action in the controller.
<?php
public function actionIndex()
{
$searchModel = new CreditsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
} ?>
And this is the search model method
<?php
public function search($params)
{
$query = Credits::find()->with('accounts');
$dataProvider = new ActiveDataProvider([
'query' => $query
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
if(!empty($this->user_id)) {
$query->andFilterWhere(['=', 'accounts.user_id', $this->user_id]);
}
// grid filtering conditions
$query->andFilterWhere([
'user_id' => $this->user_id,
'wp_id' => $this->wp_id,
'credits_bought' => $this->credits_bought,
'purchase_date' => $this->purchase_date,
'id' => $this->id,
]);
return $dataProvider;
}
} ?>
Thanks in advance!
Because you are passing the wp_id under the GridView filter and selecting the wp_id from the Accounts model as the value of the select2-dropdown
ArrayHelper::map(Accounts::find()->all(), 'wp_id', 'user_id')
Although it is confusing that you use the gridview column attribute wp_id and use the select2 to filter the user_id for the same column but if you are looking to do that you might need to change the above to
ArrayHelper::map(Accounts::find()->all(), 'user_id', 'wp_id')

How to Add Filter Search Like Grid View In List View Yii2

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]);

Get model index inside ActiveDataProvider, Yii2

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.

i want to display array in view in yii2

i want to display array in view form but i don't know how ?
can any one help me :
my controller code is :
$query = new Query;
$query
->select(['Day_Name'])
->from('days')
->join( 'INNER JOIN','doctorsworkday','doctorsworkday.Day_id =days.dayid');
$command = $query->createCommand();
$dataProvider = $command->queryAll();
$dataProvider= array($dataProvider);
return $this->render('view',
[
'model' => $this->findModel($id),
'days'=>$days,
'doctorsworkday'=>$doctorsworkday,
'dataProvider' => $dataProvider,
])
my view code is :
<?=
DetailView::widget([
'model' => $dataProvider,
'attributes' => [
'Day_Name'
],
])
?>
but its shows : (not set)
when i use vat_dump($dataProvider) there is an array .. but i don't know how to show it
enter Var_Dump
Well firstly you are using a DetailView for displaying multiple results. DetailView is used to display the detail of a single data model as said in the docs. You could use a GridView to display multiple results. GridView accepts a dataProvider not an array so you'll need to turn your array into a dataProvider.
fortunately you can use the class ArrayDataProvider to do just that:
$gridViewDataProvider = new \yii\data\ArrayDataProvider([
'allModels' => $dataProvider,
'sort' => [
'attributes' => ['Day_Name'],
],
'pagination' => ['pageSize' => 10]
]);
something like this should work.
Then you pass this $gridViewDataProvider to the gridview like this:
<?= GridView::widget([
'dataProvider' => $gridViewDataProvider,
'columns' => [
'name'
]
]) ?>
Also note that in your controller you wrapped your $dataProvider in an array with this line:
$dataProvider= array($dataProvider);
You should remove that line and then everything should work as far as I can tell.

How to display gridview in yii2

The following code is used to display gridview but it shows "data provider" error.
MODELS:
Subcategory.php:
public function getCountries()
{
return $this->hasOne(Subcategories::className(),['id'=>'catid']);
}
VIEW:
subcategory.php:
i have added this following coding to view the saved data in a grid view.
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns'=>[
['class'=>'yii\grid\SerialColumn'],
'id',
'categoryname',
[
'attribute' => subcategory,
'value'=>getCountries.subcatname,
],
['class'=>'yii\grid\ActionColumn'],
],
]); ?>
CONTROLLER
public function actionView2()
{
$dataProvider = new ActiveDataProvider([
'query' => Subcategory::find(),
'pagination' => [
'pageSize' => 20
]
]);
$posts = $dataProvider->getModels();
return $this->render('subcategory' [
'dataProvider' => $dataProvider,
]);
}
I have applied this coding but I get the following error:
PHP Notice – yii\base\ErrorException Undefined variable: dataProvider

Categories