How to add custom column in CGridView in YII? - yii

I need to add a column in CGridView.
I use this:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'pager' => array(
'firstPageLabel' => '<<',
),
'columns'=>array(
'username',
'name',
'email',
'creationDate',
array(
'class' => 'CButtonColumn',
'template' => '{change} {view}',
'buttons' => array(
'change' => array(
'url'=> "'http://test.com/userservice/".$model->username."'",
),
),
),
array(
'name' => 'test',
'value' => 'testtest',
)
),
));
But I got error:
Property "User.test" is not defined.

You're almost there, in your column array you would use the name param for attributes of the model in the dataprovider, instead for custom columns you can use header like so:
'columns'=>array(
...
array(
'header' => 'test',
'value' => '"testtest"',
),
...
)

You can write normal code on CGridView like this .
'columns'=>array(
'username',
'name',
'email',
'creationDate',
'test',
---
),
If you put code like this on your respective model .
---
public $test ;
public function afterFind() {
$this->test = 'Test Variable' ; // put your custom code to reflect exact need
return parent::afterFind();
}
---

Related

remove button from CGridView with condition

Hi I have CRUD generated CGridView in yii. I need to add a new button to CGridView rows and hide it if appointment_status(one of CGridView column) value equals 0
This is my code of CGridView,
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'bookings-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
'id',
'name',
'email',
'telephone',
'time',
'employee',
'appointment_status',
'client_ip',
'link' => array(
'header' => 'Confirmation',
'type' => 'raw',
'value' => 'CHtml::button("$data->appointment_status",array("onclick"=>"document.location.href=\'".Yii::app()->controller->createUrl("controller/action",array("id"=>$data->id))."\'"))',
'visible'=>$data->appointment_status==1,
),
array(
'class' => 'CButtonColumn',
),
),
));
But all I'm getting is error stating,
Undefined variable: data
It would be great help if someone can look into it.
you can use like this:
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'bookings-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
'id',
'name',
'email',
'telephone',
'time',
'employee',
'appointment_status',
'client_ip',
'link' => array(
'header' => 'Confirmation',
'type' => 'raw',
'value' => function ($data) {
if ($data->appointment_status == 1) {
return CHtml::button("$data->appointment_status", array("onclick" => "document.location.href=\'" . Yii::app()->controller->createUrl("controller/action", array("id" => $data->id)) . "\'"));
} else {
return;
}
}
),
array(
'class' => 'CButtonColumn',
),
),
));
Your 'visible' handling the column visibility and not the button, you can use custom attribute on model to create and handle the button visibility.
add to your model:
public function getConfirmationButton()
{
if ($data->appointment_status == 1) {
return CHtml::button($this->appointment_status,array("onclick"=>"document.location.href=\'".Yii::app()->controller->createUrl("controller/action",array("id"=>$this->id))."\'"));
} else {
return '';
}
}
and call it in your view:
..........
'link' => array(
'header' => 'Confirmation',
'type' => 'raw',
'value' => '$data->confirmationButton',
),
...........
visible is a boolean or a PHP expression that will be evaluated to give a boolean. During the evaluation $data is assigned to the current item from the dataProvider used. $data doesn't exist outside of the evaluation function evaluateExpression(). As such the implementation should be:
`visible` => '$data->appointment_status == 1',
You need to quote value of visible key in link array. So instead of this:
'visible'=>$data->appointment_status==1
It should be:
'visible'=>'$data->appointment_status==1'
it should work now.
You will get undefined variable because visible not allow any callback.
Try this solution, it's yii2 code and i don't know much of Yii.
'delete' => function ($url, $model) {
return ($model->isVisible($model)) ?
Html::a('<span class="glyphicon glyphicon-trash"></span>',
$url,
['title' => Yii::t('app', 'Delete')]) : '';
public static function isVisible($data)
{
return ($data->appointment_status == 1) ? true : false;
}

Yii, CGridView not showing records of a certain attribute

So I have created records and each has their own particular Service field. They are all filled up (not null).
In the MySQL database, there are also records of that field Service
But in my CGridView, my Service field is all blank. All the Service fields are blank for all the records. When I click on the button View - The one with the magnifying glass, it says Service - Not Set
Can I know what may cause the problem? Please help me Thanks!
UPDATED WITH CGRIDVIEW CODE
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'booking-grid',
'htmlOptions'=>array('class'=>'grid-view grid-size'),
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
array(
'name'=>'date',
'htmlOptions'=>array('style'=>'text-align:center', 'height'=>'50px', 'width'=>'80px'),
'headerHtmlOptions'=>array('height'=>'30px'),
),
array(
'name'=>'timeStart',
'header'=>"Time",
'htmlOptions'=>array('style'=>'text-align:center', 'width'=>'80px'),
),
array(
'name'=>'client_id',
'value'=>'GxHtml::valueEx($data->client)',
'filter'=>GxHtml::listDataEx(Client::model()->findAllAttributes(null, true)),
'htmlOptions'=>array('width'=>'200px'),
),
array(
'name'=>'service_id',
'value'=>'GxHtml::valueEx($data->service)',
'filter'=>GxHtml::listDataEx(Service::model()->findAllAttributes(null, true)),
),
array(
'name' => 'complete',
'header'=>'Status',
'value' => '($data->complete == 0) ? Yii::t(\'app\', \'Open\') : Yii::t(\'app\', \'Close\')',
'filter' => array('0' => Yii::t('app', 'Open'), '1' => Yii::t('app', 'Close')),
'htmlOptions'=>array('width'=>'60px', 'style'=>'text-align:center'),
),
'remarks',
array(
'class' => 'CButtonColumn',
'visible'=>true,
'htmlOptions'=>array('width'=>'70px'),
'template' => '{view}{update}{delete}{changeComplete}',
'buttons'=>array(
'view' => array(
'visible'=>"true",
),
'update' => array(
'visible'=>"true",
),
'delete' => array(
'visible'=>"UserIdentity::isRoleMember('Admin')",
),
'changeComplete' => array(
'visible' => '$data->complete=="0"',
'imageUrl' => 'images/complete.png',
'options' => array(
'title' => 'Complete',
),
'url' => 'Yii::app()->createUrl("booking/changeComplete", array("id"=>$data->id))',
'click' => "function(){
$.fn.yiiGridView.update(\"booking-grid\", {
type:'POST',
url:$(this).attr('href'),
success:function(data) {
$.fn.yiiGridView.update(\"booking-grid\");
}
})
return false;
}",
),
),
),
))); ?>
I have tried looking at other CGridViews with the same field, they look the same and they're working perfectly fine.
I think the problem is $data->service. Probably your data has a relation with the Service entity. So for accessing attributes of Service you should use $data->service->attributeName.

Yii Cgridview how to render in view 2 or more values from the same table in 1 column

I have a Cgridview that has multiple columns. I would like to merge and render the values of 3 columns in 1 column.
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'policy-grid',
'dataProvider'=>$dataProvider,
'filter'=>$dataProvider->model,
'selectableRows' => 1, // you can select only 1 row!!
'selectionChanged'=>'function(id){ var objectId = $.fn.yiiGridView.getSelection(id);
if (isNaN(objectId) || objectId == ""){return;} location.href =
"'.$this->createUrl('policy/view').'&id="+$.fn.yiiGridView.getSelection(id);}',
'htmlOptions'=>array('class'=>'grid-view grid_pointer',),
'columns'=>array(
array(
'name' => 'compliance',
'type' => 'html',
'value' => '$data->status == "ACTIVE" ? (CHtml::image($data->compliance ==
"SUFFICIENT" ? "images/policy_sufficient.png" : "images/policy_insufficient.png"))
: "N/A" ',
'filter' => VFFormUtil::getFilter(VFFormUtil::POLICY_COMPLIANCE),
'htmlOptions' => array('style'=>'text-align: center'),
),
array(
'name' => 'status',
'filter' => VFFormUtil::getFilter(VFFormUtil::ACTIVE_INACTIVE),
'htmlOptions' => array('style'=>'text-align: center'),
),
array(
'name' => 'coverage_left',
'htmlOptions' => array('style'=>'text-align: center'),
),
array(
'name' => 'model_year',
'htmlOptions' => array('style'=>'text-align: center'),
),
'make',
'model',
How would I combine 'model_year','make' and 'model', into 1 column in the views?
The correct way would be to create a new property variable in the model, and join the values afterFind().
See How to have multiple fields (D/M/Y) for single date property in Yii?, but you have to swap the find and save functions, as that user wants to do the opposite (split single column into three fields instead).
You can create a getter in the model:
public function getModelLabel()
{
return $this->model_year.' '.$this->make.' '.$this->model;
}
Then you can use modelLabel in your GridView as if it were a real attribute.

How to add radio into cgridview

I want to add single radio button to gridview row by row at yii. I googling like this "yii single radio button into cgridview" but I can't get what I want.
This is my gridview
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'acc-recei-grid',
'dataProvider'=>$model->search_implementerlist(),
'filter'=>$model,
'columns'=>array(
array(
'class' => 'CCheckBoxColumn',
'selectableRows' => 2,
'checkBoxHtmlOptions' => array(
'name' => 'implementers[]',
),
'value'=>'$data->id',
'checked'=>function($data, $row) use ($current_implementers){
return in_array($data->id, $current_implementers);
}
),
array(
'name'=>'lead_implementer',
'type'=>'raw',
'value' => 'CHtml::radioButtonList("lead_implementer","'. $lead_implementer .'",array($data->id), array("template" => "{input}", "style" => "width:20px!important", "separator"=>" "))',
),
'username',
),
)); ?>
How can I do?
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'acc-recei-grid',
'dataProvider'=>$model->search_implementerlist(),
'filter'=>$model,
'columns'=>array(
array(
'class' => 'CCheckBoxColumn',
'selectableRows' => 2,
'checkBoxHtmlOptions' => array(
'name' => 'implementers[]',
),
'value'=>'$data->id',
'checked'=>function($data, $row) use ($current_implementers){
return in_array($data->id, $current_implementers);
}
),
array(
'name'=>'lead_implementer',
'type'=>'raw',
'value' => 'CHtml::radioButtonList("lead_implementer","'. $lead_implementer .'",array("$data->id"=>"$data->id"), array("template" => "{input}", "style" => "width:20px!important", "separator"=>" "))',
),
'username',
),
)); ?>

CGridView filter dropdown from array

I have table of providers (id, title, onoff) where onoff column is a status: 1 = on, 0 = off
I dont have table in DB for these statuses, so I don't have model for statuses.
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'provider-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'name'=>'id',
'htmlOptions'=>array('width'=>'40px'),
),
'title',
array(
'name'=>'onoff',
'filter'=>CHtml::dropDownList('Provider[onoff]', '',
array(
''=>'All',
'1'=>'On',
'0'=>'Off',
)
),
),
array(
'class'=>'CButtonColumn',
'template'=>'{update}{delete}'
),
),
It filters data, but after ajax forget the state of dropdown
What is the best way to build dropdown in this case?
And what is the best way to substitute 1 to On and 0 to Off in datagrid cells?
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'provider-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'name'=>'id',
'htmlOptions'=>array('width'=>'40px'),
),
'title',
array(
'name'=>'onoff',
'value'=>'Crud::getOnoff($data->onoff)',
'filter'=>CHtml::listData(Crud::getOnoffs(), 'id', 'title'),,
),
array(
'class'=>'CButtonColumn',
'template'=>'{update}{delete}'
),
),
In model
public function getOnoffs()
{
return array(
array('id'=>'1', 'title'=>'On'),
array('id'=>'0', 'title'=>'Off'),
);
}
public function getOnoff($onoff)
{
if($onoff == 1)
return 'On';
else
return 'Off';
}
array( 'name' => 'language',
'value' => '$data->language',
'filter' => CHtml::listData( Vediodesc::model()->findall(), 'language', 'language'),
),