警予CGridView滤波器关系(yii CGridView filter with relatio

2019-09-20 14:24发布

我用警予我的Web应用程序。 在我看来,一个我有CGridView和dataProvider是邮件的模式。 在这个模型中我有与其他3种机型的关系。 在网格我显示了三种型号的cols。 我怎样才能筛选CGridView?

更新:

<?php $dialog = $this->widget('ext.ecolumns.EColumnsDialog', array(
           'options'=>array(
                'title' => 'Layout settings',
                'autoOpen' => false,
                'show' =>  'fade',
                'hide' =>  'fade',
            ),
           'htmlOptions' => array('style' => 'display: none'), //disable flush of dialog content
           'ecolumns' => array(
                'gridId' => 'mails-grid', //id of related grid
                'storage' => 'session',  //where to store settings: 'db', 'session', 'cookie'
                'fixedLeft' => array('CCheckBoxColumn'), //fix checkbox to the left side 
                'model' => $dataprovider, //model is used to get attribute labels
                'columns'=>array(
                                array(
                                    'name'=>'mailTemplate.name',
                                    'filter'=>CHtml::activeTextField($dataprovider, 'mailTemplate'),
                                ),
                                'sendDate',
                                array(
                                        'name'=>'mailTemplate.subject',
                                        'filter'=>CHtml::activeTextField($dataprovider, 'mailTemplate'),
                            ),
                            array(
                                'name'=>'client.email',
                                'filter'=>CHtml::activeTextField($dataprovider, 'client'),
                            ),
                            array(
                                'name'=>'client.name',
                                'filter'=>CHtml::activeTextField($dataprovider, 'client'),
                            ),
                            array(
                                'name'=>'operator.username',
                                'filter'=>CHtml::activeTextField($dataprovider, 'operator'),
                            ),
                            array(
                                'name'=>'status',
                                'value'=>array('MailHelper', 'getEmailStatus'),
                                'filter'=> CHtml::activeDropDownList($dataprovider, 'status', Mail::getEmailStatuses()),
                            ),
                            array(
                                'class'=>'CButtonColumn',
                                'template'=>'{update}',
                                'buttons'=>array(
                                                'update' => array(
                                                        'url'=>'$this->grid->controller->createUrl("/email/editTemplate", array("templateId"=>$data->id))',
                                                ),
                                            ),
                            )
                        ),
        )
    ));

?>

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'mails-grid',
    'dataProvider'=>$dataprovider->search(),
    'columns' => $dialog->columns(),
    'filter' => $dataprovider,
    'template' => $dialog->link()."{summary}\n{items}\n{pager}",
)); ?>

Answer 1:

我有餐厅,城市,国家和用户模型与它们之间的关系。

模型:

public function search() {
  $criteria=new CDbCriteria;
  $criteria->together = true;
  $criteria->with= array('xCountry','xCity','User');
  $criteria->compare('Id',$this->Id,true);
  $criteria->compare('Restaurant.Name',$this->Name,true);
  $criteria->addSearchCondition('xCountry.Name',$this->Country);
  $criteria->addSearchCondition('xCity.Name',$this->City);
  $criteria->compare('Zip',$this->Zip,true);
  $criteria->compare('Address',$this->Address,true);
  $criteria->compare('Description',$this->Description,true);
  $criteria->compare('Restaurant.Active',$this->Active,true);
  $criteria->addSearchCondition('User.Username',$this->Owner);
  $criteria->compare('Lat',$this->Lat);
  $criteria->compare('Lon',$this->Lon);

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

视图:

$this->widget('zii.widgets.grid.CGridView', array(
      'id'=>'restaurant-grid',
      'dataProvider'=>$model->search(),
      'filter'=>$model,
      'columns'=>array(
        'Id',
        'Name',
        'Zip',
        'Address',
        'Active',
        array(
          'name' => 'User.Username',
          'header' => 'Username',
          'filter' => CHtml::activeTextField($model, 'Owner'),
          'value' => '$data->User->Username',
            ),
        array(
          'name' => 'xCountry.Name',
          'header' => 'Country',
          'filter' => CHtml::activeTextField($model, 'Country'),
          'value' => '$data->xCountry->Name',
            ),
        array(
          'name' => 'xCity.Name',
          'header' => 'City',
          'filter' => CHtml::activeTextField($model, 'City'),
          'value' => '$data->xCity->Name',
            ),
        array(
        'class'=>'CButtonColumn',
        ),
      ),
    ));

我希望这可以帮助你。

更新:

如果你尝试这样的事:

...
'columns'=>array(
  'mailTemplate.name',
  'sendDate',
  'mailTemplate.subject',
  'client.email',
  ...

更新#2:

做好准备,这将是一个有点脏。

比方说,我们已经有了两个类,A和B. B属于A,B的有一个特性,让我们说“色”,我们希望在我们的电网,以显示它在这里我们列出了“A”秒。

你必须做的第一件事是,手动创建一个属性到您的数据提供程序类(什么是“A”)。 此属性将是“colorOfB”,所以你必须要加上“公共$ colorOfB;” 到你的模型A.

添加标准,该属性:

$criteria->compare('B.color',$this->colorOfB,true);

列添加到网格:

array(
  'name' => 'B.color',
  'header' => 'Color of B',
  'filter' => CHtml::activeTextField($model, 'colorOfB'),
  'value' => '$data->B->color'),

的最后一件事是在A的控制器手动设置该属性:

$modelA = new A('search');
$modelA->colorOfB = $_GET['A']['colorOfB'];


Answer 2:

这将设置选择列表

$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'avto-ugon-grid',
'dataProvider'=>$model_data,
'filter'=>$model,
'columns'=>array(
    'id',
    array(
        'name' => 'time',
        'value' => 'date("d/m/Y", $data->time)',
        'type' => 'html',
    ),
    array(
        'name' => 'nomer',
        'value' => '$data->nomer',
        'type' => 'html',
    ),
    array(
        'name' => 'id_marka',
        'value' => '$data->idMarka->mark',
        'type' => 'html',
        'filter'=> CHtml::listData(AvtoUgon::model()->with('idMarka')->findAll(array('group'=> 'id_marka', 'order'=> 'idMarka.mark')), 'id_marka', 'idMarka.mark'),
    ),
    array(
        'name' => 'id_model',
        'value' => '$data->idModel->model',
        'type' => 'html',
        'filter'=> CHtml::listData(AvtoUgon::model()->with('idModel')->findAll(array('group'=> 'id_model', 'order'=> 'idModel.model')), 'id_model', 'idModel.model'),
    ),
    array(
        'name' => 'color',
        'value' => $data->color,
        'type' => 'raw',
    ),

    array(
        'name' => 'id_street',
        'value' => '$data->idStreet->street',
        'type' => 'html',
    ),
    array(
        'name' => 'publish',
        'value' => 'CHtml::link($data->publish ? "Опубликовано" : "Не опубликовано", Yii::app()->controller->createUrl("publish", array("id" => $data->id)))',
        'type' => 'html',
    ),
    /*'id_street',
    'nomer',
    */
    array(
        'class'=>'CButtonColumn',
    ),
),

));



文章来源: yii CGridView filter with relations