Display name using condition in TbGridView - yii

I have stored the status value 0 or 1 which means it Enabled or Disabled. I am displaying values in the TbGridView. In status field the display value is 0 or 1. But i want to display Enabled or Disabled.
My code is here,
<?php $this->widget('bootstrap.widgets.TbGridView',array(
'id'=>'states-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'state_val',
'state_name',
'status',
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
'template'=>'{update}',
),
),
)); ?>

Try this way:
[
'attribute' => 'status',
'value' => '($data->status) ? "Enabled" : "Disabled"',
],

Related

How to disable sort for a column in CGridView?

There is the enableSorting flag, that, if equals false, disable sorting for all grid columns. But how can I do this for just one specific column?
try this
'sortable'=>false,
Here is example ,
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'tasks-grid',
'dataProvider'=>$model->search(),
//'filter'=>$model,
'columns'=>array(
'id',
array(
'header'=>'Surname',
'value'=> '$data->surname',
'name'=> 'surname',
'sortable'=>false,
),
'due_date',
'status',
),

Passing ID to CGridView

How to pass grid row id from CGridView to filter values in another CGridView opened in Dialog
My View Code for Form Grid(see screenshot),
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'document-grid',
'dataProvider'=>$model->search(),
'columns'=>array(
array(
'class' => 'CButtonColumn',
'template' => '{edit}{DocumentDelete}',
'buttons' => array(
'DocumentDelete' => array(
'imageUrl'=>Yii::app()->request->baseUrl.'/images/delete.png',
'url'=>'Yii::app()->createUrl("baseContact/DocumentDelete", array("id"=>$data->crm_document_id))',
),
'edit' => array(
'imageUrl'=>Yii::app()->request->baseUrl.'/images/update.png',
'url'=>'Yii::app()->createUrl("baseContact/edit", array("id"=>$data->crm_document_id))',
),
),),
'crm_document_id',
'name',
'doc_type',
'delivery_method',
'content_subject',
'content_body',
'is_active',
),
)); ?>
View Code for Popup Grid,
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'ManagedocumentAttach-grid',
'dataProvider'=>DocumentAttachmentModel::model()->search(),
//'filter'=>$model,
'columns'=>array(
array(
'name'=>'',
'value'=>'CHtml::checkBox("cid[]",null,array("value"=>$data->crm_document_attachment_id,"id"=>"cid_".$data->crm_document_attachment_id))',
'type'=>'raw',
'htmlOptions'=>array('width'=>5),
//'visible'=>false,
),
'crm_document_id',
'name',
'type',
),
)); ?>
how to pass Form edited row id to Popup GridView ?
Clearly you use AJAX to get the popup so i would first would make sure the ID of the edit button is the same as the ID from the item you want to open. Then you can do the following JS (using JQUERY)
$(".edit").on("click", function() {
var id = $(this).attr("id");
$.ajax({
type:"POST",
url: "controller/action/"+id;
success: function(data) {
//open dialog box and fill it with data
}
});
You could also add the id as data so you can get it with $_POST instead of it being a variable defined by the function. If you write the JS in a php document you can use $this->createUrl, but that is just whatever you prefer.
If with this you can not solve your problem then let us see how you implemented it right now.
i am not sure ... but i have a technique to do it....
if i have to do something like this ....
i will give class by htmlOptions and after that i will get value which should be an id to open popup..
example
<?php $this->widget('bootstrap.widgets.TbGridView', array(
'dataProvider' => $dataProvider ,
'type' => TbHtml::GRID_TYPE_BORDERED,
'template' => "{items}",
'columns' => array(
array(
'name' => 'vendor_configuration_id',
'header' => $dataProvider->model->getAttributelabel('vendor_configuration_id'),
'htmlOptions' => array('class'=>'idClass'),
),
array(
'name' => 'menu_type',
'header' => $dataProvider->model->getAttributelabel('menu_type'),
'htmlOptions' => array(),
),
?>
now jquery for it
$('.idClass').on("click",function(){
var neededId = $(this).html();
alert(neededId );
//openn popup based on this id or caal ajax to retrive data based on this
});

BootGridView Filter not working in Yii Framework

I have tried to implement the sorting using BootGridView but it does not seem to work. Here is the line of code.
<?php $this->widget('bootstrap.widgets.BootGridView', array(
'id'=>'user-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'user_id',
array('name'=>'user_type',
'value'=>'User::getType($data->user_type)'),
'username',
array('header'=>'Name',
'type'=>'raw',
'value'=> 'CHtml::link($data->firstname." ".$data->lastname, array("view", "id"=>$data->id))'
),
array('name' => 'status',
'type' => 'raw',
'value' => '$data->status == 1 ? "Active" : CHtml::link("In Active", "javascript:void(0);", array("id" => "active_" . $data->id, "onClick"=>"js:activate($data->id)"))'
),
/*
'email',
'dob',
'profession',
'hobby',
'height',
'weight',
'weight_taken_on',
'login_attempt',
'registration_date',
*/
array(
'class'=>'bootstrap.widgets.BootButtonColumn',
),
),
)); ?>
The code does not seem to sort the table as expected. What could be the possible problem in this. ?
Sorting is configured in the data provider, not in the grid view. So you should look into the search() method of your model. You need something like this:
return new CActiveDataProvider('User',array(
// ...
'sort' => array(
'attributes' => array(
'name',
'email',
// ...
Have a look at CSort and especially the attributes property there to learn more about sort options you can use here.

Yii list view update on radio button selection

In a model have 4 fields. id, name, address, status. In the list view showing a radio button list for displaying status. In the Listview i want to update the list based on the radio button selection. How it possible in Yii?
You can do this by:
array(
'header' => 'Status',
'name' => 'status',
'value' => '($data->status == 0) ? "Yes" : "No"'
),
in the view file CGridview widget:like this
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'users-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array( array(
'header' => 'Status',
'name' => 'status',
'value' => '($data->status == 0) ? "Yes" : "No"'
),`

How do you add a button to a CGridView?

Sounds simple, right? I've searched high and low and I can't find out how to do this. I have a CGridView:
$dataProvider = new CArrayDataProvider ($auctions);
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
'id::ID',
'product.title::Title',
'state::Status',
),
));
I want to add a fourth column that only contains a simple button that will execute javascript when pressed. I've tried:
array(
'class' => 'CButtonColumn',
),
This just gives me an error:
Undefined property: stdClass::$primaryKey
Any ideas?
Try this:
$dataProvider = new CArrayDataProvider ($auctions);
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
'id::ID',
'product.title::Title',
'state::Status',
array(
'type' => 'raw',
'value' => '<button onclick=\'alert("It works!")\' value="clickme"/>'
)
),
));
The best way to do it is with CButtonColumn::template and CButtonColumn::buttons.
array(
'class' => 'CButtonColumn',
'template' => '{view} {update} {delete} {copy}',
'buttons'=>array(
'copy' => array(
'label'=>'Copy', // text label of the button
'url'=>"CHtml::normalizeUrl(array('copy', 'id'=>\$data->id))",
'imageUrl'=>'/path/to/copy.gif', // image URL of the button. If not set or false, a text link is used
'options' => array('class'=>'copy'), // HTML options for the button
),
),
),
In this example there are the three default buttons and one custom, the 'copy' button.
If you don't want some of the default buttons (i.e. view, update and delete), you can remove them. Then, define the properties of the button that you added. I defined label, url, image and html options.