/** * @inheritdoc */ public function run() { $inputId = $this->options['id']; $hasModel = $this->hasModel(); if (array_key_exists('value', $this->options)) { $value = $this->options['value']; } elseif ($hasModel) { $value = Html::getAttributeValue($this->model, $this->attribute); } else { $value = $this->value; } $options = array_merge($this->options, ['multiple' => true, 'value' => $value]); if ($hasModel) { $output = Html::activeListBox($this->model, $this->attribute, $this->items, $options); } else { $output = Html::listBox($this->name, $this->value, $this->items, $options); } $clientOptions = array_merge(['filter' => $this->filter, 'multiple' => $this->multiple, 'multipleWidth' => $this->multipleWidth], $this->clientOptions); if (!array_key_exists('placeholder', $clientOptions) && array_key_exists('placeholder', $options)) { $clientOptions['placeholder'] = $options['placeholder']; } $js = 'jQuery(\'#' . $inputId . '\').multipleSelect(' . Json::htmlEncode($clientOptions) . ');'; if (Yii::$app->getRequest()->getIsAjax()) { $output .= Html::script($js); } else { $view = $this->getView(); MultipleSelectAsset::register($view); $view->registerJs($js); } return $output; }
/** * @inheritdoc * @throw NotSupportedException */ public function run() { if ($this->hasModel()) { if (!is_null($this->value)) { if (!in_array($this->attribute, $this->model->attributes())) { throw new NotSupportedException('Unable to set value of the property \'' . $this->attribute . '\'.'); } $stash = $this->model->{$this->attribute}; $this->model->{$this->attribute} = $this->value; } $output = Html::activeListBox($this->model, $this->attribute, $this->items, $this->options); if (isset($stash)) { $this->model->{$this->attribute} = $stash; } } else { $output = Html::listBox($this->name, $this->value, $this->items, $this->options); } $js = 'jQuery(\'#' . $this->options['id'] . '\').multipleSelect(' . Json::htmlEncode($this->clientOptions) . ');'; if (Yii::$app->getRequest()->getIsAjax()) { $output .= Html::script($js); } else { $view = $this->getView(); MultipleSelectAsset::register($view); $view->registerJs($js); } return $output; }