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.
Related
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
i'm having trouble retrieving data from the database where i have joined table user to table family_tree thrice. CdbCommand returns the values selected from the first table (family_tree) fine, but the data selected from the 2nd and 3rd table (user) are not returned.
the schema of the tables are:
user
user_id | user_fullname
family_tree
tree_creator_id | tree_user1_id | tree_user2_id
model/familytree.php:
public function search()
{
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
//$criteria->alias='FamilyTree';
$criteria->select='t.*,A.*,B.*,C.*';
$criteria->join = 'join user A on A.user_id=tree_user1_id
join user B on B.user_id=tree_user2_id
join user C on C.user_id=tree_creator_id';
$criteria->compare('tree_id',$this->tree_id,true);
$criteria->compare('tree_creator_id',$this->tree_creator_id,true);
$criteria->compare('tree_user1_id',$this->tree_user1_id,true);
$criteria->compare('tree_user2_id',$this->tree_user2_id,true);
$criteria->compare('tree_type',$this->tree_type,true);
$criteria->compare('tree_type_name',$this->tree_type_name,true);
$criteria->compare('tree_grey_flag',$this->tree_grey_flag);
//$criteria->join('user', 'tree_creator_id=user.user_id');
//$criteria->compare('tree_user_id',$this->A->user_fullname, true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
view/familytree/admin.php
<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">
<?php $this->renderPartial('_search',array(
'model'=>$model,
)); ?>
</div><!-- search-form -->
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'family-tree-grid',
'dataProvider'=>$model->search(),
'columns'=>array(
'tree_id',
'tree_creator_id',
'C.user_fullname',
'tree_user1_id',
'A.user_fullname',
'tree_user2_id',
'B.user_fullname',
'tree_type',
'tree_type_name',
/*'tree_grey_flag',*/
array(
'class'=>'CButtonColumn',
),
),
)); ?>
Please you can try this below code:
model/familytree.php
/**
* #return array relational rules.
*/
public function relations() {
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'treeCreaterRel' => array(self::BELONGS_TO, 'user', 'tree_creator_id'),
'treeUser1Rel' => array(self::BELONGS_TO, 'user', 'tree_user1_id'),
'treeUser2Rel' => array(self::BELONGS_TO, 'user', 'tree_user2_id'),
);
}
public function search()
{
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->with = array('treeCreaterRel', 'treeUser1Rel', 'treeUser2Rel');
$criteria->together = true;
$criteria->compare('tree_id',$this->tree_id,true);
$criteria->compare('tree_creator_id',$this->tree_creator_id,true);
$criteria->compare('tree_user1_id',$this->tree_user1_id,true);
$criteria->compare('tree_user2_id',$this->tree_user2_id,true);
$criteria->compare('tree_type',$this->tree_type,true);
$criteria->compare('tree_type_name',$this->tree_type_name,true);
$criteria->compare('tree_grey_flag',$this->tree_grey_flag);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination'=>array(
'pageSize'=>'10', //Yii::app()->params['defaultPageSize'],
//'currentPage'=>$currentPage
),
'sort'=>array(
'defaultOrder'=>array(
'tree_id'=>CSort::SORT_DESC
),
),
));
}
view/familytree/admin.php
<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">
<?php $this->renderPartial('_search',array(
'model'=>$model,
)); ?>
</div><!-- search-form -->
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'family-tree-grid',
'dataProvider'=>$model->search(),
'columns'=>array(
array(
'header' => 'Tree Id',
'name' => 'tree_id',
'value' => '$data->tree_id'
),
array(
'header' => 'Tree Creater Id',
'name' => 'tree_id',
'value' => '$data->tree_id'
),
array(
'header' => 'Tree Creater Name',
'name' => 'tree_creator_id',
'value' => '$data->treeCreaterRel->user_fullname'
),
array(
'header' => 'Tree User1 Name',
'name' => 'tree_user1_id',
'value' => '$data->treeUser1Rel->user_fullname'
),
array(
'header' => 'Tree User1 id',
'name' => 'tree_user1_id',
'value' => '$data->tree_user1_id'
),
array(
'header' => 'Tree User2 Name',
'name' => 'tree_user2_id',
'value' => '$data->treeUser2Rel->user_fullname'
),
array(
'header' => 'Tree User2 id',
'name' => 'tree_user2_id',
'value' => '$data->tree_user2_id'
),
'tree_type',
'tree_type_name',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
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 the code below in view.php of a model:
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'name',
'cat_id',
'price',
array(
'name' => 'create_date',
'header' => 'Create Date',
'value' => 'Yii::app()->dateFormatter->format("yyyy-MM-dd HH:mm:ss", $data->create_date)'
),
array(
'name' => 'update_date',
'header' => 'update Date',
'value' => 'Yii::app()->dateFormatter->format("yyyy-MM-dd HH:mm:ss", $data->update_date)'
),
array(
'name' => 'last_visit_date',
'header' => 'Last visit Date',
'value' => 'Yii::app()->dateFormatter->format("yyyy-MM-dd HH:mm:ss", $data->last_visit_date)'
),
'hit',
'update_count',
'status',
'sold',
'active',
),
)); ?>
but I see
Yii::app()->dateFormatter->format("yyyy-MM-dd HH:mm:ss", $data->last_visit_date)
instead of converted date. I use this in admin.php and it is ok but in view.php it is not.
Delete quotes, use this:
'value' => Yii::app()->dateFormatter->format("yyyy-MM-dd HH:mm:ss", $model->last_visit_date)
You need to delete the quotes, because the CDetailView can only show one record. I think you also need to change $data to $model like this:
'value' => Yii::app()->dateFormatter->format("yyyy-MM-dd HH:mm:ss", $model->create_date)
If you want to show multiple records, you can use CListView
Note:
In the CDetailView you're using a CModel as data and in the CListView you're using a dataProvider
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,
),