This time I will share jQuery Plugin and tutorial about Cascading (Dependent) Dropdown List Plugin – jQuery Chained Selects, hope it will help you in programming stack.
Chained Selects is a jQuery plugin for creating dependent dropdowns from JSON data that dynamically populates a set of select elements based on the previous selection.
How to use it:
1. Create a normal empty select element on the webpage.
1 |
< select id = "example" ></ select > |
2. Put jQuery library and the JavaScript file jquery.chained.selects.js
right before the </body>
tag.
1 |
< script src = "/path/to/cdn/jquery.slim.min.js" > |
3 |
< script src = "jquery.chained.selects.js" ></ script > |
3. Prepare your hierarchical data in the JSON objects.
4. Activate the plugin and done.
1 |
$( "#example" ).chainedSelects({ |
5. Customize the placeholder for the select elements.
1 |
$( "#example" ).chainedSelects({ |
3 |
placeholder: 'jQueryScript' |
6. Set the maximum number of levels of hierarchical data (Default: 10).
1 |
$( "#example" ).chainedSelects({ |
7. Enable/disable the debugg mode (Default: false).
1 |
$( "#example" ).chainedSelects({ |
8. Set the pre-selected option.
1 |
$( "#example" ).chainedSelects({ |
9. Set the pre-selected option by path.
1 |
$( "#example" ).chainedSelects({ |
2 |
defaultPath: [ "A" , "AB" ] |
10. Determine whether to sort the list by value..
1 |
$( "#example" ).chainedSelects({ |
11. Execute a function on selectection change.
1 |
$( "#example" ).chainedSelects({ |
2 |
onSelectedCallback: function (data){ |
12. Append a CSS class to the selected option. Default: false.
1 |
$( "#example" ).chainedSelects({ |
2 |
selectCssClass: 'yourClass' |
13. Determine whether to allow automatic pre-selection of option if there is only single one. Default: false.
1 |
$( "#example" ).chainedSelects({ |
2 |
sautoSelectSingleOptions: true |
14. API methods.
2 |
$( "#example" ).data( "chainedSelects" ).setLoggingEnabled(boolean); |
5 |
$( "#example" ).data( "chainedSelects" ).changeSelectedKey(newSelectedKey); |
Changelog:
v2.1.0 (2020-09-25)
- Added autoSelectSingleOptions option that allow automatic pre-selection of option if there is only single one (works recursively)
v2.0.3 (2020-05-25)
- fixed pre-selecting first option when no placeholder is provided
v2.0.2 (2020-04-27)
v2.0.1 (2019-09-09)
v2.0.0 (2019-07-09)
v1.0.4 (2019-06-21)
- Incorrect default value for sortByValue fixed
v1.0.3 (2018-12-14)
- user callback on selectection change.
2018-12-07
2018-11-08
- Added an option to pre-select option by name/value or path