How to add select2 extension as filter in CGridVie

2019-08-02 21:14发布

I'm trying to add in one column of the CGridView a filter with select2 extension following this answer but does not work.

My code:

In view vehiculos/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',
    ),
)));

In vehiculos.php Model

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

Display Error CException:

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

2条回答
何必那么认真
2楼-- · 2019-08-02 21:52

No need to fetch the widget from controller method

In the gridview column

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

        ),
查看更多
叼着烟拽天下
3楼-- · 2019-08-02 21:56

I have created a class extending the CDataColumn to add a filter to the column:

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>';
    }
}

And then you add this column to your cgridview:

array(
    'class' => 'ext.widgets.TbTableDeviceType',
    'model' => $model,
    'fieldName' => 'deviceType_id',
    'name' => 'deviceType_id',
),
查看更多
登录 后发表回答