我正在尝试在此answer之后的CGridView的一列中添加一个带有select2扩展名的过滤器但不起作用。
我的代码:
在视图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',
),
)));
在vehiculos.php模型中
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(
),
));
}
显示错误CException:
Vehiculos and its behaviors do not have a method or closure named "widget".
答案 0 :(得分:1)
无需从控制器方法
获取小部件在gridview列中
array(
'name'=>'asociado_id',
'value'=>'Vehiculos::model()->getListNombreCompleto()',
'filter' => $this->widget('ext.select2.ESelect2',array(
'name'=>'asociado_id',
'data'=>$select2_options,
), true)
),
答案 1 :(得分:0)
我创建了一个扩展CDataColumn的类,为列添加过滤器:
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>';
}
}
然后将此列添加到cgridview:
array(
'class' => 'ext.widgets.TbTableDeviceType',
'model' => $model,
'fieldName' => 'deviceType_id',
'name' => 'deviceType_id',
),