How to add select2 extension as filter in CGridView on Yii

大兔子大兔子 提交于 2019-12-11 06:52:31

问题


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

回答1:


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)

        ),



回答2:


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


来源:https://stackoverflow.com/questions/19743859/how-to-add-select2-extension-as-filter-in-cgridview-on-yii

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!