yii中Cgridview的自定义筛选
我正在使用CGridView显示postgres函数的结果。CGridView工作正常。现在我想在模型中使用一个不同的函数来过滤CGridView CGridView的当前代码如下yii中Cgridview的自定义筛选,yii,Yii,我正在使用CGridView显示postgres函数的结果。CGridView工作正常。现在我想在模型中使用一个不同的函数来过滤CGridView CGridView的当前代码如下 $this->widget('zii.widgets.grid.CGridView', array( 'id'=>'purchase-grid', 'itemsCssClass'=>'table table-bordered table-condensed table-hover t
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'purchase-grid',
'itemsCssClass'=>'table table-bordered table-condensed table-hover table-striped dataTable',
'filter'=>$model,
'dataProvider'=>$model->search(),
..............
$model->search()函数接受用于从表中选择行的id。我使用CSqlDataProvider在$model->search()中运行自定义查询并返回数据提供程序。如果我使用上面的代码,它将显示CGridView中所有字段的过滤器文本框。但搜索功能使用的id未显示在CGridView中。所以过滤不起作用。所以我想使用一个新的过滤函数,它将接受过滤字段。我尝试了下面的代码
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'purchase-grid',
'itemsCssClass'=>'table table-bordered table-condensed table-hover table-striped dataTable',
'filter'=>$model->filter_search(),
'dataProvider'=>$model->search(),
..............
但它显示出错误。请帮忙
提前感谢。过滤器参数应接收模型。如果不想显示筛选器,则特定列应执行以下操作:
array(
'name'=>'attributeName',
'value'=>'$data->attributeName',
'filter'=>false,
)
CGridView中的筛选器属性应为CActiveRecord 您可以像下面的代码一样更改过滤器输入
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'item-categoria-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'nombre',
array(
'id'=>'tipo_id',
'header'=>'Tipo',
'value'=>'$data->tipo->nombre',
'filter'=>CHtml::activeDropDownList(
$model,
"tipo_id",
CHtml::listData(TipoItem::model()->findAll(), 'id', 'nombre'),
array(
'empty'=>'(Seleccione uno)',
)),
),
'cuentaVenta.descripcion:html:Cuenta de Ventas',
CgridView自定义过滤器元素:
查看代码:
array(
'name'=>'status',
'header'=>'Confirmed',
'type'=>'raw',
'value'=>'($data->status==0 ? "No" : "Yes")',
'filter'=>CHtml::listData($pastEventModel->getYesNoCgridviewFilter(), 'id', 'title'),
),
型号代码:
public function getYesNoCgridviewFilter()
{
return array(
array('id'=>0, 'title'=>'No'),
array('id'=>1, 'title'=>'Yes'),
);
}
public function search($when)
{
$criteria=new CDbCriteria;
$criteria->compare('mandant_id',$this->mandant_id);
if($when == "past")
$criteria->addCondition("start < ". time(), 'AND');
elseif($when == "upcoming")
$criteria->addCondition("start >= ". time(), 'AND');
CgridView自定义搜索功能:
查看代码:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'events-grid2',
'dataProvider'=>$pastEventModel->search("past"),
'filter'=>$pastEventModel,
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'events-grid1',
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'events-grid2',
型号代码:
public function getYesNoCgridviewFilter()
{
return array(
array('id'=>0, 'title'=>'No'),
array('id'=>1, 'title'=>'Yes'),
);
}
public function search($when)
{
$criteria=new CDbCriteria;
$criteria->compare('mandant_id',$this->mandant_id);
if($when == "past")
$criteria->addCondition("start < ". time(), 'AND');
elseif($when == "upcoming")
$criteria->addCondition("start >= ". time(), 'AND');
控制器代码:
public function actionAdmin()
{
$pastEventModel=new Events('search');
$pastEventModel->unsetAttributes(); // clear any default values
if(isset($_GET['ajax'],$_GET['Events']) && $_GET['ajax']=="events-grid2")
{
$pastEventModel->attributes=$_GET['Events'];
unset($_GET['Events']);
}
$upComingEventModel=new Events('search');
$upComingEventModel->unsetAttributes(); // clear any default values
if(isset($_GET['ajax'],$_GET['Events']) && $_GET['ajax']=="events-grid1")
{
$upComingEventModel->attributes=$_GET['Events'];
unset($_GET['Events']);
}
$this->render('admin',array(
'pastEventModel'=>$pastEventModel,
'upComingEventModel'=>$upComingEventModel,
));
}
我已经使用HtmlOptions隐藏了该列,如下所示
array(name=>'project_id','headerHtmlOptions' => array('style' => 'display:none'),
'htmlOptions' => array('style' => 'display:none'),'filterHtmlOptions' => array('style' => 'display:none')),
然后我使用相同的$model->search()函数进行过滤。您的方法$model->filter\u search()返回什么?什么数据类型?