1

我试图在 CGridView 的一列中添加一个带有 select2 扩展名的过滤器,不起作用。

我的代码:

在查看vehculos/admin.php

<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'vehiculos-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
    'id',
    'placa',
    array(
        'name'=>'asociado_id',
        'value'=>'Vehiculos::model()->getListNombreCompleto()',
        'type'=>'html',

        ),
    'modelo',
    'color',
    array(

        'class'=>'CButtonColumn',
    ),
)));

在 vehiculos.php 模型中

public function getListNombreCompleto()
    {
        $nombreCompleto = Contactos::model()->findAll();
        $data = array();
        foreach ($nombreCompleto as $contacto) {
            $data[$contacto->id] = $contacto->nombre;
        }
         $this->widget('ext.select2.ESelect2',array(
          'name'=>'asociado_id',
          'data'=>$data,
          'htmlOptions'=>array(
          ),
        ));
    }

显示错误 CException:

Vehiculos and its behaviors do not have a method or closure named "widget".
4

2 回答 2

1

无需从控制器方法中获取小部件

在gridview列

array(
        'name'=>'asociado_id',
        'value'=>'Vehiculos::model()->getListNombreCompleto()',
        'filter' => $this->widget('ext.select2.ESelect2',array(
          'name'=>'asociado_id',
          'data'=>$select2_options,
        ), true)

        ),
于 2013-11-02T17:05:14.667 回答
0

我创建了一个扩展 CDataColumn 的类以向列添加过滤器:

Yii::import('zii.widgets.grid.CDataColumn');

class TbTableDeviceType extends CDataColumn {
    public $model;
    public $fieldName;

    public function init() {
        $ajaxUpdate = $this->grid->afterAjaxUpdate;
        $this->grid->afterAjaxUpdate = "function(id,data){'.$ajaxUpdate.' 
                $('#" . get_class($this->model) . "_" . $this->fieldName .     "').select2({placeholder:' ', allowClear: true});
        }";
    }

    /**
     * Renders the filter cell.
     */
    public function renderFilterCell() {
        echo '<td><div class="filter-container">';
        $deviceTypes = Helper::getDeviceTypesArray();
        $deviceTypes[''] = ''; // Translate::t('globals', '-- all --');
        asort($deviceTypes);
        $this->filter = $deviceTypes;
        $model = $this->model;
        $field = $this->fieldName;
        if (empty($model->$field))
            echo CHtml::dropDownList(get_class($this->model) . '[' . $this-    >fieldName . ']', $this->fieldName, $deviceTypes);
        else
            echo CHtml::dropDownList(get_class($this->model) . '[' . $this->fieldName . ']', $this->fieldName, $deviceTypes, array(
                'options' => array(
                    $model->$field => array(
                        'selected' => true
                    )
                )
            ));
        Yii::app()->controller->widget('ext.ESelect2.ESelect2', array(
            'selector' => '#' . get_class($this->model) . '_' . $this-    >fieldName,
            'data' => $deviceTypes,
            'options' => array(
                'placeholder' => ' ',
                'allowClear' => true
            ),
            'htmlOptions' => array(
                'minimumInputLength' => 2,
                'style' => 'width:100%'
            )
        ));
        echo '</div></td>';
    }
}

然后将此列添加到 cgridview:

array(
    'class' => 'ext.widgets.TbTableDeviceType',
    'model' => $model,
    'fieldName' => 'deviceType_id',
    'name' => 'deviceType_id',
),
于 2015-02-11T06:58:21.153 回答