I have the User model. It has the field "role" , which contains numbers 1,2,3. 1 - admin, 2 - moderator, 3 - simple user. I've created CRUD controller to manage users. And when I open view.php I want to see "admin" instead of "1" and etc.
When I operate with 2 states I use as bellow. But what about more then?
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'status' => array(
'name' => 'status',
'value' => ($data->status==0)?"Good":"Bad",
),
),
)): ?>
A way is this
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'status' => array(
'name' => 'status',
'value' => ($data->status==0)?"Good":"Bad",
),
array(
'name' => 'your_name'
'value' => array($this,'yourActionForDescription')
)
),
)): ?>
this means that grid view will search a function in its controller for a function yourActionForDescription()
In controller
public function yourActionForDescription($data,$row){
$your_value = $data->your_value_column_name;
//do what you need for retrive your value
//for eg.
return $yourSearchedModel->description ;
}
another way is based on relation
Related
i want to create a table like this Yii Booster Gridview table
here is my code in controller:
$rawData=Jobspecs::model()->with('customer')->findAll();
$gridDataProvider=new CArrayDataProvider($rawData, array(
'id'=>'user',
'sort'=>array(
'attributes'=>array(
'id', 'customer',
),
),
));
$gridColumns = array(
array('name'=>'id', 'header'=>'Js No.', 'htmlOptions'=>array('style'=>'width: 60px')),
array('name'=>'WHAT TO PUT HERE TO SHOW CUSTOMER NAME', 'header'=>'Customer Name'),
array(
'htmlOptions' => array('nowrap'=>'nowrap'),
'class'=>'booster.widgets.TbButtonColumn',
'viewButtonUrl'=>null,
)
);
in my model sapcustomers:
return array(
'customer'=>array(self::BELONGS_TO, 'Sapcustomers', 'customer'),
);
jobspecs model
return array(
'cardname'=>array( self::HAS_MANY, 'Jobspecs', 'customer' ),
);
my view
<?php
$this->widget(
'booster.widgets.TbGridView',
array(
'type' => 'bordered',
'dataProvider' => $gridDataProvider,
'template' => "{items}",
'columns' => $gridColumns,
)
);
?>
as you can see i joined sapcustomers and jobspecs table. my question is what code i need to put on the $gridcolums to show the customer name data from the table sapcustomer. thanks for the help
I think this should work for you:
$gridColumns = array(
[...]
array(
'name'=>'customer',
'value'=>'$data->customer->name',
'header'=>'Customer Name'
),
);
Explanation
The $data stands for the Jobspecs-model from the actual row. Then you access the related customer and his name.
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 these codes in my view file:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'name' => 'Name',
'type'=>'raw',
'value' => 'CHtml::link(CHtml::encode($data->profile->first_name." ".$data->profile->last_name),array("match/view","id"=>$data->id))',
),
array(
'name' => 'Similiarity Score',
'type'=>'raw',
'value' => array($this, 'calculateScore'),
),
),
)); ?>
You will notice that the second column call the function calculateScore($data, $row) in the controller file. Is there anyway I can sort the table based on these scores?
I assume that your result is from a database and you use a CActiveDataProvider or CSqlDataProvider. In this case you will have to move the logic for calculateScore into the DB query somehow. You can add a public property score to your model class and add this to the select property of your CDbCriteria:
$criteria->select = array('*', '... SQL FOR SCORE CALC HERE ... AS score');
Then you will be able to sort by that score in the sort definition of your CActiveDataProvider:
'sort' => array(
'attributes' => array(
// ...
'score',
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")'
),