├── .php_cs ├── ChosenSelect.php ├── ChosenSelectAsset.php ├── LICENSE.md ├── README.md └── composer.json /.php_cs: -------------------------------------------------------------------------------- 1 | exclude('vendor') 5 | ->in([__DIR__]); 6 | 7 | $config = PhpCsFixer\Config::create() 8 | ->setUsingCache(false) 9 | ->setRules([ 10 | '@Symfony' => true, 11 | 'phpdoc_align' => false, 12 | 'phpdoc_summary' => false, 13 | 'phpdoc_inline_tag' => false, 14 | 'pre_increment' => false, 15 | 'heredoc_to_nowdoc' => false, 16 | 'cast_spaces' => false, 17 | 'include' => false, 18 | 'phpdoc_no_package' => false, 19 | 'concat_space' => ['spacing' => 'one'], 20 | 'ordered_imports' => true, 21 | 'array_syntax' => ['syntax' => 'short'], 22 | ]) 23 | ->setFinder($finder); 24 | 25 | return $config; 26 | -------------------------------------------------------------------------------- /ChosenSelect.php: -------------------------------------------------------------------------------- 1 | 13 | * 14 | * @since 1.0 15 | */ 16 | class ChosenSelect extends InputWidget 17 | { 18 | /** 19 | * @var array select items 20 | */ 21 | public $items = []; 22 | 23 | /** 24 | * @var array plugin options. By default it contains ['width' => '100%'] 25 | */ 26 | public $pluginOptions = []; 27 | 28 | /** 29 | * @var bool Enable placeholder in Chosen plugin 30 | */ 31 | public $withPlaceHolder = true; 32 | 33 | /** 34 | * @inheritdoc 35 | */ 36 | public function init() 37 | { 38 | parent::init(); 39 | 40 | if ($this->withPlaceHolder) { 41 | if (!empty($this->options['prompt'])) { 42 | $this->options['data-placeholder'] = $this->options['prompt']; 43 | unset($this->options['prompt']); 44 | } 45 | if (empty($this->options['data-placeholder'])) { 46 | $this->options['data-placeholder'] = 'Please select...'; 47 | } 48 | $this->items = ['' => ''] + $this->items; 49 | } 50 | 51 | if (empty($this->options['multiple'])) { 52 | $this->options['multiple'] = false; 53 | } 54 | 55 | if (empty($this->pluginOptions['width'])) { 56 | $this->pluginOptions['width'] = '100%'; 57 | } 58 | } 59 | 60 | /** 61 | * @inheritdoc 62 | */ 63 | public function run() 64 | { 65 | if ($this->hasModel()) { 66 | echo Html::activeDropDownList($this->model, $this->attribute, $this->items, $this->options); 67 | } else { 68 | echo Html::dropDownList($this->name, $this->value, $this->items, $this->options); 69 | } 70 | $this->registerAssets(); 71 | } 72 | 73 | /** 74 | * Register client assets 75 | */ 76 | protected function registerAssets() 77 | { 78 | $view = $this->getView(); 79 | ChosenSelectAsset::register($view); 80 | $js = '$("#' . $this->options['id'] . '").chosen(' . $this->getPluginOptions() . ');'; 81 | $view->registerJs($js, $view::POS_END); 82 | } 83 | 84 | /** 85 | * Return plugin options in json format 86 | * 87 | * @return string 88 | */ 89 | public function getPluginOptions() 90 | { 91 | return Json::encode($this->pluginOptions); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /ChosenSelectAsset.php: -------------------------------------------------------------------------------- 1 | 11 | * 12 | * @since 1.0 13 | */ 14 | class ChosenSelectAsset extends AssetBundle 15 | { 16 | /** 17 | * @var string the directory that contains the source asset files for this asset bundle 18 | */ 19 | public $sourcePath = '@bower/drmonty-chosen'; 20 | 21 | /** 22 | * @var array list of JavaScript files that this bundle contains 23 | */ 24 | public $js = [ 25 | 'js/chosen.jquery.js', 26 | 'js/chosen.proto.js', 27 | ]; 28 | 29 | /** 30 | * @var array list of CSS files that this bundle contains 31 | */ 32 | public $css = [ 33 | 'css/chosen.css', 34 | ]; 35 | 36 | /** 37 | * @var array list of JavaScript files that this bundle contains 38 | */ 39 | public $depends = [ 40 | 'yii\web\YiiAsset', 41 | ]; 42 | } 43 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 yii2mod 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Chosen Select Widget for Yii 2 2 | ========= 3 | Chosen Select Widget based on Chosen jQuery plugin [chosen](http://harvesthq.github.io/chosen) 4 | 5 | [![Latest Stable Version](https://poser.pugx.org/yii2mod/yii2-chosen-select/v/stable)](https://packagist.org/packages/yii2mod/yii2-chosen-select) [![Total Downloads](https://poser.pugx.org/yii2mod/yii2-chosen-select/downloads)](https://packagist.org/packages/yii2mod/yii2-chosen-select) [![License](https://poser.pugx.org/yii2mod/yii2-chosen-select/license)](https://packagist.org/packages/yii2mod/yii2-chosen-select) 6 | [![Build Status](https://travis-ci.org/yii2mod/yii2-chosen-select.svg?branch=master)](https://travis-ci.org/yii2mod/yii2-chosen-select) 7 | 8 | Installation 9 | ------------ 10 | 11 | The preferred way to install this extension is through [composer](http://getcomposer.org/download/). 12 | 13 | Either run 14 | 15 | ```sh 16 | composer require yii2mod/yii2-chosen-select 17 | ``` 18 | 19 | or add 20 | 21 | ```json 22 | "yii2mod/yii2-chosen-select": "^1.0" 23 | ``` 24 | 25 | to the require section of your composer.json. 26 | 27 | Usage 28 | ------------ 29 | 30 | 1) Usage with ActiveForm and model 31 | 32 | ```php 33 | echo $form->field($model, 'subject')->widget(\yii2mod\chosen\ChosenSelect::class, [ 34 | 'items' => [ 35 | 'first' => 'First', 36 | 'second' => 'Second' 37 | ], 38 | ]); 39 | ``` 40 | 41 | 2) Usage without a model 42 | 43 | ```php 44 | echo \yii2mod\chosen\ChosenSelect::widget([ 45 | 'name' => 'select', 46 | 'items' => BooleanEnum::listData(), 47 | 'options' => [ 48 | 'width' => '95%' 49 | ] 50 | ]); 51 | ``` 52 | 53 | Select Options 54 | ---------------- 55 | You can find them on the [options page](http://harvesthq.github.io/chosen/options.html) 56 | 57 | 58 | ## Support us 59 | 60 | Does your business depend on our contributions? Reach out and support us on [Patreon](https://www.patreon.com/yii2mod). 61 | All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff. 62 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "yii2mod/yii2-chosen-select", 3 | "description": "Chosen Select Widget based on Chosen jQuery plugin", 4 | "type": "yii2-extension", 5 | "keywords": [ 6 | "yii2", 7 | "select", 8 | "widget" 9 | ], 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Igor Chepurnoy", 14 | "email": "igorzfort@gmail.com" 15 | } 16 | ], 17 | "require": { 18 | "yiisoft/yii2": "*", 19 | "bower-asset/drmonty-chosen": ">=1.4" 20 | }, 21 | "require-dev": { 22 | "friendsofphp/php-cs-fixer": "~2.0" 23 | }, 24 | "autoload": { 25 | "psr-4": { 26 | "yii2mod\\chosen\\": "" 27 | } 28 | }, 29 | "repositories": [ 30 | { 31 | "type": "composer", 32 | "url": "https://asset-packagist.org" 33 | } 34 | ] 35 | } 36 | --------------------------------------------------------------------------------