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,
]);
}
Related
So I have this form to create a job role and i need to show whether the job role is active or dormant. I am using boolean values 0 and 1 to represent dormant and active. This is my code in the form view (form.php).
<?= $form->field($model, 'status')->dropDownList(['1' => 'Active', '0' => 'Dormant'], ['prompt'=>'Select Option']) ?>
In my model (Application.php) I have added this function
public function getStatusLabel()
{
return $this->status ? 'Active' : 'Dormant';
}
Then in my index.php view I added to display Active/Dormant.
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'Status',
'value' => 'statusLabel',
],
All is working so far. The only problem is that in my view.php (to view each application added) the status is still display 1 and 0. How do I display active/dormant in my view.php too?
You can pass this to a function like this:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'Status',
'value' => function ($data) {
return $data->getStatusLabel();
},
],
i'm a new yii2 developer !
i made a GridView and the code is shown below :
<?php Pjax::begin(); ?> <?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\ActionColumn'],
['class' => 'yii\grid\CheckboxColumn'],
['class' => 'yii\grid\SerialColumn'],
'id',
'countryCode',
'countryName',
'currencyCode',
],
]); ?>
<?php Pjax::end(); ?>
a screenshot of output :
OUTPUT
now i want to have a column contain some button and that button for example open a page or somthing else !
my problem is how can i create that column ?
You can also add the button (or as many as you like) to the existing action column like this
<?= GridView::widget([
::
::
'columns' => [
[
'class' => 'yii\grid\ActionColumn',
'template' => '{view} {update} {delete} {myButton}', // the default buttons + your custom button
'buttons' => [
'myButton' => function($url, $model, $key) { // render your custom button
return Html::a(..);
}
]
]
::
::
'currencyCode'
]
]); ?>
Example:
<?php Pjax::begin(); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\ActionColumn'],
['class' => 'yii\grid\CheckboxColumn'],
['class' => 'yii\grid\SerialColumn'],
'id',
'countryCode',
'countryName',
'currencyCode',
[
'label' => 'My Label',
'format' => 'raw',
'value' => Html::a('Click me', ['site/index'], ['class' => 'btn btn-success btn-xs', 'data-pjax' => 0])
]
],
]); ?>
<?php Pjax::end(); ?>
Try this way:
[
'header' => 'Button',
'content' => function($model) {
return Html::a(..);
}
],
More Info
i want to search the column username and get the id of that username in the user table
usercontroller.php
public function actionIndex()
{
$searchModel = new UserSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
if (Yii::$app->request->isPjax) {
return $this->renderPartial('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
} else {
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
}
view/user/index.php
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a(Yii::t('app', 'Create User'), ['create'],
['class' => 'btn btn-success']) ?>
</p>
<?php Pjax::begin(); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'username',
'email:email',
'password',
'user_type',
// 'newsletters',
// 'terms',
// 'auth_key',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end(); ?>
</div>
to achieve this where i have to place the code i am new to yii2 framework
thanks in advance
In index.php of Member module I have write this widget to open detail after click on id.but here open a Url code as it is not url page.please give me solution. and suggestion what i use because i am not familiar with Yii2.
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'label'=>'practiceCode',
'format' => 'url',
'value'=>function ($data) {
return Html::a(Html::encode("View"),'practice/view');
},
],
'memberCode',
'firstName',
'lastName',
'email:email',
'mobile',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'lable'=>'practiceCode',
'format' => 'raw',
'value'=>function ($model, $index, $widget){
return Html::a($model->practiceCode,['practice/view','id'=>$model->practiceCode],['title'=>'Go!','target'=>'_blank']
);
// return Html::a(Html::encode("View"),'practice/view');
// return Html::a(Html::encode($data- >practiceCode),'practice/view');
},
],
Now it's working 'attribute'=>'practiceCode' instead of 'lable'=>'practiceCode',
I am trying to add filter to GridView widget included in _form.php. The grid is showing fine, even filter field is shown, but the filter is not working.
Here is my code:
<?php
$searchModel = New CitySearch(); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
// 'id',
'city_name',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
I have found the solution. The search model must actually be searched before attaching it to the GridView. Therefore, I just needed to add one line to get it to work:
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
The entire code would like like that:
<?php
$searchModel = New CitySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
// 'id',
'city_name',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>