75034

How to add select2 extension as filter in CGridView on Yii

Question:

I'm trying to add in one column of the CGridView a filter with select2 extension following this <a href="https://stackoverflow.com/questions/19209112/how-to-add-select2-extension-column-in-yii-gridview" rel="nofollow"><em>answer</em></a> but does not work.

My code:

<strong>In view vehiculos/admin.php</strong>

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

<strong>In vehiculos.php Model</strong>

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

<strong>Display Error CException:</strong>

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

Answer1:

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

Answer2:

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

Recommend

  • 'HttpPostedFileBase' has no key defined. Define the key for this EntityType
  • connecting api controller with service layer
  • Get a list of checked models in controller [duplicate]
  • Navigate properties when using Dapper Extenstions
  • viewmodel return null on HttpPost
  • How to add select2 extension as filter in CGridView on Yii
  • How to create database and table in sqlite programatically using monotouch?
  • Simple tags system in Laravel 5.2
  • Null parameter when trying to pass a blob to C# controller
  • Implement OnClickListener on FastAdapter implementing IItem
  • How to update translation cakephp but not main table
  • Implementing multi selection in Android RecyclerView
  • How to fix android Multiple dex files define exception
  • Orchard Content Type is null
  • Highlight and Bold text in JTextPane
  • Algorithm for a smudge tool?
  • How reduce the height of an mschart by breaking up the y-axis
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • Akka Routing: Reply's send to router ends up as dead letters
  • Cannot Parse HTML Data Using Android / JSOUP
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • unknown Exception android
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?