Tuesday, October 8, 2013

Separate key=>value combobox The original jQuery combobox allows for separate key=>value pairs. In order to add this functionality to the combobox I added a public property $assoc and the run() method has to be changed as follows: public function run() { list($name, $id) = $this->resolveNameID(); if (is_array($this->data) && !empty($this->data)){ //if $data is not an assoc array make each value its key if($this->assoc){ $data=$this->data; } else{ $data=array_combine($this->data, $this->data); } //does the same as array_unshift($data,null) but does not break assoc arrays $data=array(""=>"")+$data; } else $data = array(); if ($this->hasModel()) echo CHtml::activeDropDownList($this->model,$this->attribute,$data); else echo CHtml::dropDownList($name, $this->value, $data); echo CHtml::textField(null, ($this->hasModel()? $data[CHtml::resolveValue($this->model,$this->attribute)]: $data[$this->value] ),array('id'=>$id.'_combobox') ); $this->options = array_merge($this->defaultOptions, $this->options); $options = CJavaScript::encode($this->options); $cs = Yii::app()->getClientScript(); $js = "combobox({$options});"; list($id, $js) = $this->setSelector($id.'_combobox', $js); $cs->registerScript(__CLASS__ . '#' . $id, $js); } Full code on GitHub IMPORTANT the combobox input field has "_combobox" appended to its id. The select now has the original name and id of the field. Shout out to @_construct() for the bug fix. An example of the usage is below $this->widget('ext.combobox.EJuiComboBox', array( 'model' => $myModel, 'attribute' => 'myAttribute', // data to populate the select. Must be an array. 'data' => array('Y'=>'Yii','R'=>'Rocks') 'assoc'=>true, .... )); #10214 report it 0 0 leocharrua at 2012/10/11 01:23pm Demo link doesn't work Thanks.