├── .github ├── CONTRIBUTING.md ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE.md └── stale.yml ├── CHANGE.md ├── LICENSE.md ├── README.md ├── composer.json └── src ├── DepDrop.php ├── DepDropAction.php ├── DepDropAsset.php ├── DepDropExtAsset.php └── assets └── js ├── depdrop.js └── depdrop.min.js /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Contributing to yii2-widget-depdrop 2 | =================================== 3 | Looking to contribute something to yii2-widget-depdrop? **Here's how you can help.** 4 | 5 | Please take a moment to review this document in order to make the contribution 6 | process easy and effective for everyone involved. 7 | 8 | Following these guidelines helps to communicate that you respect the time of 9 | the developers managing and developing this open source project. In return, 10 | they should reciprocate that respect in addressing your issue or assessing 11 | patches and features. 12 | 13 | Using the issue tracker 14 | ----------------------- 15 | When [reporting bugs][reporting-bugs] or 16 | [requesting features][requesting-features], the 17 | [issue tracker on GitHub][issue-tracker] is the recommended channel to use. 18 | 19 | The issue tracker **is not** a place for support requests. Refer the 20 | [extension documentation and demos](http://demos.krajee.com/widget-details/active-form) and/or refer to the 21 | [webtips Q & A forum](http://webtips.krajee.com/questions) which are the better places to get help. 22 | 23 | Reporting bugs with yii2-widget-depdrop 24 | ------------------------------------------ 25 | We really appreciate clear bug reports that _consistently_ show an issue 26 | _within yii2-widget-depdrop_. 27 | 28 | The ideal bug report follows these guidelines: 29 | 30 | 1. **Use the [GitHub issue search][issue-search]** — Check if the issue 31 | has already been reported. 32 | 2. **Check if the issue has been fixed** — Try to reproduce the problem 33 | using the code in the `master` branch. 34 | 3. **Isolate the problem** — Try to share a demo or a test case that 35 | consistently reproduces the problem. 36 | 37 | Please try to be as detailed as possible in your bug report, especially if an 38 | isolated test case cannot be made. Some useful questions to include the answer 39 | to are: 40 | 41 | - What steps can be used to reproduce the issue? 42 | - What is the bug and what is the expected outcome? 43 | - What browser(s) and Operating System have you tested with? 44 | - Does the bug happen consistently across all tested browsers? 45 | - What version of jQuery are you using? And what version of yii2-widget-depdrop? 46 | - Are you using yii2-widget-depdrop with other plugins? 47 | 48 | All of these questions will help others fix and identify any potential bugs. 49 | 50 | Requesting features in yii2-widget-depdrop 51 | ------------------------------------------ 52 | Before starting work on a major feature for yii2-widget-depdrop, **read the 53 | [documentation](http://demos.krajee.com/widget-details/active-form) first** or you may risk spending a considerable amount of 54 | time on something which the project developers are not interested in bringing into the project. 55 | 56 | ### Submitting a pull request 57 | 58 | We use GitHub's pull request system for submitting patches. Here are some 59 | guidelines to follow when creating the pull request for your fix. 60 | 61 | 1. Make sure to create a ticket for your pull request. This will serve as the 62 | bug ticket, and any discussion about the bug will take place there. Your pull 63 | request will be focused on the specific changes that fix the bug. 64 | 2. Make sure to reference the ticket you are fixing within your pull request. 65 | This will allow us to close off the ticket once we merge the pull request, or 66 | follow up on the ticket if there are any related blocking issues. 67 | 3. Explain why the specific change was made. Not everyone who is reviewing your 68 | pull request will be familiar with the problem it is fixing. 69 | 4. Run your tests first. If your tests aren't passing, the pull request won't 70 | be able to be merged. If you're breaking existing tests, make sure that you 71 | aren't causing any breaking changes. 72 | 5. Only include source changes. While it's not required, only including changes 73 | from the `src` directory will prevent merge conflicts from occuring. Making 74 | this happen can be as a simple as not committing changes from the `dist` 75 | directory. 76 | 77 | By following these steps, you will make it easier for your pull request to be 78 | reviewed and eventually merged. 79 | 80 | Triaging issues and pull requests 81 | --------------------------------- 82 | Anyone can help the project maintainers triage issues and review pull requests. 83 | 84 | ### Handling new issues 85 | 86 | yii2-widget-depdrop regularly receives new issues which need to be tested and organized. 87 | 88 | When a new issue that comes in that is similar to another existing issue, it 89 | should be checked to make sure it is not a duplicate. Duplicates issues should 90 | be marked by replying to the issue with "Duplicate of #[issue number]" where 91 | `[issue number]` is the url or issue number for the existing issue. This will 92 | allow the project maintainers to quickly close off additional issues and keep 93 | the discussion focused within a single issue. 94 | 95 | If you can test issues that are reported to yii2-widget-depdrop that contain test cases and 96 | confirm under what conditions bugs happen, that will allow others to identify 97 | what causes a bug quicker. 98 | 99 | ### Reviewing pull requests 100 | 101 | It is very common for pull requests to be opened for issues that contain a clear 102 | solution to the problem. These pull requests should be rigorously reviewed by 103 | the community before being accepted. If you are not sure about a piece of 104 | submitted code, or know of a better way to do something, do not hesitate to make 105 | a comment on the pull request. 106 | 107 | ### Reviving old tickets 108 | 109 | If you come across tickets which have not been updated for a while, you are 110 | encouraged to revive them. While this can be as simple as saying `:+1:`, it is 111 | best if you can include more information on the issue. Common bugs and feature 112 | requests are more likely to be fixed, whether it is by the community or the 113 | developers, so keeping tickets up to date is encouraged. 114 | 115 | Licensing 116 | --------- 117 | 118 | It should also be made clear that **all code contributed to yii2-widget-depdrop** must be 119 | licensable under the [BSD-3 license][licensing]. Code that cannot be released 120 | under this license **cannot be accepted** into the project. 121 | 122 | [issue-search]: https://github.com/kartik-v/yii2-widget-depdrop/search?q=&type=Issues 123 | [issue-tracker]: https://github.com/kartik-v/yii2-widget-depdrop/issues 124 | [licensing]: https://github.com/kartik-v/yii2-widget-depdrop/blob/master/LICENSE.md 125 | [reporting-bugs]: #reporting-bugs-with-yii2-widget-depdrop 126 | [requesting-features]: #requesting-features-in-yii2-widget-depdrop -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ## Prerequisites 2 | 3 | - [ ] I have searched for similar issues in both open and closed tickets and cannot find a duplicate. 4 | - [ ] The issue still exists against the latest `master` branch of yii2-widget-depdrop. 5 | - [ ] This is not an usage question. I confirm having gone through and read the documentation for [ActiveForm](http://demos.krajee.com/widget-details/active-form) and [ActiveField](http://demos.krajee.com/widget-details/active-field). 6 | - [ ] This is not a general programming / coding question. (Those should be directed to the [webtips Q & A forum](http://webtips.krajee.com/questions)). 7 | - [ ] I have attempted to find the simplest possible steps to reproduce the issue. 8 | - [ ] I have included a failing test as a pull request (Optional). 9 | 10 | ## Steps to reproduce the issue 11 | 12 | 1. 13 | 2. 14 | 3. 15 | 16 | ## Expected behavior and actual behavior 17 | 18 | When I follow those steps, I see... 19 | 20 | I was expecting... 21 | 22 | ## Environment 23 | 24 | #### Browsers 25 | 26 | - [ ] Google Chrome 27 | - [ ] Mozilla Firefox 28 | - [ ] Internet Explorer 29 | - [ ] Safari 30 | 31 | #### Operating System 32 | 33 | - [ ] Windows 34 | - [ ] Mac OS X 35 | - [ ] Linux 36 | - [ ] Mobile 37 | 38 | #### Libraries 39 | 40 | - jQuery version: 41 | - yii2-widget-depdrop version: 42 | 43 | ## Isolating the problem 44 | 45 | - [ ] This bug happens [on the demos page](http://demos.krajee.com/widget-details/active-form) 46 | - [ ] The bug happens consistently across all tested browsers 47 | - [ ] This bug happens when using yii2-widget-depdrop without other plugins. -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ## Scope 2 | This pull request includes a 3 | 4 | - [ ] Bug fix 5 | - [ ] New feature 6 | - [ ] Translation 7 | 8 | ## Changes 9 | The following changes were made (this change is also documented in the [change log](https://github.com/kartik-v/yii2-widget-depdrop/blob/master/CHANGE.md)): 10 | 11 | - 12 | - 13 | - 14 | 15 | ## Related Issues 16 | If this is related to an existing ticket, include a link to it as well. -------------------------------------------------------------------------------- /.github/stale.yml: -------------------------------------------------------------------------------- 1 | # Number of days of inactivity before an issue becomes stale 2 | daysUntilStale: 60 3 | # Number of days of inactivity before a stale issue is closed 4 | daysUntilClose: 7 5 | # Issues with these labels will never be considered stale 6 | exemptLabels: 7 | - bug 8 | - enhancement 9 | - pinned 10 | - security 11 | # Label to use when marking an issue as stale 12 | staleLabel: wontfix 13 | # Comment to post when marking an issue as stale. Set to `false` to disable 14 | markComment: > 15 | This issue has been automatically marked as stale because it has not had 16 | recent activity. It will be closed if no further activity occurs. Thank you 17 | for your contributions. 18 | # Comment to post when closing a stale issue. Set to `false` to disable 19 | closeComment: false -------------------------------------------------------------------------------- /CHANGE.md: -------------------------------------------------------------------------------- 1 | Change Log: `yii2-widget-depdrop` 2 | ================================= 3 | 4 | ## Version 1.0.6 5 | 6 | **Date:** 19-Apr-2019 7 | 8 | - (enh #73): Bump composer dependencies. 9 | - (enh #71, #72): Add `enableCsrfValidation` property to `DepDropAction`. 10 | 11 | ## Version 1.0.5 12 | 13 | **Date:** 09-Oct-2018 14 | 15 | - Bump composer dependencies. 16 | - Add Bootstrap 4.x support. 17 | - Reorganize all source code in `src` directory. 18 | - Add github contribution and issue/PR log templates. 19 | - Update copyright year to current. 20 | 21 | ## Version 1.0.4 22 | 23 | **Date:** 10-Jan-2016 24 | 25 | - Enhancements for PJAX based reinitialization. Complements enhancements in kartik-v/yii2-krajee-base#52 and kartik-v/yii2-krajee-base#53. 26 | 27 | ## version 1.0.3 28 | 29 | **Date:** 16-Dec-2015 30 | 31 | - (enh #25): Implement new standalone `DepDropAction` for usage in Controller. 32 | - (enh #26): Enhance update of Select2 plugin values after dependency change. 33 | 34 | ## version 1.0.2 35 | 36 | **Date:** 26-Jun-2015 37 | 38 | - (enh #17): Tighter integration for Select2 widget on clear. 39 | - Set composer dependencies. 40 | 41 | ## version 1.0.1 42 | 43 | **Date:** 22-May-2015 44 | 45 | - (enh #15): Fixes for release v2.0.0 of kartik-v/yii2-widget-select2. 46 | 47 | ## version 1.0.0 48 | 49 | **Date:** 08-Nov-2014 50 | 51 | - Initial release 52 | - Sub repo split from [yii2-widgets](https://github.com/kartik-v/yii2-widgets) -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 - 2019, Kartik Visweswaran 2 | Krajee.com 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without modification, 6 | are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, this 9 | list of conditions and the following disclaimer. 10 | 11 | * Redistributions in binary form must reproduce the above copyright notice, this 12 | list of conditions and the following disclaimer in the documentation and/or 13 | other materials provided with the distribution. 14 | 15 | * Neither the names of Kartik Visweswaran or Krajee nor the names of its 16 | contributors may be used to endorse or promote products derived from 17 | this software without specific prior written permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 20 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 23 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 26 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | Krajee Logo 4 | 5 |
6 | yii2-widget-depdrop 7 |
8 | 10 | Donate 11 | 12 |

13 | 14 | [![Stable Version](https://poser.pugx.org/kartik-v/yii2-widget-depdrop/v/stable)](https://packagist.org/packages/kartik-v/yii2-widget-depdrop) 15 | [![Unstable Version](https://poser.pugx.org/kartik-v/yii2-widget-depdrop/v/unstable)](https://packagist.org/packages/kartik-v/yii2-widget-depdrop) 16 | [![License](https://poser.pugx.org/kartik-v/yii2-widget-depdrop/license)](https://packagist.org/packages/kartik-v/yii2-widget-depdrop) 17 | [![Total Downloads](https://poser.pugx.org/kartik-v/yii2-widget-depdrop/downloads)](https://packagist.org/packages/kartik-v/yii2-widget-depdrop) 18 | [![Monthly Downloads](https://poser.pugx.org/kartik-v/yii2-widget-depdrop/d/monthly)](https://packagist.org/packages/kartik-v/yii2-widget-depdrop) 19 | [![Daily Downloads](https://poser.pugx.org/kartik-v/yii2-widget-depdrop/d/daily)](https://packagist.org/packages/kartik-v/yii2-widget-depdrop) 20 | 21 | The DepDrop widget is a Yii 2 wrapper for the [dependent-dropdown jQuery plugin by Krajee](http://plugins.krajee.com/dependent-dropdown). This plugin allows multi level dependent dropdown with nested dependencies. The plugin thus enables to convert normal select inputs to a dependent input field, whose options are derived based on value selected in another input/or a group of inputs. It works both with normal select options and select with optgroups as well. 22 | 23 | > NOTE: This extension is a sub repo split of [yii2-widgets](https://github.com/kartik-v/yii2-widgets). The split has been done since 08-Nov-2014 to allow developers to install this specific widget in isolation if needed. One can also use the extension the previous way with the whole suite of [yii2-widgets](http://demos.krajee.com/widgets). 24 | 25 | ## Installation 26 | 27 | The preferred way to install this extension is through [composer](http://getcomposer.org/download/). Check the [composer.json](https://github.com/kartik-v/yii2-widget-depdrop/blob/master/composer.json) for this extension's requirements and dependencies. Read this [web tip /wiki](http://webtips.krajee.com/setting-composer-minimum-stability-application/) on setting the `minimum-stability` settings for your application's composer.json. 28 | 29 | To install, either run 30 | 31 | ``` 32 | $ php composer.phar require kartik-v/yii2-widget-depdrop "@dev" 33 | ``` 34 | 35 | or add 36 | 37 | ``` 38 | "kartik-v/yii2-widget-depdrop": "@dev" 39 | ``` 40 | 41 | to the ```require``` section of your `composer.json` file. 42 | 43 | ## Release Changes 44 | 45 | > NOTE: Refer the [CHANGE LOG](https://github.com/kartik-v/yii2-widget-depdrop/blob/master/CHANGE.md) for details on changes to various releases. 46 | 47 | ## Demo 48 | 49 | You can refer detailed [documentation and demos](http://demos.krajee.com/widget-details/depdrop) on usage of the extension. 50 | 51 | ## Usage 52 | 53 | ```php 54 | use kartik\depdrop\DepDrop; 55 | 56 | // Normal parent select 57 | echo $form->field($model, 'cat')->dropDownList($catList, ['id'=>'cat-id']); 58 | 59 | // Dependent Dropdown 60 | echo $form->field($model, 'subcat')->widget(DepDrop::classname(), [ 61 | 'options' => ['id'=>'subcat-id'], 62 | 'pluginOptions'=>[ 63 | 'depends'=>['cat-id'], 64 | 'placeholder' => 'Select...', 65 | 'url' => Url::to(['/site/subcat']) 66 | ] 67 | ]); 68 | ``` 69 | 70 | ## License 71 | 72 | **yii2-widget-depdrop** is released under the BSD-3-Clause License. See the bundled `LICENSE.md` for details. -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "kartik-v/yii2-widget-depdrop", 3 | "description": "Widget that enables setting up dependent dropdowns with nested dependencies (sub repo split from yii2-widgets)", 4 | "keywords": [ 5 | "yii2", 6 | "extension", 7 | "widget", 8 | "dependent", 9 | "dropdown", 10 | "form", 11 | "jquery", 12 | "plugin" 13 | ], 14 | "homepage": "https://github.com/kartik-v/yii2-widget-depdrop", 15 | "type": "yii2-extension", 16 | "license": "BSD-3-Clause", 17 | "authors": [ 18 | { 19 | "name": "Kartik Visweswaran", 20 | "email": "kartikv2@gmail.com", 21 | "homepage": "http://www.krajee.com/" 22 | } 23 | ], 24 | "require": { 25 | "kartik-v/yii2-krajee-base": ">=2.0.0", 26 | "kartik-v/dependent-dropdown": "~1.4" 27 | }, 28 | "autoload": { 29 | "psr-4": { 30 | "kartik\\depdrop\\": "src" 31 | } 32 | }, 33 | "extra": { 34 | "branch-alias": { 35 | "dev-master": "1.0.x-dev" 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /src/DepDrop.php: -------------------------------------------------------------------------------- 1 | 27 | * @since 1.0.6 28 | */ 29 | class DepDrop extends InputWidget 30 | { 31 | /** 32 | * @var string the default native select input dropdown style 33 | */ 34 | const TYPE_DEFAULT = 1; 35 | 36 | /** 37 | * @var string the enhanced Select2 widget dropdown style 38 | */ 39 | const TYPE_SELECT2 = 2; 40 | 41 | /** 42 | * @var int the type of the dropdown element. Defaults to `1`. Can be one of the following: 43 | * 44 | * - 1 or [[DepDrop::TYPE_DEFAULT]] will render using \yii\helpers\Html::dropDownList 45 | * - 2 or [[DepDrop::TYPE_SELECT2]] will render using \kartik\widgets\Select2 widget 46 | */ 47 | public $type = self::TYPE_DEFAULT; 48 | 49 | /** 50 | * @var array the configuration options for the Select2 widget. Applicable only if the `type` property is set to 51 | * [[DepDrop::TYPE_SELECT2]]. 52 | */ 53 | public $select2Options = []; 54 | 55 | /** 56 | * @inheritdoc 57 | */ 58 | public $pluginName = 'depdrop'; 59 | 60 | /** 61 | * Runs the widget 62 | * 63 | * @throws InvalidConfigException 64 | */ 65 | public function run() 66 | { 67 | if (empty($this->pluginOptions['url'])) { 68 | throw new InvalidConfigException("The 'pluginOptions[\"url\"]' property has not been set."); 69 | } 70 | if (empty($this->pluginOptions['depends']) || !is_array($this->pluginOptions['depends'])) { 71 | throw new InvalidConfigException("The 'pluginOptions[\"depends\"]' property must be set and must be an array of dependent dropdown element identifiers."); 72 | } 73 | if (empty($this->options['class'])) { 74 | $this->options['class'] = 'form-control'; 75 | } 76 | if ($this->type === self::TYPE_SELECT2) { 77 | Config::checkDependency('select2\Select2', 'yii2-widget-select2', 'for dependent dropdown for Select2'); 78 | } 79 | if ($this->type !== self::TYPE_SELECT2 && !empty($this->options['placeholder'])) { 80 | $this->data = ['' => $this->options['placeholder']] + $this->data; 81 | } 82 | $this->registerAssets(); 83 | } 84 | 85 | /** 86 | * Registers the needed assets 87 | */ 88 | public function registerAssets() 89 | { 90 | $view = $this->getView(); 91 | DepDropAsset::register($view)->addLanguage($this->language, 'depdrop_locale_'); 92 | DepDropExtAsset::register($view); 93 | $this->registerPlugin($this->pluginName); 94 | if ($this->type === self::TYPE_SELECT2) { 95 | $loading = ArrayHelper::getValue($this->pluginOptions, 'loadingText', 'Loading ...'); 96 | $this->select2Options['data'] = $this->data; 97 | $this->select2Options['options'] = $this->options; 98 | if ($this->hasModel()) { 99 | $settings = ArrayHelper::merge($this->select2Options, [ 100 | 'model' => $this->model, 101 | 'attribute' => $this->attribute 102 | ]); 103 | } else { 104 | $settings = ArrayHelper::merge($this->select2Options, [ 105 | 'name' => $this->name, 106 | 'value' => $this->value 107 | ]); 108 | } 109 | echo Select2::widget($settings); 110 | $id = $this->options['id']; 111 | $view->registerJs("initDepdropS2('{$id}','{$loading}');"); 112 | } else { 113 | echo $this->getInput('dropdownList', true); 114 | } 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /src/DepDropAction.php: -------------------------------------------------------------------------------- 1 | [ 29 | * 'class' => \kartik\depdrop\DepDropAction::className(), 30 | * 'outputCallback' => function ($selectedId, $params) { 31 | * return [ 32 | * [ 33 | * 'id' => 1, 34 | * 'name' => 'Car', 35 | * ], 36 | * [ 37 | * 'id' => 2, 38 | * 'name' => 'bike', 39 | * ], 40 | * ]; 41 | * 42 | * // with optgroup 43 | * return [ 44 | * 'group1' => [ 45 | * ['id' => '', 'name' => ''], 46 | * ['id' => '', 'name' => ''] 47 | * ], 48 | * 'group2' => [ 49 | * ['id' => '', 'name' => ''], 50 | * ['id' => '', 'name' => ''] 51 | * ] 52 | * ]; 53 | * } 54 | * ] 55 | * ]); 56 | * } 57 | * ``` 58 | * 59 | * @see http://plugins.krajee.com/dependent-dropdown 60 | * @see http://github.com/kartik-v/dependent-dropdown 61 | * 62 | * @author Kartik Visweswaran 63 | * @since 1.0.6 64 | * 65 | */ 66 | class DepDropAction extends Action 67 | { 68 | /** 69 | * @var string parent parameter name for the dependent dropdown 70 | */ 71 | public $parentParam = 'depdrop_parents'; 72 | 73 | /** 74 | * @var string other parameter name for the dependent dropdown 75 | */ 76 | public $otherParam = 'depdrop_params'; 77 | 78 | /** 79 | * @var Closure the output callback function 80 | */ 81 | public $outputCallback; 82 | 83 | /** 84 | * @var Closure the selected callback function 85 | */ 86 | public $selectedCallback; 87 | 88 | /** 89 | * @var bool whether selected value can be empty. Defaults to `false`. 90 | */ 91 | public $allowEmpty = false; 92 | 93 | /** 94 | * @var bool whether CSRF validation is enabled for this action. Defaults to `true`. 95 | */ 96 | public $enableCsrfValidation = true; 97 | 98 | /** 99 | * @inheritdoc 100 | */ 101 | public function init() 102 | { 103 | parent::init(); 104 | $this->controller->enableCsrfValidation = $this->enableCsrfValidation; 105 | } 106 | 107 | /** 108 | * @inheritdoc 109 | */ 110 | public function run() 111 | { 112 | Yii::$app->response->format = Response::FORMAT_JSON; 113 | $request = Yii::$app->getRequest(); 114 | if (($selected = $request->post($this->parentParam)) && is_array($selected) && (!empty($selected[0]) || $this->allowEmpty)) { 115 | $params = $request->post($this->otherParam, []); 116 | $id = $selected[0]; 117 | return ['output' => $this->getOutput($id, $params), 'selected' => $this->getSelected($id, $params)]; 118 | } 119 | return ['output' => '', 'selected' => '']; 120 | } 121 | 122 | /** 123 | * Return select option values output 124 | * 125 | * @param string $id the selected value identifier 126 | * @param array $params the parameters passed 127 | * 128 | * @return mixed the option values 129 | */ 130 | protected function getOutput($id, $params = []) 131 | { 132 | return $this->parseCallback('outputCallback', $id, $params); 133 | } 134 | 135 | /** 136 | * Return selected value 137 | * 138 | * @param string $id the selected value identifier 139 | * @param array $params the parameters passed 140 | * 141 | * @return string the selected value 142 | */ 143 | protected function getSelected($id, $params = []) 144 | { 145 | return $this->parseCallback('selectedCallback', $id, $params); 146 | } 147 | 148 | /** 149 | * Parses the callback function name and if callable, executes it to return value 150 | * 151 | * @param string $funcName the function name 152 | * @param string $id the selected value identifier 153 | * @param array $params the parameters passed 154 | * 155 | * @return mixed the parsed value 156 | */ 157 | protected function parseCallback($funcName, $id, $params = []) 158 | { 159 | if (!isset($this->$funcName)) { 160 | return ''; 161 | } 162 | $func = $this->$funcName; 163 | if (is_callable($func)) { 164 | return $func($id, $params); 165 | } 166 | return ''; 167 | } 168 | } 169 | -------------------------------------------------------------------------------- /src/DepDropAsset.php: -------------------------------------------------------------------------------- 1 | 18 | * @since 1.0 19 | */ 20 | class DepDropAsset extends AssetBundle 21 | { 22 | /** 23 | * @inheritdoc 24 | */ 25 | public function init() 26 | { 27 | $this->setSourcePath('@vendor/kartik-v/dependent-dropdown'); 28 | $this->setupAssets('css', ['css/dependent-dropdown']); 29 | $this->setupAssets('js', ['js/dependent-dropdown']); 30 | parent::init(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/DepDropExtAsset.php: -------------------------------------------------------------------------------- 1 | 18 | * @since 1.0 19 | */ 20 | class DepDropExtAsset extends AssetBundle 21 | { 22 | /** 23 | * @inheritdoc 24 | */ 25 | public function init() 26 | { 27 | $this->setSourcePath(__DIR__ . '/assets'); 28 | $this->setupAssets('js', ['js/depdrop']); 29 | parent::init(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/assets/js/depdrop.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014 - 2016 3 | * @package yii2-widgets 4 | * @subpackage yii2-widget-depdrop 5 | * @version 1.0.6 6 | * 7 | * Extensions to dependent dropdown for Yii: 8 | * - Initializes dependent dropdown for Select2 widget 9 | * 10 | * For more JQuery plugins visit http://plugins.krajee.com 11 | * For more Yii related demos visit http://demos.krajee.com 12 | */ 13 | var initDepdropS2; 14 | (function ($) { 15 | "use strict"; 16 | initDepdropS2 = function (id, text) { 17 | var $s2 = $('#' + id), $s2cont = $('#select2-' + id + '-container'), ph = '...'; 18 | $s2.on('depdrop:beforeChange', function () { 19 | $s2.find('option').attr('value', ph).html(text); 20 | $s2.val(ph); 21 | $s2.select2('val', ph); 22 | $s2cont.removeClass('kv-loading').addClass('kv-loading'); 23 | }).on('depdrop:afterChange', function () { 24 | $s2.trigger('change'); 25 | $s2cont.removeClass('kv-loading'); 26 | }); 27 | }; 28 | }(window.jQuery)); -------------------------------------------------------------------------------- /src/assets/js/depdrop.min.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014 - 2016 3 | * @package yii2-widgets 4 | * @subpackage yii2-widget-depdrop 5 | * @version 1.0.3 6 | * 7 | * Extensions to dependent dropdown for Yii: 8 | * - Initializes dependent dropdown for Select2 widget 9 | * 10 | * For more JQuery plugins visit http://plugins.krajee.com 11 | * For more Yii related demos visit http://demos.krajee.com 12 | */var initDepdropS2;!function(e){"use strict";initDepdropS2=function(n,o){var a=e("#"+n),i=e("#select2-"+n+"-container"),t="...";a.on("depdrop.beforeChange",function(){a.find("option").attr("value",t).html(o),a.val(t),a.select2("val",t),i.removeClass("kv-loading").addClass("kv-loading")}).on("depdrop.afterChange",function(){a.trigger("change"),i.removeClass("kv-loading")})}}(window.jQuery); --------------------------------------------------------------------------------