我知道有类似的问题已被问到,对不起,但我不能为我的生活看到我做错了什么。任何帮助将不胜感激。我想要做的就是在CGridView中添加一个额外的列,在数据库中显示总共两个值。
这些是我采取的步骤:
Database
User(forename, surname, part1, part2)
class User extends Controller:
protected function getTotal($data,$row,$dataColumn){
return $data->getAttribute('part1')+$data->getAttribute('part2');
}
public function actionIndex(){
$dataProvider=new CActiveDataProvider('User');
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
class Stadium extends CActiveRecord:
public function rules(){
return array(
array('forename, surname', 'required'),
array('forename', 'surname', 'max'=>50),
array('part1, part2', 'numerical', 'integerOnly'=>true),
array('forename, surname, part1, part2, total', 'safe', 'on'=>'search'),
);
}
public function attributeLabels()
{
return array(
'forename' => 'forename',
'surname' => 'surname',
'part1' => 'part1',
'part2' => 'part2',
'total' => array(
'asc'=>'part1 + part2',
'desc'=>'part1 + part2 desc',
),
);
}
public function search(){
$criteria=new CDbCriteria;
$criteria->compare('forename',$this->forename,true);
$criteria->compare('surname',$this->surname,true);
$criteria->compare('part1',$this->part1);
$criteria->compare('part2',$this->part2);
$criteria->compare('total',$this->part1 + $this->part2,true);
$sort = new CSort();
$sort->attributes = array(
'total'=>array(
'asc'=>'part1 + part2 ASC',
'desc'=>'part1 + part2 DESC',
),
'*',
);
return new CActiveDataProvider(User', array(
'criteria'=>$criteria,
'sort'=>$sort,
));
}
}
view/User/index.php
<?php $this->widget('zii.widgets.grid.CGridView', array(
'itemsCssClass'=>'table table-hover',
'dataProvider'=>$dataProvider,
'template'=>"{items}",
'columns'=>array(
array('name'=>'forename', 'header'=>'Forename'),
array('name'=>'surname', 'header'=>'Surname'),
array('name'=>'total', 'header'=>'Total', 'value'=>array($this, 'getTotal')),
),
)); ?>
<?php $this->endWidget();?>
非常感谢任何建议。
答案 0 :(得分:1)
解决方案: 好的,这就是你需要做的事情:
class Stadium extends CActiveRecord:
public $total; //add this line
//your rules should be fine
//don't know if you need the asc and desc on attribute labels i just have one
'total' => 'Total',
$criteria->compare('total',$this->total); //change this line
//i didn't use a Csort you might be able to but this is they way i did it
return new CActiveDataProvider(User', array(
'criteria'=>$criteria,
'sort'=> array(
'attributes' => array(
'total'=>array(
'asc'=>'(part1 + part2) ASC',
'desc'=>'(part1 + part2) DESC',
),
'*',
),
),
));
//add this inside your model also
public function afterFind() {
parent::afterFind();
$this->total = $this->part1+ $this->part2;
return;
}
模型中的其他所有内容都很好。然后在CGridView上只需更改一行:
array('name'=>'total', 'header'=>'Total'), //don't need to set value it is part of the model like other columns now
http://www.yiiframework.com/forum/index.php/topic/21114-create-now-model-variable/
原始答案:
我相信如果您要使用CSort
对象,则需要使用CActiveDataProvider
将其添加到setSort()
,否则您无需创建CSort
你可以把它直接放到DataProvider。
选项1:
$sort = new CSort();
$sort->attributes = array(
'total'=>array(
'asc'=>'part1 + part2 ASC',
'desc'=>'part1 + part2 DESC',
),
'*',
);
$data = new CActiveDataProvider(User', array(
'criteria'=>$criteria,
));
$data->setSort($sort);
return $data;
选项2:
return new CActiveDataProvider(User', array(
'criteria'=>$criteria,
'sort'=> array(
'attributes' => array(
'total'=>array(
'asc'=>'part1 + part2 ASC',
'desc'=>'part1 + part2 DESC',
),
'*',
),
),
));
这可能会有所帮助 http://www.yiiframework.com/wiki/381/cgridview-clistview-and-cactivedataprovider/#hh11 http://www.yiiframework.com/doc/api/1.1/CDataProvider#setSort-detail
答案 1 :(得分:0)
Pitchinnate的回答可能对你有所帮助,而且,我注意到你的一个倾向是你把逗号放在不应该出现的地方。例如
而不是:
$sort->attributes = array(
'total'=>array(
'asc'=>'part1 + part2 ASC',
'desc'=>'part1 + part2 DESC',
),
'*',
);
你需要:
$sort->attributes = array(
'total'=>array(
'asc'=>'part1 + part2 ASC',
'desc'=>'part1 + part2 DESC'
),
'*'
);
你应该摆脱最后一个元素之后的所有逗号。