diff --git a/README.md b/README.md new file mode 100644 index 0000000..7849921 --- /dev/null +++ b/README.md @@ -0,0 +1,85 @@ +Yii2 Select2 Extension +====================== +Yii2 Select2 Extension + +Installation +------------ + +The preferred way to install this extension is through [composer](http://getcomposer.org/download/). + +Either run + +``` +composer require --prefer-dist mhunesi/yii2-select2 "*" +``` + +or add + +``` +"mhunesi/yii2-select2": "*" +``` + +to the require section of your `composer.json` file. + + +Usage +----- + +Once the extension is installed, simply use it in your code by : + +``` php + 'my-id', + 'name' => 'my-name', + 'items' => ['1' => 'Deneme'] +]) ?> +``` + +``` php +field($model, 'address') + ->widget(\mhunesi\select2\Select2Widget::className(), + [ + 'items' => [1 => 'Address Title 1', 2 => 'Address Title 2'], + 'options' => [ + 'options' => [ + 1 => [ + 'data-title' => 'Address Title 1', + 'data-name' => 'Mustafa Hayri', + 'data-lastname' => 'ÜNEŞİ', + 'data-country' => 'Country', + 'data-province' => 'Province', + 'data-phone' => '+90 542 999 99 99', + 'data-address' => '214 West 36th Street', + ], + 2 => [ + 'data-title' => 'Address Title 2', + 'data-name' => 'Mustafa Hayri', + 'data-lastname' => 'ÜNEŞİ', + 'data-country' => 'Country', + 'data-province' => 'Province', + 'data-phone' => '+90 542 999 99 99', + 'data-address' => '214 West 36th Street', + ] + ] + ], + 'clientOptions' => [ + 'templateResult' => new JsExpression(' + function(item){ + if (!item.id) { + return item.text; + } + var data = $(item.element).data(); + var template = $( + `${data.title}
+ ${data.address} - ${data.province} / ${data.country}
+ ${data.name} ${data.lastname} ${data.phone}` + ); + return template; + } + ') + ] + ] + ) ?> +``` + diff --git a/Select2Asset.php b/Select2Asset.php new file mode 100644 index 0000000..16371d8 --- /dev/null +++ b/Select2Asset.php @@ -0,0 +1,19 @@ +initPlaceholder(); + } + + /** + * Select2 plugin placeholder check and initialization + */ + protected function initPlaceholder() + { + $multipleSelection = ArrayHelper::getValue($this->options, 'multiple'); + + if (!empty($this->options['prompt']) && empty($this->clientOptions['placeholder'])) { + $this->clientOptions['placeholder'] = $multipleSelection + ? ArrayHelper::remove($this->options, 'prompt') + : $this->options['prompt']; + + return null; + } elseif (!empty($this->options['placeholder'])) { + $this->clientOptions['placeholder'] = ArrayHelper::remove($this->options, 'placeholder'); + } + if (!empty($this->clientOptions['placeholder']) && !$multipleSelection) { + $this->options['prompt'] = is_string($this->clientOptions['placeholder']) + ? $this->clientOptions['placeholder'] + : ArrayHelper::getValue((array)$this->clientOptions['placeholder'], 'placeholder', ''); + } + } + + /** + * @inheritdoc + */ + public function run() + { + if ($this->hasModel()) { + echo Html::activeDropDownList($this->model, $this->attribute, $this->items, $this->options); + } else { + echo Html::dropDownList($this->name, $this->value, $this->items, $this->options); + } + $this->registerClientScript(); + } + + /** + * @inheritdoc + */ + public function registerClientScript() + { + $view = $this->getView(); + + $this->registerBundle($view); + + $options = !empty($this->clientOptions) + ? Json::encode($this->clientOptions) + : ''; + + $id = $this->options['id']; + + $js[] = ";jQuery('#$id').select2($options);"; + if (!empty($this->clientEvents)) { + foreach ($this->clientEvents as $event => $handler) { + $js[] = "jQuery('#$id').on('$event', $handler);"; + } + } + + $view->registerJs(implode("\n", $js)); + } + + /** + * Registers asset bundle + * + * @param View $view + */ + protected function registerBundle(View $view) + { + Select2Asset::register($view); + } +} diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..f93874b --- /dev/null +++ b/composer.json @@ -0,0 +1,22 @@ +{ + "name": "mhunesi/yii2-select2", + "description": "Yii2 Select2 Extension", + "type": "yii2-extension", + "keywords": ["yii2","extension","select2"], + "license": "MIT", + "authors": [ + { + "name": "Mustafa Hayri ÜNEŞİ", + "email": "mhunesi@gmail.com" + } + ], + "require": { + "yiisoft/yii2": "~2.0.0", + "bower-asset/select2": "~4.0" + }, + "autoload": { + "psr-4": { + "mhunesi\\select2\\": "" + } + } +}