/** * Run this widget. * This method registers necessary javascript and renders the needed HTML code. */ public function run() { $ret = '<span class="ui-autocomplete-span">'; // list($name,$id)=$this->resolveNameID(); // $name = Html::getAttributeName($this->attribute); $name = Html::getInputName($this->model, $this->attribute); $id = 'autocomplete-' . $this->attribute . '-' . self::$counter++; $defOptions = array('id' => $id, 'name' => $name, 'autocomplete' => 'off'); $defClientOptions = array('minLength' => 1, 'delay' => 500, 'autoFocus' => false); $this->options = array_merge($defOptions, $this->options); $this->clientOptions = array_merge($defClientOptions, $this->clientOptions); if ($this->keyValue === true) { $inputValue = isset($this->options['value']) ? $this->options['value'] : null; $value = Html::getAttributeValue($this->model, $this->attribute); $hiddenId = $this->options['id'] . '-value'; $ret .= Html::hiddenInput($name, $value, ['id' => $hiddenId]); if (empty($this->clientEvents['select'])) { $this->clientEvents['select'] = <<<Eof function(event,ui){ \t\$(this).parents(".ui-autocomplete-span").children("input[type='hidden']").val(ui.item.id?ui.item.id:ui.item.value); } Eof; } if (empty($this->clientEvents['change'])) { $this->clientEvents['change'] = <<<Eof function(event,ui){ \tif(ui.item === null) \t\t\$(this).parents(".ui-autocomplete-span").children("input[type='hidden']").val(''); } Eof; } $name2 = 'query[' . $this->model->formName() . '][' . $this->attribute . ']'; $ret .= Html::textInput($name2, $inputValue, $this->options); } else { if ($this->hasModel()) { // \THtml::dump($this->options); $ret .= Html::activeTextInput($this->model, $this->attribute, $this->options); } else { $ret .= Html::textInput($name, $this->value, $this->options); } } if ($this->readOnly === TRUE) { $ret .= '</span>'; return $ret; } if ($this->sourceUrl !== null) { $url = $this->sourceUrl; $field = isset($this->clientOptions['searchField']) ? $this->clientOptions['searchField'] : $this->attribute; if (is_string($url)) { $url = array($url, 'field' => $field); } elseif (is_array($url)) { $url['field'] = $field; } $this->clientOptions['source'] = Url::to($url); } else { $this->clientOptions['source'] = $this->source; } $this->clientOptions['isOpened'] = false; if (empty($this->clientEvents['open'])) { $this->clientEvents['open'] = <<<Eof function(event,ui){ \t\$(this).autocomplete( "option", "isOpened",true ); } Eof; } if (empty($this->clientEvents['close'])) { $this->clientEvents['close'] = <<<Eof function(event,ui){ \t\$(this).autocomplete( "option", "isOpened",false ); } Eof; } if ($this->showDropdown === true) { $ret .= '<span class="dropdown"><i class="iconfont small-btn"></i></span>'; $js = <<<Eof \$(".ui-autocomplete-span .dropdown").on('click',function(event){ \tvar obj=\$(this).prev(); \tvar isOpened = obj.autocomplete( "option", "isOpened" ); \tif( isOpened ){ \t\tobj.autocomplete( "close"); \t}else{ \t\tif( obj.attr("readonly")==="readonly" )return; \t\tvar minLength = obj.autocomplete( "option", "minLength" ); \t\tobj.autocomplete( "option", "minLength", 0 ); \t\tobj.autocomplete("search",""); \t\tobj.autocomplete( "option", "minLength", minLength ); \t} }); Eof; $this->getView()->registerJs($js); // Yii::app()->getClientScript()->registerScript(__CLASS__,$js); } $ret .= '</span>'; // $clientOptions= Json::encode($this->clientOptions); // $clientOptions=CJavaScript::encode($this->clientOptions); // $this->registerJs(__CLASS__.'#'.$id,"jQuery('#{$id}').autocomplete($clientOptions);"); $this->registerWidget('autocomplete'); return $ret; }