I want to show list of my attributes (id, name, price, discount) and (total_price)column that is not an attribute.
我想显示我的属性列表(id,name,price,discount)和(total_price)列,它不是属性。
when I use this code, it shows table of data but I can not search or sort in column (total_price)
当我使用此代码时,它显示数据表但我无法在列中搜索或排序(total_price)
Can anyone hep me?
谁能帮我?
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'product-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'name',
array(
'header' =>'Price',
'name'=>'price',
'value'=>'$data->price',
),
array(
'header' =>'Discount',
'name'=>'discount',
'value'=>'$data->discount',
),
array(
'header' =>'total price',// total price doesn't save in Data Base
'name'=>'total_price',
'value'=>'total_price - (total_price * $data->discount/100)',
),
array(
'class'=>'CButtonColumn',
),
),
));
?>
1 个解决方案
#1
0
You can put on $model->search() method this:
你可以使用$ model-> search()方法:
$criteria=new CDbCriteria;
$criteria->select = "t.*, t.price - (t.price * t.discount/100) as total_price";
// EDITED: Added for Sort:
$sort = new CSort();
$sort->attributes = array(
'defaultOrder'=>'name ASC',
'total_price'=>array(
'asc'=>'total_price ASC',
'desc'=>'total_price DESC',
),
'*', // Add order in the other fields.
);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>$sort, // Added to sort.
);
And create a not persistence attribute on "Product" model Class:
并在“Product”模型类上创建一个非持久性属性:
public $total_price;
Finally put 'total_price' on rules() in array with ('safe', 'on'=>'search'), something like this:
最后将'total_price'放在数组中的rules()上('safe','on'=>'search'),如下所示:
array('id, name, price, discount, total_price', 'safe', 'on'=>'search'),
#1
0
You can put on $model->search() method this:
你可以使用$ model-> search()方法:
$criteria=new CDbCriteria;
$criteria->select = "t.*, t.price - (t.price * t.discount/100) as total_price";
// EDITED: Added for Sort:
$sort = new CSort();
$sort->attributes = array(
'defaultOrder'=>'name ASC',
'total_price'=>array(
'asc'=>'total_price ASC',
'desc'=>'total_price DESC',
),
'*', // Add order in the other fields.
);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>$sort, // Added to sort.
);
And create a not persistence attribute on "Product" model Class:
并在“Product”模型类上创建一个非持久性属性:
public $total_price;
Finally put 'total_price' on rules() in array with ('safe', 'on'=>'search'), something like this:
最后将'total_price'放在数组中的rules()上('safe','on'=>'search'),如下所示:
array('id, name, price, discount, total_price', 'safe', 'on'=>'search'),