2014-02-25 40 views
0

我正在使用CGridView顯示來自postgres函數的結果。 CGridView工作正常。現在我想在模型中使用不同的函數來過濾CGridView。在yii中爲Cgridview定製過濾

爲CGridView當前碼是像

$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(), 
     .............. 

的$模型 - >搜索()函數接受它用來從表中選擇的行的ID。我正在使用CSqlDataProvider在$ model-> search()中運行自定義查詢並返回數據提供者。如果我使用上面的代碼,它將顯示CGridView中所有字段的過濾器文本框。但是搜索功能正在使用一個沒有顯示在CGridView中的ID。所以過濾不起作用。所以我想使用一個新的函數進行過濾,它將接受過濾的字段。我試着用下面的代碼

$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(), 
     .............. 

但它顯示錯誤。請幫忙。

在此先感謝。

+0

你的方法$ model-> filter_search()返回什麼?什麼數據類型? – gSorry

回答

0

我以HtmlOptions如下面給出的隱藏的列。

array(name=>'project_id','headerHtmlOptions' => array('style' => 'display:none'), 
    'htmlOptions' => array('style' => 'display:none'),'filterHtmlOptions' => array('style' => 'display:none')), 

然後我使用相同的$ model-> search()函數進行過濾。

0

過濾器參數應該接收模型。如果你不想顯示過濾特定的列應該做的:在CGridView

array(
'name'=>'attributeName', 
'value'=>'$data->attributeName', 
'filter'=>false, 
) 
0

過濾器屬性應該是一個的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', 

注:過濾器是的CActiveRecord,並在列的篩選是一個選擇

0

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'), 
    ); 
} 

CgridView自定義搜索功能:

查看代碼:

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'events-grid2', 
    'dataProvider'=>$pastEventModel->search("past"), 
    'filter'=>$pastEventModel, 

型號代碼:

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-grid1', 

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'events-grid2', 

控制器代碼:

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, 
    )); 
}