How can I change cbuttoncolumn label dynamically in yii cgridview - php

I am trying to change cbuttoncolumn label dyanamically. But somehow it does not work. My code is
array(
'class'=>'CButtonColumn',
'template'=>'{publish}',
'buttons'=>array(
'publish'=>array(
//'type'=>'raw',
'label'=>'$data->content_type == 1 ? "View & Publish" : "Publish"',
'icon'=>'ok',
'url'=>'Yii::app()->createUrl("/admin/contentToPublish/publish")',
),
),
),
How can i do this??

You can just create a new column with custom links, something like this:
In your model :
public function getMyValue(){
$linkOne = CHtml::link("$this->labelOne", Yii::app()->createUrl("model/action",array("id"=>$this->id)));
$linkTwo = CHtml::link("$this->labelTwo", Yii::app()->createUrl("model/action",array("id"=>$this->id)));
return $linkOne.' '.$linkTwo;
}
And in your CGridView :
'columns'=>array(
'labelOne',
'labelTwo',
array(
'type' => 'raw',
'header' => 'Manage',
'value' => '$data->getMyValue()',
),
),
Or, you can use the visible attribute in CButtonColumn:
array(
'class'=>'CButtonColumn',
'template'=>'{publish}{viewPublish}',
'buttons'=>array(
'publish'=>array(
//'type'=>'raw',
'label'=>'Publish',
'visible' => '$data->content_type != "1"',
'icon'=>'ok',
'url'=>'Yii::app()->createUrl("/admin/contentToPublish/publish")',
),
'viewPublish'=>array(
//'type'=>'raw',
'label'=>'View & Publish',
'visible' => '$data->content_type == "1"',
'icon'=>'ok',
'url'=>'Yii::app()->createUrl("/admin/contentToPublish/publish")',
),
),
),
Hope that helps :)

Related

Include a pagination and summary text alone before gridview in YII

I want show pagination before gridview content. i tried renderPager() I cannot get widget object call the method
code here...
$gridview = $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$expirations,
'id'=>'image-grid-list',
'itemsCssClass'=>'table collections',
'ajaxUpdate'=>true,
'template'=>'{items}{summary}',
'pager'=>array(
'class'=>'CLinkPager',
'header'=>'',
),
'columns'=>array
(
array(
'class'=>'CCheckBoxColumn',
'selectableRows' => null,
'name'=>'image_id',
"value"=>'$data->image_id',
"id"=>"expirations",
"htmlOptions"=>array("style"=>"width:30px;")
),
array(
'name'=>'',
'type'=>'html',
'value'=>'
(!empty($data->image_title))?CHtml::image("/".Yii::app()->params["imagePath"]."/".$data->catalog->catalog_title."/thumb_".$data->image_title,"",array()):"no image"',
'header'=>''
),
array(
'name'=>'image_title',
'type'=>'raw',
'value'=>'CHtml::link($data->catalog->catalog_title."/".$data->image_title,"javascript:void(0);")."<br>#".$data->image_id."<br/><br/> Number of light boxes ".$data->collection_count." <br/><br/> Number of downloads ".$data->downloaded_count ',
'header'=>'Title'
),
array( // display 'author.username' using an expression
'name'=>'copyright_expiration_date',
'type'=>'raw',
'value'=>'(!empty($data->copyright_expiration_date))?date("m-d-Y",$data->copyright_expiration_date):"-"',
'header'=>'Rights expiration date'
),
array( // display 'author.username' using an expression
'name'=>'copyright_type',
'type'=>'raw',
'value'=>'$data->copyright_type',
'header'=>'Rights'
),
array( // display 'author.username' using an expression
'name'=>'',
'value'=>'$data->usage_and_terms',
'header'=>'Usage and terms'
),
array( // display 'create_time' using an expression
'name'=>'photographer_name',
'value'=>'$data->photographer_name',
'type'=>'raw',
'header'=>'Photographer name'
),
),
),true);
$gridview->renderPager();
I cannot call the method .. I want create widget object , call renderContent, renderPager etc
use template attribute.
'template' => '{pager}{items}{summary}',
eg.,
$gridview = $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$expirations,
'id'=>'image-grid-list',
'itemsCssClass'=>'table collections',
'ajaxUpdate'=>true,
'template' => '{pager}{items}{summary}',
'pager'=>array(
'class'=>'CLinkPager',
'header'=>'',
),
'columns'=>array
(
......

How to Disable Button on CButtonColumn Table

I want to disable the update button from CGridView Table. I don't know how to do it. Just disable the update button, not delete the function of update. Anybody know how to do it? Thanks
'columns' => array(
// All your columns here
// ...
array(
'template' => '{view}{delete}',
'class' => 'CButtonColumn',
),
),
It's enough :)
EDIT: above solution removes the button. The following disable it (by removing the link):
'columns' => array(
// All your columns here
// ...
array(
'template' => '{view}{update}{delete}',
'class' => 'CButtonColumn',
'buttons' => array(
'update' => array(
'url' => ''
),
),
),
You have to change the 'template' property of your CButtonColumn.
The default value for 'template' is '{update}{view}{delete}'.
So you have to do this:
array(
'class' => 'CButtonColumn',
'template' => '{view}{delete}',
),

text instead of icons on TbButtonColumn using Yii-Booster

I'm using TbButtonColumn to render some icon buttons. I want to render text instead of the icons. Is this possible and how would I alter the following code to do this?
$gridColumns = array(
array('name'=>'nick_name', 'header'=>'Interests Sets'),
array(
'htmlOptions' => array('nowrap'=>'nowrap'),
'class'=>'bootstrap.widgets.TbButtonColumn',
'template'=>'{add} {view}',
'buttons'=>array(
'add' => array
(
'label'=>'See this friend\'s list',
'icon'=>'plus',
'url'=>'Yii::app()->createUrl("itemList/viewlist", array("friend_id"=>$data->id))',
'options'=>array(
'class'=>'btn btn-small',
),
),
'view' => array(
'label'=>'Search under this friend\'s interesrs',
'url'=>'Yii::app()->createUrl("friend/filter", array("friend_id"=>$data->id))',
'options'=>array(
'class'=>'btn btn-small',
),
),
),
)
);
I know nothing about yii-booster, but if it's anything like Yii's CButtonColumn, you only need to set the imageUrl to false. Like this:
'view' => array(
'imageUrl'=>false, // Setting an empty string does not work in vanilla Yii.
'label'=>'Search under this friend\'s interesrs',
'url'=>'Yii::app()->createUrl("friend/filter", array("friend_id"=>$data->id))',
'options'=>array(
'class'=>'btn btn-small',
),
),
$gridColumns = array(
...
'buttons'=>array(
'add' => array
(
'label'=>'text instead of the icons' . 'See this friend\'s list',
'url'=>'Yii::app()->createUrl("itemList/viewlist", array("friend_id"=>$data->id))',
'options'=>array(
'class'=>'btn btn-small',
),
),
),
)
);
if you set 'icon'=>'ololo', run this code:
if (isset($this->icon))
{
if (strpos($this->icon, 'icon') === false)
$this->icon = 'icon-'.implode(' icon-', explode(' ', $this->icon));
$this->label = '<i class="'.$this->icon.'"></i> '.$this->label;
}

how to show "Yes/No" CGridView yii depending on the flag field 0/1?

I am stuck in a problem in CGridView yii, my refund field show 0/1 but I want to show "Yes" if 0 and "No" if 1, without using any second table.
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'transaction-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'member_id',
array(
'header' => 'MemberName',
'name' => 'member_id',
'value' => '$data->member->f_name'
),
'refund',
'band_id',
array(
'class'=>'CButtonColumn',
'template'=>'{view}',
),
),
));
Both of the other answers will work, but the cleanest way to do it is:
'columns'=>array(
'id',
'member_id',
...
'refund:boolean',
),
There are a bunch of CGridView column data types that are auto-used if you use colons like above. More info here: https://github.com/samdark/a-guide-to-yii-grids-lists-and-data-providers/blob/master/grid-columns.md
array(
'name' => 'refund',
'header' => "Refund",
'value' => '$data->refund?Yii::t(\'app\',\'Yes\'):Yii::t(\'app\', \'No\')',
'filter' => array('0' => Yii::t('app', 'No'), '1' => Yii::t('app', 'Yes')),
'htmlOptions' => array('style' => "text-align:center;"),
),
Hope this will solve your problem.
Replace "refund" with this code.
array(
'header' => 'Refund',
'name' => 'refund',
'value' => '($data->refund == 0) ? "Yes" : "No"'
),
When displaying a boolean field in a CGridView use the name:type:header format when creating the columns to specify the type as boolean. E.g.
$this->widget('zii.widgets.grid.CGridView', array(
...
'columns'=>array(
'id',
'refund:boolean',
),
If you want to change the way the field is displayed in a CActiveForm change the render method to use either a checkbox or a dropdown list. My preference is dropdown list because it gives you the option of setting the value back to null.
$form->dropDownList($model,'refund', array(null=>"Not checked", 0=>"No", 1=>"Yes"));
Quick fix:
Replace 'refund', with:
array(
'name' => 'refund',
'type' => 'raw',
'value' => function($model){
return $model->refund == 1 ? 'No' : 'Yes';
}
),
IN VIEWS NAMES ADMIN.PHP
array(
'name'=>'status',
'header'=>'status',
'filter'=>array('1'=>'Inacive','2'=>'Active'),
'value'=>'($data->status=="1")?("Inacive"):("Active")'
),

CGridView encoded the value

I'm following the documents here http://www.yiiframework.com/wiki/278/cgridview-render-customized-complex-datacolumns/
So I have the following in view
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'item-table-grid',
'dataProvider'=>$model->search(),
'itemsCssClass'=>'item-table-grid',
'columns'=>array(
'customer_name',
array(
'name'=>'Edit',
'value'=>array($model, 'editLink'),
),
),
));
And here is the editLink function in model
public function editLink($data, $row) {
$link = '';
if ($data->is_draft) {
$link = 'Edit';
}
return $link;
}
The problem that I'm having is that the return value is encoded so I get <a href=...>
Is there a way to tell CGridView not to encode the value?
Thanks
Solution A:
array(
'name'=>'Edit',
'type' => 'raw',
'value'=>array($model, 'editLink'),
),
B: (not good enough)
array(
'name' => 'Edit',
'class' => 'CLinkColumn',
'urlExpression' => '$data->is_draft ? "customer/update/{$data->id}" : "#disabled"',
'label' => 'edit',
),
try this ..
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'item-table-grid',
'dataProvider'=>$model->search(),
'itemsCssClass'=>'item-table-grid',
'columns'=>array(
'customer_name',
array(
'name'=>'Edit',
'type' => 'raw',
'value'=>array($model, 'editLink'),
),
),
));
my code in CGridView
array(
'name'=>'Post Content',
'value'=>array($model,'postContent'),
'type'=>'html',
'htmlOptions'=>array('width'=>'380'),
),
in Model I have the following method
public function postContent($data){
echo $data->content;
}
it works fine but when i click on another page of my pagination
then it doesn't works means it work only on Index page first time opened...
plz any solution....???

Categories