I've created crud for user table. In admin page I've implemented custom code for displaying custom column values in $this->widget('zii.widgets.grid.CGridView',array())....
When I try to add code for custom data in column filter bar from that column of the table is not displaying.
Following is my code in view of the admin page
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'users-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'itemsCssClass' => 'table table-responsive table-striped table-hover table-bordered',
'columns'=>array(
array(
'header' => 'ID',
'type' => 'raw',
'value' => function($model, $key, $index) {
return 'HJ-'.date('Ym', strtotime($model->created_date)).$model->id;
},
),
array(
'header' => 'First Name',
'type' => 'raw',
'value' => '$data->fname',
),
array(
'header' => 'Last Name',
'type' => 'raw',
'value' => '$data->lname',
),
'contact_no',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
In above code, filter bar is displaying for 'contact_no' but its not displaying for ID, First Name and Last name due to custom code. How to add filter bar to this?
You have to indicate which attribute the data belongs to via name
. From the docs:
[name]: the attribute name of the data model. Used for column sorting, filtering and to render the corresponding attribute value in each data cell. If value is specified it will be used to rendered the data cell instead of the attribute value.
As such your code should be as follows:
array(
'header' => 'First Name',
'type' => 'raw',
'value' => '$data->fname',
'name' => 'fname',
),
Also:
When a column is specified as a string, it should be in the format of
"name:type:header"
, where "type" and "header" are optional.
thus the above can be shortened as follows:
'columns'=>array(
...
'fname:raw:First Name',
'lname:raw:Last Name',
...