Skip to content

Commit

Permalink
version 2.1.0 - autoSelectSingleOptions option added, documented, tested
Browse files Browse the repository at this point in the history
  • Loading branch information
smarek committed Sep 24, 2020
1 parent 3c85ddf commit 6bb4c96
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@
## 2.0.2

- Added possibility to append extra CSS classes to used/generated select elements through option 'selectCssClass'

## 2.1.0

Added option `autoSelectSingleOptions` to allow automatic pre-selection of option if there is only single one (works recursively)
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ $("#select-id").chainedSelects({
sortByValue: false, // sort options by text value, defaults to `false`
// IMPORTANT: if provided callback function fails, it will not report caught error if the `loggingEnabled` is not `true`
onSelectedCallback: function(id){}, // will call user defined function with id of currently selected, or empty string if non-final option was chosen, defaults to `false`
autoSelectSingleOptions: true, // will automatically select single options at any level (recursively), forcing user to make a choice only when there is choice to make, defaults to `false`
});
```

Expand Down
11 changes: 9 additions & 2 deletions jquery.chained.selects.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
defaultPath: false,
sortByValue: false,
onSelectedCallback: false,
selectCssClass: false
selectCssClass: false,
autoSelectSingleOptions: false
};

function ChainedSelect(element, options) {
Expand Down Expand Up @@ -91,7 +92,7 @@
this.hideLevelsGreaterThan(sid, levelNum);
let $level = this.getLevel(sid, levelNum);
$level.empty();
if(this.options.placeholder) {
if (this.options.placeholder) {
$level.append(new Option(this.options.placeholder ? this.options.placeholder : "", ""));
}
if ($.isFunction(data)) {
Expand Down Expand Up @@ -141,6 +142,12 @@
}
}
}
if (this.options.autoSelectSingleOptions === true) {
let childrenCount = $level.children().length;
if ((this.options.placeholder && childrenCount === 2) || (childrenCount === 1)) {
$level.children().last().attr('selected', 'selected');
}
}
$level.trigger('change');
$level.show();
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jquery-chained-selects",
"version": "2.0.3",
"version": "2.1.0",
"description": "jQuery plugin to create chained selects from JSON data",
"main": "jquery.chained.selects.js",
"scripts": {
Expand Down

0 comments on commit 6bb4c96

Please sign in to comment.