Yii2 GridView multiple rows for one record - php

I am new in Yii2 framework. I did Gii table, and I wanted to show multiple data related to one ID.
Here is my Grid View:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'asosSlave.tovar_nom',
],
]); ?>
However, it is only showing one piece of information related to that id:

is this mean?
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'asosSlave.tovar_nom',
['label'=>'title','value'=>function($model){
// do you want to do and return
)}]
],
]); ?>

Related

how to increase container width to fix GridView scroll in yii2

I am using yii2 GridView widget for showing data in form of table.
As there are many fields to show, GridView ( below table ) showing scroll for it. But i don't want it.
I want to increase container width so that it show table without scroll.
I had search about bootstrap.css file but not found in any directory.
How do i change GridView width ?
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
.........
['class' => 'yii\grid\ActionColumn'],
],
'tableOptions' =>['style' => 'width: 1800px;'],
]); ?>
or the gridview container options
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
.........
['class' => 'yii\grid\ActionColumn'],
],
'options' =>['style' => 'width: 1800px;'],
]); ?>
http://www.yiiframework.com/doc-2.0/yii-grid-gridview.html
http://www.yiiframework.com/doc-2.0/yii-grid-gridview.html#$options-detail
http://www.yiiframework.com/doc-2.0/yii-grid-gridview.html#$tableOptions-detail
Otherwise if you need a larger page container you need a new layout
add tye new layout in you app/views/layout eg:changing the container this this way
....
<div class="container-fluid">
<?= Breadcrumbs::widget([
'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],
]) ?>
<?= Alert::widget() ?>
<?= $content ?>
</div>
....
then assign you new layout to you view before render
public function actionIndex()
{
......
$this->layout = 'my_new_layout'
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}

Yii2 Export redirects to destination page

i'm currently implementing an export function for our yii2 based application. (An humhub module)
I use for this the "kartik-v/yii2-export" module.
The action contains:
$gridColumns = [
['class' => 'yii\grid\SerialColumn'],
'id',
'language',
'time_zone',
['class' => 'yii\grid\ActionColumn'],
];
$dataProvider = new SqlDataProvider([
'sql' => 'SELECT * FROM user WHERE status=:status'
]);
return $this->render('export',[
'dataProvider' => $dataProvider,
'gridColumns' => $gridColumns
]);
The view contains:
echo ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' => $gridColumns
]);
When I klick on the export function a new popup windows opens with an information that it creates the file now. After a few seconds it should start a download, but redirects to the destination page.
Do I need to add something to the controller or did I do anything else wrong?
Thanks for your help.
It's not clearly defined if that you need the "exportRequestParam" needs to be set.
echo ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' => $gridColumns,
'exportRequestParam' => 'user'
]);
This way it works.

Transform GridView column so it shows data from a static array

Using gii, I created a model based on a database table and then CRUD for the model. One of the columns is showing either 1 or 2 as they are stored in the database. To create new, it was easy using a ActiveForm->dropDownList() widget:
<?= $form->field($model, 'type')->dropDownList(['1'=>'Role', '2'=>'Permission'], ['prompt'=>'Select Auth Item Type']) ?>
How to use GridView and show Role instead of 1 and Permission instead 2?
In gridview you can use value
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
.........
[
'attribute' => 'type',
'label' => 'Type',
'format' => 'raw',
'value' => function ($model) {
if ( $model->type == 1) {
return 'Role';
} else {
return 'Permission';
}
},
],

yii2 : how to keep the url same in kartik grid view?

I am using kartik grid view to display my data in yii 2 with pjax enabled. Every time, I search a data in the grid view, the search is done using ajax but the url keeps changing. Is there a way to keep the url unchanged? Please help me with the solution. Here is my code:
<?php use kartik\grid\GridView;?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'pjax'=>true,
'pjaxSettings'=>[
'neverTimeout'=>true,
],
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'hotel_id',
'name',
'address',
'phone_no',
'contact_person',
// 'email_address:email',
// 'website',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
You can disable pushState feature like this:
'pjax' => true,
'pjaxSettings' => [
'options' => [
'enablePushState' => false,
],
],

Setting the template property on Yii2 GridView / ActionColumn?

First ... old school, rusty Pascal/Clipper/VB developer trying to learn PHP and Yii2 at the same time.
Looking at http://www.yiiframework.com/doc-2.0/yii-grid-actioncolumn.html#$template-detail I should be able to easily remove the update and delete buttons in the ActionColumn ... I just don't know how / where to set the property.
Our GridView code is:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'last_name',
'first_name',
'email1:email',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
My gut tells me it's something to do with instantiating the ActionColumn class before we throw that in as a column ... but I've combed the forums and documentation and just can't find any example.
You are searching for the template properly. http://www.yiiframework.com/doc-2.0/yii-grid-actioncolumn.html#$template-detail
your column should be
[
'class' => 'yii\grid\ActionColumn',
'template' => "{view}",
],

Categories