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....???
Related
I have code like this :
<?php
$this->widget('bootstrap.widgets.TbGridView',array(
'id'=>'appliances-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'name'=>'price',
'type'=>'number',
'htmlOptions'=>array('style' => 'text-align: right;'),
),
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
'template'=>'{update}{delete}'
),
),
));
?>
the result is integer number only and search filter is work, how to change it into decimal format but search filter still working ?
I tried to change it into :
array(
'name'=>'price',
'type'=>'number',
'htmlOptions'=>array('style' => 'text-align: right;'),
'value'=>function($data)
{
return number_format($data->price,2,',','.');
},
),
But it makes search filter not working properly.
I found the answers here :
Yii Number Formatting
How to format numeric string using C.Gridview - Yii
And also I found this link Yii , show different date format in cgridview
Try This:-
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dataProvider,
'columns' => array(
'cost', // display the 'cost' attribute
array( // display the 'cost_in_dollars' using an expression
'name' => 'cost_in_dollars',
'value' => 'number_format($data->cost/100, 2)',
),
),
));
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 :)
I am using Yii's 'ItemAlias' function to give a custom name to a value, like this:
<?php
public static function itemAlias($type,$code=NULL) {
$_items = array(
'currency' => array(
'1'=>'US Dollar',
'2'=>'Euro',
'3'=>'GB Pound',
'4'=>'Chinese RMB',
'5'=>'Singaporese Dollar'
)
);
if (isset($code))
return isset($_items[$type][$code]) ? $_items[$type][$code] : false;
else
return isset($_items[$type]) ? $_items[$type] : false;
}
?>
Now, in CGridView, it currently displays the number (key) of the currency which it found in the database, but I want to display it's label. The CGridView is generated like this:
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'purchases-grid',
'dataProvider'=>$model->search(),
'columns'=>array(
'id',
'name',
'currency',
[..etc]
Can someone point me into the right direction to display the label instead of the key of the value? Thanks!
Based off of the limited code you showed this is what I think you want if itemAlias() is a function built into your model:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'purchases-grid',
'dataProvider'=>$model->search(),
'columns'=>array(
'id',
'name',
array(
'name' => 'currency',
'value' => '$data->itemAlias("currency",$data->currency)',
),
...
If it is some generic function somewhere not part of your model you would do this:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'purchases-grid',
'dataProvider'=>$model->search(),
'columns'=>array(
'id',
'name',
array(
'name' => 'currency',
'value' => 'itemAlias("currency",$data->currency)',
),
...
Below is my code in view.php file
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'myGrid',
'dataProvider' => $model->search(),
'filter' => $model,
'enableSorting' => true,
'columns' => array(
array(
'name' => 'id',
'header' => 'ID',
'value' => $data->id,
),
array(
'header' => 'Value',
'value' => '$data->getValue($data->id)', //getValue($id) is a custom method in the model.php file which returns a value after some calculations
'filter' => $model->listOfFilterValues(), //listOfFilterValues() is a custom method in the model.php file which returns a CHtml::listData
),
),
)
);
As you can observe, I am using a custom method in model.php file to get the Value column(because i cannot get it from a query).
The gridView appears and the dropdown list appears. Works fine till now.
The problem is that the filtering using the dropdown (in Value column) doesnt work. (because its not a column from the query output)
And also the sort on the Value column(when i click on the column header) doesnt work.
Is there a way to get this done? Your help is highly appreciated. Thank you.
Try this:
In Model:
class Plans extends BasePlans
{
...
public $planTypeSearch;
...
public function search() {
...
$criteria->compare('plan.plan_type', $this->planTypeSearch, true);
...
),
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'sort'=>array(
'attributes'=>array(
'planTypeSearch'=>array(
'asc'=>'plan.plan_type',
'desc'=>'plan.plan_type DESC',
),
),
),
);
In your view:
array(
'name'=>'planTypeSearch',
'header'=> 'Plan Type',
'value'=>'(isset($data->plan)) ? $data->plan->plan_type: null',
'filter'=>isset($plans) ? CHtml::listData($plans, 'plan_type','plan_type') : NULL,
),
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")'
),