我正在尝试使用YII CGridview来显示一些数据。
这是家我的模型搜索功能看起来像:
/**
* 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()
{
$criteria=new CDbCriteria;
$criteria->compare('ip',$this->ip,true);
$criteria->compare('first_use',$this->first_use,true);
$criteria->compare('last_use',$this->last_use);
$criteria->compare('memberid',$this->memberid);
$criteria->compare('countryid',$this->countryid);
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
));
}
这就是我的观点看起来像
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'iplog-grid',
'dataProvider'=>$oIPLog->search(),
'filter'=>$oIPLog,
'summaryText' => 'showing you {start} - {end} of {count} logged Ips',
'columns'=>array(
array(
'name'=>'ip',
'type'=>'raw',
),
array(
'name'=>'first_use',
'type'=>'datetime',
),
array(
'name'=>'last_use',
'type'=>'datetime',
),
),
));
显示CGridview是可行的,但我似乎无法让它上面的过滤器工作。它发送调用,我没有得到任何错误作为响应,它只是再次返回整个未过滤的数据。。
有线索吗?
您的控制器到底是什么样子的?
为了让CGridview过滤器发挥作用,您需要在控制器中检查是否设置了任何过滤器,然后返回过滤后的对象。
为了澄清,像这样的东西应该放在你的控制器动作中
$oObject = new Object('search');
if (isset($_GET['Object'])) {
$oObject->attributes = $_GET['Object'];
}
希望这能帮助
您必须应用以下几点:1.在控制器的函数中指定全局变量($_REQUEST)例如
$model = new User('search');
$model->unsetAttributes(); // clear any default values
if (isset($_REQUEST['User'])){
$model->attributes = $_REQUEST['User'];
}
$this->render('admin', array(
'model' => $model,
));
在搜索表单中设置方法类型
<?php $form=$this->beginWidget('CActiveForm', array( 'action'=>Yii::app()->createUrl('user/admin'), 'method'=>'POST', )); ?>
3.在Cgrid视图中,您必须定义类似的url
'ajaxUrl'=>Yii::app()->createUrl( 'controller/function' ),