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)',
),
...
Related
now i have the code like this:
$testArray1 = array(array(
'user_id'=>'1',
'name'=>array(
'david',
'haword'),
),
array(
'user_id'=>'2',
'name'=>'andrew',
),
);
print_r($testArray1);
$arrayDataProvider = new CArrayDataProvider ($testArray1,array(
//'KeyField'=>'_id',
'pagination'=>array(
'pageSize'=>10,
),
'sort'=>array(
),
));
$this->widget('zii.widgets.grid.CGridView', array(
'id' =>'BCImported-grid',
'dataProvider' =>$arrayDataProvider,
'columns' =>array('name'),
));
here is my result:
so the form only display name = andrew,and name = david and haword is not display.
how can I use DataProvider and CGridView in Yii to display array in array data like name = david and haword where 'user_id'=>'1'?
there is no way to display array in CGridView except you custom that value.
create some static function in your model
public static function implodeName($data)
{
if(isset($data['name'])) {
if(is_array($data['name'])) {
return implode(',', $data['name']);
}
else {
return $data['name'];
}
}
return '';
}
then in CGridView will be like this
$this->widget('zii.widgets.grid.CGridView', array(
'id' =>'BCImported-grid',
'dataProvider' =>$arrayDataProvider,
'columns' =>array(
array(
'header' => 'Names',
'name' => 'name',
'value' => 'YourModelName::implodeName($data)'
),
),
));
and the result should be :
david, hawords
How to filter the records using user's role at user grid view?
I am using yii-user extension. I am able to show user's role on user/admin gridview,but i can i use the filter on this? Here is my view:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'name' => 'id',
'type'=>'raw',
'value' => 'CHtml::link(CHtml::encode($data->id),array("admin/update","id"=>$data->id))',
),
array(
'name' => 'username',
'type'=>'raw',
'value' => 'CHtml::link(UHtml::markSearch($data,"username"),array("admin/view","id"=>$data->id))',
),
array(
'name'=>'email',
'type'=>'raw',
'value'=>'CHtml::link(UHtml::markSearch($data,"email"), "mailto:".$data->email)',
),
'create_at',
'lastvisit_at',
/*array(
'name'=>'superuser',
'value'=>'User::itemAlias("AdminStatus",$data->superuser)',
'filter'=>User::itemAlias("AdminStatus"),
),*/
array(
'name'=>'status',
'value'=>'User::itemAlias("UserStatus",$data->status)',
'filter' => User::itemAlias("UserStatus"),
),
array(
//'name'=>'assignments',
'header'=>Rights::t('core', 'Roles'),
'type'=>'raw',
'value'=>function($data) {
$roles = Rights::getAssignedRoles($data->id);
foreach($roles as $role){
$user_role=$role->name;
}
return $user_role;
}
),
array(
'class'=>'CButtonColumn',
'template'=>'{view}{delete}'
),
),
));
You can add filter like below:
array(
//'name'=>'assignments',
'header'=>Rights::t('core', 'Roles'),
'type'=>'raw',
'filter'=>CHtml::listData(Rights::model()->findAll(),'id','name'), //***
'value'=>function($data) {
$roles = Rights::getAssignedRoles($data->id);
foreach($roles as $role){
$user_role=$role->name;
}
return $user_role;
}
),
I assumed that Rights is a model and holds roles on it with id,name.
By the line that I indicated that with *** in comments, Yii will generate a dropdown as a filter which shows roles name as option value and roles id as option value.
I have a admin.php created by gii, inside it there's a table column 'lang_id' have relation to primary key 'id' of table 'lang'.
What should I put in the columns array ? I think i should use "Lang.name" but it didn't work.
protect/view/mainmenu/admin.php
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'mainmenu-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'menu_id',
'Lang.name', // I want this column display the name of Language, instead of lang_id
'name',
'remark',
array(
'class'=>'CButtonColumn',
'template'=>'{update}'
),
),
)); ?>
protect/model/Mainmenu.php
public function relations(){
return array(
'lang'=>array(self::HAS_ONE, 'Lang', 'lang_id')
);
}
public function search(){
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id,true);
$criteria->compare('menu_id',$this->menu_id);
$criteria->compare('lang_id',$this->lang_id);
$criteria->compare('name',$this->name,true);
$criteria->compare('sorting',$this->sorting);
$criteria->compare('remark',$this->remark,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
the relation name is lang. Whereas in the columns array you have used Lang (with l in uppercase).
So you will have to do this
'lang.name'
in the columns array
and second thing in the search()
You need to add one more line
$criteria->with = array(
'lang'
);
maybe this can work
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'mainmenu-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'menu_id',
array(
'name'=>'Lang',
'value'=>'$data->lang->name',
),
'name',
'remark',
array(
'class'=>'CButtonColumn',
'template'=>'{update}'
),
),
)); ?>
I have that code in my admin.php i.e. view file in my Yii Project.
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'topic-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'name',
'description:html', // I want to change this
array(
'class'=>'CButtonColumn',
),
),
)); ?>
Actually description:html is showing a lot of text description which is stored in my database.
So my question is that, I want to print just a single line of description here.
So I don't know how to accomplish it.
Any help will be appreciated.
Thanks.
Although hett has given an answer but you can do this also
array(
'name'=>'description',
'value'=>array($this,'showFewLines')
),
and then in your controller create a function
public function showFewLines($data,$row)
{
$allData=$data->description;
return substr($allData, 0, 40);
}
somehow
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'topic-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'name',
array(
'name'=>'description',
'value'=>function($data) {
return substr($data->description, 0, strpos($data->description, '<br>'));
},
),
array(
'class'=>'CButtonColumn',
),
),
)); ?>
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....???