3

我正在为我的网络应用程序使用 yii。在我的一个观点中,我有 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}",
)); ?>
4

2 回答 2

13

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

模型:

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”,因此您必须添加“public $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'];
于 2012-08-01T11:43:52.473 回答
0

这将设置选择列表

$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',
    ),
),

));

于 2015-06-11T07:46:24.990 回答