我在Yii中遇到CGridView问题。
如果我使用Table作为模型,那么CGridView正常工作。它可以进行排序和过滤。
但如果我使用View作为模型,则无法对CGridView进行排序..
请帮忙, 感谢
以下是我的代码
的index.php
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'unit-history-grid',
'cssFile' => Yii::app()->request->baseUrl . '/css/gridview.css',
'dataProvider'=>$model->search(),
'filter'=>$model,
'enableSorting'=>false,
'columns'=>array(
'serial_no',
'customer_name',
'visit_count',
'startup_serviceman',
array(
'header' => 'Startup Date',
'name' => 'startup_date',
'type' => 'raw',
'value' => 'AppHelper::formatDate($data->startup_date)',
'filter' => false,
),
array(
'header' => '',
'type' => 'raw',
'value' => '"<a href=\"'. Yii::app()->request->baseUrl .'/inquiry/unitHistory/visit/serial_no/". $data->serial_no ."\">History Visit</a>"',
),
array(
'header' => '',
'type' => 'raw',
'value' => '"<a href=\"'. Yii::app()->request->baseUrl .'/inquiry/unitHistory/spareParts/serial_no/". $data->serial_no ."\">History Recommended Parts</a>"',
),
),
)); ?>
我的模特:ViewUnitHistory.php
class ViewUnitHistory extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @return ViewUnitHistory the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'view_unit_history';
}
public function primaryKey(){
return 'serial_no';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('serial_no, customer_name', 'required'),
array('serial_no', 'length', 'max'=>30),
array('customer_name', 'length', 'max'=>100),
array('visit_count', 'length', 'max'=>21),
array('startup_date, startup_serviceman', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('serial_no, customer_name, visit_count, startup_date, startup_serviceman', 'safe', 'on'=>'search'),
);
}
/**
* @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(
'customerProduct' => array(self::BELONGS_TO, 'CustomerProduct', 'serial_no'),
'userCreate' => array(self::BELONGS_TO, 'User', 'created_by'),
'userModify' => array(self::BELONGS_TO, 'User', 'modified_by'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'serial_no' => 'Serial No',
'customer_name' => 'Customer Name',
'visit_count' => 'Visit Count',
'startup_date' => 'Startup Date',
'startup_serviceman' => 'Startup Serviceman',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('t.serial_no',$this->serial_no,true);
$criteria->compare('t.customer_name',$this->customer_name,true);
$criteria->compare('t.visit_count',$this->visit_count,true);
$criteria->compare('t.startup_date',$this->startup_date,true);
$criteria->compare('t.startup_serviceman',$this->startup_serviceman,true);
$user = User::model()->findByPk(Yii::app()->user->getState('user_id'));
if ($user->branch_id != NULL) {
$criteria->addCondition('a.branch_id = ' . $user->branch_id);
}
$criteria->with = array(
'customerProduct.customer' => array('alias'=>'a'),
);
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
'sort'=>array(
'attributes'=>array(
'serial_no'=>array(
'asc'=>'t.serial_no',
'desc'=>'t.serial_no DESC',
),
'customer_name'=>array(
'asc'=>'t.customer_name',
'desc'=>'t.customer_name DESC'
),
),
),
));
}
}
以下是我的控制器中的actionIndex函数
/**
* Lists all models.
*/
public function actionIndex()
{
$model=new ViewUnitHistory('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['ViewUnitHistory']))
$model->attributes=$_GET['ViewUnitHistory'];
$this->render('index',array(
'model'=>$model,
));
}
答案 0 :(得分:1)
答案 1 :(得分:1)
'enableSorting'=&gt; false
当该属性设置为false时,网格是不可移植的。如果你在生成网格时不能自动进行分类,我认为不管怎么说也不会因为它不合格而按照你想要的方式进行排序。
这样做只需
$criteria->with = array(
'customerProduct.customer' => array('alias'=>'a'),
);
$criteria->order = 't.serial_no DESC';
CActiveDataProvider中的排序数组尝试对“enableSorting”属性设置为false的网格进行排序,因此无法执行此操作。
如果你想对网格进行排序只是启用排序,如果你不按标准执行顺序,这将按照你想要的顺序返回按db排序的结果。
希望这会有所帮助
答案 2 :(得分:0)
我刚试过这个,它运作得很好。我能够根据视图生成Gii完全crud(我必须为模型定义主键,但其余的很棒)。
现在我来看看'enableSorting'=&gt; false是什么,可能是这样:)。