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();
},
],
Related
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,
]);
}
I need to convert an integer number to date using the Yii2 Framework for PHP.
I can convert it using the PHP function like this (as I found in this question):
date("Y-m-d H:i:s", 1498675028);
But I am new using Yii2 so I am not sure how to do it and maybe it can be done with Yii2.
I have a table with some columns and two of them (created_at and updated_at) has the integer 1498675028 (it is in the Unix time stamp format). But the user see in the view the integer number instead of a date. This is part of my index.php view file:
<?php Pjax::begin(); ?> <?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'username',
'created_at',
'updated_at',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end(); ?>
<?php Pjax::begin(); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'username',
'created_at:date',
'updated_at:date',
[
'attribute' => 'updated_at',
'value' => function ($model, $key, $index, $grid) {
return date('Y-m-d', $model->updated_at);
},
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end(); ?>
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';
}
},
],
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'],
],
]); ?>