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)',
),
),
));
Related
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',
...
I want show pagination before gridview content. i tried renderPager() I cannot get widget object call the method
code here...
$gridview = $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$expirations,
'id'=>'image-grid-list',
'itemsCssClass'=>'table collections',
'ajaxUpdate'=>true,
'template'=>'{items}{summary}',
'pager'=>array(
'class'=>'CLinkPager',
'header'=>'',
),
'columns'=>array
(
array(
'class'=>'CCheckBoxColumn',
'selectableRows' => null,
'name'=>'image_id',
"value"=>'$data->image_id',
"id"=>"expirations",
"htmlOptions"=>array("style"=>"width:30px;")
),
array(
'name'=>'',
'type'=>'html',
'value'=>'
(!empty($data->image_title))?CHtml::image("/".Yii::app()->params["imagePath"]."/".$data->catalog->catalog_title."/thumb_".$data->image_title,"",array()):"no image"',
'header'=>''
),
array(
'name'=>'image_title',
'type'=>'raw',
'value'=>'CHtml::link($data->catalog->catalog_title."/".$data->image_title,"javascript:void(0);")."<br>#".$data->image_id."<br/><br/> Number of light boxes ".$data->collection_count." <br/><br/> Number of downloads ".$data->downloaded_count ',
'header'=>'Title'
),
array( // display 'author.username' using an expression
'name'=>'copyright_expiration_date',
'type'=>'raw',
'value'=>'(!empty($data->copyright_expiration_date))?date("m-d-Y",$data->copyright_expiration_date):"-"',
'header'=>'Rights expiration date'
),
array( // display 'author.username' using an expression
'name'=>'copyright_type',
'type'=>'raw',
'value'=>'$data->copyright_type',
'header'=>'Rights'
),
array( // display 'author.username' using an expression
'name'=>'',
'value'=>'$data->usage_and_terms',
'header'=>'Usage and terms'
),
array( // display 'create_time' using an expression
'name'=>'photographer_name',
'value'=>'$data->photographer_name',
'type'=>'raw',
'header'=>'Photographer name'
),
),
),true);
$gridview->renderPager();
I cannot call the method .. I want create widget object , call renderContent, renderPager etc
use template attribute.
'template' => '{pager}{items}{summary}',
eg.,
$gridview = $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$expirations,
'id'=>'image-grid-list',
'itemsCssClass'=>'table collections',
'ajaxUpdate'=>true,
'template' => '{pager}{items}{summary}',
'pager'=>array(
'class'=>'CLinkPager',
'header'=>'',
),
'columns'=>array
(
......
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 have the following table:
CREATE TABLE "place" (
"id" int8 NOT NULL DEFAULT nextval('place_id_seq'::regclass),
"name" varchar(128) NOT NULL,
"parent" int8,
"description" varchar(100)
)
WITH (OIDS=FALSE);
(It's PostgreSQL but this shouldn't be relevant here)
I created the CRUD through giix and changed a bit the relations to match my needs. So I ended up with:
public function relations() {
return array(
'parent0' => array(self::BELONGS_TO, 'Places', 'parent'),
'places' => array(self::HAS_MANY, 'Places', 'parent'),
);
}
The goal here is that a place can belong to another place and have multiple children places.
My problem is that I need to change the admin action to match the following grid:
Print manipulated by the inspector to reflect the desired behaviour
So my problem is to get the list of all the children objects and display it as a list of links. I tried change the respective admin.php view file for:
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'places-grid',
'dataProvider' => new CActiveDataProvider('Places', array('id'=>$model->places)),
'filter' => $model,
'columns' => array(
array(
'name'=>'parent',
'value'=>'GxHtml::valueEx($data->parent0)',
'filter'=>GxHtml::listDataEx(Places::model()->findAllAttributes(null, true)),
),
'name',
'places',
array(
'class' => 'CButtonColumn',
),
),
));
But this, as expected, throughs an error:
htmlspecialchars() expects parameter 1 to be string, array given
Also I don't know this would even work with the built in avanced search.
Could anyone kindly point me into the correct direction here?
After much search I found a way to solve my problem. Not sure if it's the best way but it works nicely:
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'places-grid',
'dataProvider' => new CActiveDataProvider('Places', array('id'=>$model->places)),
'filter' => $model,
'columns' => array(
array(
'name'=>'parent',
'value'=>'GxHtml::valueEx($data->parent0)',
'filter'=>GxHtml::listDataEx(Places::model()->findAllAttributes(null, true)),
),
'name',
array(
'header'=>'Children',
'type'=>'html',
'value'=> function($data) {
$placesLinks = array();
foreach ($data->places as $place) {
$placesLinks[] = GxHtml::link(GxHtml::encode(GxHtml::valueEx($place, 'name')), array('places/view', 'id' => GxActiveRecord::extractPkValue($place, true)));
}
return implode(', ', $placesLinks);
}
),
array(
'class' => 'CButtonColumn',
),
),
));
This uses Giix. If you don't use that then you'll have to change the link generation accordingly.
Cheers
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")'
),