Two GridViews in one view - yii2

I developed a web site with Yii2 framework and I need some help. I need to show two different GridViews with different data in one of my pages. I managed to show the tables and their data, but when I switch from the page of one table to another, the pages the other tables change too.
How can I work on one table without get the others one involved? (I'm using yii2-grid from Kartik-v)
<?= GridView::widget([
'dataProvider' => $dataProvider1,
'filterModel' => $searchModel1,
'columns' => $this->context->columnDefaultList($parm1),
]); ?>
<hr class="page_separator"/>
<?= GridView::widget([
'dataProvider' => $dataProvider2,
'filterModel' => $searchModel2,
'columns' => $this->context->columnDefaultList($Parm2),
]); ?>

Configure You Dataprovider as
$dataprovider1->pagination->pageParam = 'dp1';
$dataprovider1->pagination->pageSizeParam = 'dp1-size';
$dataprovider2->pagination->pageParam = 'dp2';
$dataprovider2->pagination->pageSizeParam = 'dp2-size';

Related

I use kartik widget(ExportMenu) in Yii2. But I have very much data. I can not download data. help to find alternative variation ExportMenu

echo ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' => $gridColumns,
]);
please help to find alternative export menu
Try with batch export. See the link or example below - https://demos.krajee.com/export-demo-dtl/batch-loading
echo ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' => $gridColumns,
'batchSize' => 10,
'target' => '_blank',
])

Yii2 Gridview display all columns

How can I display all columns in gridview without define the columns that I want to displau in view ?
In Yii2 doc,
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [ //define columns here
'id',
'name',
'created_at:datetime',
// ...
],
]) ?>
Can we just
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => '*',
]) ?>
I don't think there is such a wildcard, but you can use attributes() method on your model (if you have an instance) or array_keys(Model::getTableSchema()->columns);

Yii2 ListView not display HTML

I have created a list view for a Quiz App. But it's displaying html.
This is how it displays
Inspect elements
My code:
echo ListView::widget([
'dataProvider' => $dataProvider,
// 'format' => 'raw',
'itemView' => '_view',
'layout'=>'{pager}{items}{pager}',
]);
My _view:
<div class="quiz-answer">
<?php
echo $model->title;
?>
<br/>
<?php
echo Html::radioList($model->id, $model->user_answer, $model->answers, array('class' => 'question'));
?>
I have found a solution by using HTML::decode.
Thank you for all your suggestion and help.
You should render your template like this:
echo ListView::widget([
'dataProvider' => $dataProvider,
// 'format' => 'raw',
'itemView' => function($model){
return $this->render('_view',[
'model' => $model
]);
},
'layout'=>'{pager}{items}{pager}',
]);
In hereapp\modules\quiz\controllers\SiteController of yii2-quiz title encoded $answer->title = Html::encode($question->title);
so the html tag in your title encoded.
you can comment it and submit your answer to check it.
Solved by using Html::decode($model->title);.

Yii2 Linkpager can pagination like this

How can I pagination like this with yii 2 framework, demo on page https://laravel-tricks.com/
resource: https://www.yiiframework.com/doc/api/2.0/yii-widgets-linkpager
This is easy just increase number of maxButtonCount. Here is example:
This is GridView.
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'pager' => [
'maxButtonCount'=>40, // Set maximum number of page buttons that can be displayed
],
'columns' => [ ....
These is LinkPager.
echo LinkPager::widget([
'pagination' => $pagination,
'maxButtonCount'=>40,
.....
]);

How to hide rows from Yii2 GridView based on user permissions?

I'm trying to hide rows from a GridView based on user permissions (RBAC).
(Yii::$app->user->can('readModel', ['model' => $model]);)
I assumed i have to add some filters to the search model, but i can't find out how i can add this filter to the query.
Maybe there is an easier solution that i haven't found yet, like adding an argument to the GridView call?
Docs don't really help me understand this specific situation either.
Thanks in advance.
a way clould be based on assign a proper class to rows using row options
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
.........
['class' => 'yii\grid\ActionColumn'],
],
'rowOptions'=>function ($model){
$class= (Yii::$app->user->can('readModel', ['model' => $model]) ? 'hide' : 'swow';
return $class;
},
As suggest the plurality of the attribute "rowOptions" is an array, lamba-function should return an array:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
.........
['class' => 'yii\grid\ActionColumn'],
],
'rowOptions'=>function ($model){
$class= (Yii::$app->user->can('readModel', ['model' => $model]) ? 'hide' : 'swow';
// HTML tag attribute class
return ['class' => $class];
},