2015-12-01 35 views
1

我正在使用Yii 1.1.16,並試圖使用CGridView來顯示搜索結果列表。我需要它能夠通過這些過濾。就像默認的actionAdmin函數一樣。默認CGridView使用findAll的結果

這裏是我的控制器代碼:

public function actionajaxResearchList() 
    { 
     if(Yii::app()->request->isAjaxRequest) 
     { 
      $year = $_POST['year']; 
      $criteria = new CDbCriteria(); 
      $criteria->addCondition("year=:year"); 
      $criteria->params = array(':year' => $year,); 
      $query = Abc::model()->findAll($criteria); 

      if ($query===null) 
       throw new CHttpException(404, 'The requested page does not exist.'); 
      $model=new Abc('search'); 
      $model->unsetAttributes(); 
      if(isset($_GET['Abc'])) 
       $model->attributes=$_GET['Abc']; 

      $this->renderPartial('_view',array(
       'model'=>$model, 
       'query'=>$query 
      )); 

      Yii::app()->end(); 
    } 
} 

在我_view.php我有這個

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'abc-grid', 
    'dataProvider'=>$model->search(), 
    'filter'=>$model, 
    'columns'=>array(
     array(  
       'name'=>'photo', 
       'value'=> '$data->photo', 
       'type'=>'raw', 
       'filter'=>false, //remove filter search for photo's 
     ), 
     'id', 
     'year', 
     array(
      'class'=>'CButtonColumn', 
     ), 
    ), 
)); ?> 

我如何得到它顯示我$query結果?我應該嘗試使用過濾器功能,還是有更簡單的方法?

+1

在'search()'模型函數中添加您的查詢。 – Criesto

+0

@Criesto是否這樣? 'if(empty($ _ GET ['year']))$ criteria-> compare('year',$ this-> year,true); \t \t else $ criteria-> compare('year',$ _ GET ['year'],true);'但cgridview上的過濾器不起作用?我使用ajax加載整個cgridview表。 – user2636556

回答

0

這裏是一個粗略的代碼,它應該工作,我沒有測試過,但:

控制器動作:

public function actionajaxResearchList() 
    { 
     if(Yii::app()->request->isAjaxRequest) 
     { 
      if(isset($_POST['year']) && $_POST['year'] != "") { 
       $year = $_POST['year']; //sanitize and validate this 
      } else { 
       $year = ""; //sanitize and validate this 
      } 
      $model=new Abc('search'); 
      $model->unsetAttributes(); 
      if(isset($_GET['Abc'])) 
       $model->attributes=$_GET['Abc']; 

      $this->renderPartial('_view',array(
       'model'=>$model, 
       'year'=>$year // passing the year 
      )); 

      Yii::app()->end(); 
    } 
} 

_view.php:

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'abc-grid', 
    'dataProvider'=>$model->search($year),// year from controller 
    'filter'=>$model, 
    'columns'=>array(
     array(  
       'name'=>'photo', 
       'value'=> '$data->photo', 
       'type'=>'raw', 
       'filter'=>false, //remove filter search for photo's 
     ), 
     'id', 
     'year', 
     array(
      'class'=>'CButtonColumn', 
     ), 
    ), 
)); ?> 

模型搜索:

public function search($year) 
    { 
     $criteria=new CDbCriteria; 
     ... 

     if($year != "") { 
      $criteria->compare('year',$year); 
      //$criteria->addSearchCondition('year',$year); 
     } else { 
      $criteria->compare('year',$this->year); 
     } 

     ... 

     return new CActiveDataProvider($this, array(
      'criteria'=>$criteria, 
     )); 
    }