【问题标题】:Custom Filtering For Cgridview in yiiyii中Cgridview的自定义过滤
【发布时间】:2014-02-25 12:25:54
【问题描述】:

我正在使用 CGridView 来显示 postgres 函数的结果。 CGridView 工作正常。现在我想在模型中使用不同的函数来过滤 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(),
        ..............

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

但它显示错误。请帮忙。

提前致谢。

【问题讨论】:

  • 你的方法 $model->filter_search() 返回什么?什么数据类型?

标签: yii


【解决方案1】:

过滤器参数应该接收模型。如果您不想显示特定列应该执行的过滤器:

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

【讨论】:

    【解决方案2】:

    CGridView 中的 filter 属性应该是 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,列中的过滤器是一个Select

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        我使用 HtmlOptions 隐藏了该列,如下所示。

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

        然后我使用相同的 $model->search() 函数进行过滤。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-04
          • 1970-01-01
          • 2014-03-22
          • 2011-02-23
          相关资源
          最近更新 更多