简体   繁体   中英

How to add select2 extension as filter in CGridView on Yii

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".

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)

        ),

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM