Sorry, maybe I should have described my problem a little bit more detailed. IMHO the difference between a combo box and a dropdown list is that former one offers the additional possibility to type in a textfield to select between different entries from the included dropdown list. This is especially usefull for long lists with many entries where scrolling takes longer than typing parts of the desired entry. That’s the reason why I’m looking for either AutoComplete (with or without combobox) or a standard combobox (which AFAIK doesn’t exist in pure html).
I know how to get a simple dropdown list in CGridView filter bar:
'filter' => CHtml::listData(SomeModel::model()->findAll(), 'id', 'someField'),
But my problem starts if I want to use CJuiAutoComplete instead. The Class Reference suggests that it is possible to specify HTML code for the filter property. I just don’t know how to retrieve the HTML code for CJuiAutoComplete to use it as filter property value. This is my working code for CJuiAutoComplete outside CGridView:
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name'=>'someField-ac',
'value'=>'',
'model'=>$model->someRelation,
'attribute'=>'someField',
'source'=>$this->createUrl('someModelController/autoCompleteId'),
// additional javascript options for the autocomplete plugin
'options'=>array(
'showAnim'=>'fold',
'select'=>"js:function(event, ui) {
$('#OtherModels_someField_id').val(ui.item.id)
}",
),
));
someModelController/autoCompleteId is retrieving the JSON data for AutoComplete plugin:
/**
* Performs AJAX AutoComplete request
* returns array including IDs
*/
public function actionAutoCompleteId()
{
$res =array();
$arr =array();
if (isset($_GET['term'])) {
// http://www.yiiframework.com/doc/guide/database.dao
$condition ="SELECT id, someField FROM {{someModel}} WHERE someField LIKE :someField";
$params = array(":someField"=>'%'.$_GET['term'].'%');
$rows = SomeModel::model()->findAllBySql($condition,$params);
foreach ($rows as $row)
{
$arr[] = array(
'label'=>$row->someField, // label for dropdown list
'value'=>$row->someField, // value for input field
'id'=>$row->id, // return value from autocomplete
);
};
}
echo CJSON::encode($arr);
Yii::app()->end();
}
I was looking around for quite a while before posting but couldn’t find what I was looking for, just code for simple dropdown list. As I’m just doing my first steps with yii, maybe it is quite easy to achieve what I’m looking for. Would be very nice if somebody could help me!
Best regards, mubo
PS: Another question would be how to get the ComboBox option of jquery autocomplete implemented, but I would be first satisfied with just the standard CJuiAutoComplete replacing textfield or dropdown list in CGridView filter bar respectively.
EDIT: PPS: If it makes any difference the solution could also use ejui-autocomplete-fk-field extension.