/** * 自定义下拉列表 * @param type $data * @param type $htmlOptions * @param type $selectValue * @return type */ public function select($data, $htmlOptions = [], $selectValue = null) { if (isset($htmlOptions['label']) && !isset($this->parts['{label}'])) { $this->parts['{label}'] = $htmlOptions['label']; if (!empty($htmlOptions['labelOptions'])) { $this->labelOptions = $htmlOptions['labelOptions']; } } unset($htmlOptions['labelOptions']); $htmlOptions['label'] = null; $model = $this->model; $attribute = $this->attribute; // self::$count++; $selectionText = Yii::t('sys', 'unset'); if (isset($htmlOptions['empty']) && empty($htmlOptions['empty'])) { $htmlOptions['empty'] = $selectionText; } if (empty($htmlOptions['name'])) { $htmlOptions['name'] = Html::getInputName($this->model, $this->attribute); } if (empty($htmlOptions['id'])) { $htmlOptions['id'] = 'select-' . $this->attribute; } $selection = ''; if (isset($selectValue)) { $selection = $selectValue; } else { $selection = ArrayHelper::getValue($model, $attribute); } $content = "\n" . $this->listSelections($selection, $data, $htmlOptions); if (isset($htmlOptions['multiple'])) { if (substr($htmlOptions['name'], -2) !== '[]') { $htmlOptions['name'] .= '[]'; } } if (!is_array($selection) && isset($selection) && isset($data[$selection])) { $selectionText = $data[$selection]; } $content = '<span>' . $selectionText . '</span>' . Html::tag('ul', $content, array('class' => 'hide')); if (!isset($htmlOptions['readonly'])) { $content .= '<span class="dropdown"><i class="iconfont small-btn"></i></span>'; } // if( empty($htmlOptions['class']))$htmlOptions['class']='select'; $htmlOptions['class'] = empty($htmlOptions['class']) ? 'select' : 'select ' . $htmlOptions['class']; $this->parts['{input}'] = Html::tag('div', $content, $htmlOptions); $this->adjustLabelFor($htmlOptions); return $this; }
/** * 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; }