Cgridview + Custom Dropdown (Select2)

Hi,

I’m trying without success to insert a custom dropdown (Select2) in Cgridview.

My grid:




<?php $this->widget('bootstrap.widgets.TbExtendedGridView', array(

        'id' => 'usuario-grid',

        'dataProvider' => $model->search(),

        'filter' => $model,

        'columns' => array(

                'nome',

                'login',

                'email',

                'inclusao',

                //SUBSTITUIR PELO SELECT2

                array(

                                'header'=>'Tipo',

                                'name'=>'grupo_id',

                                'value'=>'GxHtml::valueEx($data->grupo)',

                                'filter'=>GxHtml::listDataEx(UsuarioGrupo::model()->findAllAttributes(null, true)),

                                ),                              

                array(

                        'class' => 'bootstrap.widgets.TbButtonColumn',

                ),

        ),

)); ?>



Now the Select2 example:




$this->widget('ext.select2.ESelect2',array(

  'name'=>'asik2x',

  'data'=>$data,

  'options'=>array(

    'placeholder'=>'Keren ya?',

    'allowClear'=>true,

  ),

));



How to join?

Thanks

Try to put it in ‘value’ parameter! between double quotation :wink:

I got an CException "UsuarioController.widget" is not defined…

I had the same problem but with cgridView and it’s easy to solve,

1-Use the natural select 2 http://ivaynberg.github.io/select2/index.html#lifecycle

2-Don’t assign the values to the key instead use a class.

3-Assign tags value


$("#e15").select2({tags:["red", "green", "blue", "orange", "white", "black", "purple", "cyan", "teal"]});

on afterAjaxUpdate property

you can write a custome query on filter options

for e.g


 'filter'=>GxHtml::listDataEx(User::model()->findAll('user_type=\'venue\''), 'id', 'fullname'),

also check your model relation…

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[''] = ''; // Add empty value to select 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',

    ),