mirror of
https://github.com/Choices-js/Choices.git
synced 2024-05-20 22:46:33 +02:00
Documentation for 'getValue' method + housekeeping
This commit is contained in:
parent
eeaeccc168
commit
79c08ceff9
13
README.md
13
README.md
|
@ -397,6 +397,19 @@ choices.setChoices([
|
|||
], 'value', 'label');
|
||||
```
|
||||
|
||||
### getValue(valueOnly)
|
||||
<strong>Input types affected:</strong> `text`, `select-one`, `select-multiple`
|
||||
|
||||
<strong>Usage:</strong> Get value(s) of input (i.e. inputted items (text) or selected choices (select)). Optionally pass an argument of `true` to only return values rather than value objects.
|
||||
|
||||
<strong>Example:</strong>
|
||||
|
||||
```js
|
||||
const example = new Choices(element);
|
||||
const values = example.getValue(true); // returns ['value 1', 'value 2'];
|
||||
const valueArray = example.getValue(); // returns [{ active: true, choiceId: 1, highlighted: false, id: 1, label: 'Label 1', value: 'Value 1'}, { active: true, choiceId: 2, highlighted: false, id: 2, label: 'Label 2', value: 'Value 2'}];
|
||||
```
|
||||
|
||||
### setValue(args);
|
||||
<strong>Input types affected:</strong> `text`
|
||||
|
||||
|
|
4
assets/scripts/dist/choices.min.js
vendored
4
assets/scripts/dist/choices.min.js
vendored
File diff suppressed because one or more lines are too long
|
@ -361,6 +361,32 @@ export class Choices {
|
|||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get value(s) of input (i.e. inputted items (text) or selected choices (select))
|
||||
* @param {Boolean} valueOnly Get only values of selected items, otherwise return selected items
|
||||
* @return {Array/String} selected value (select-one) or array of selected items (inputs & select-multiple)
|
||||
* @public
|
||||
*/
|
||||
getValue(valueOnly = false){
|
||||
const items = this.store.getItemsFilteredByActive();
|
||||
let selectedItems = [];
|
||||
|
||||
items.forEach((item) => {
|
||||
if (this.passedElement.type === 'text'){
|
||||
selectedItems.push(valueOnly ? item.value : item);
|
||||
} else if(item.active) {
|
||||
selectedItems.push(valueOnly ? item.value : item);
|
||||
}
|
||||
});
|
||||
|
||||
if (this.passedElement.type == 'select-one') {
|
||||
return selectedItems[0];
|
||||
} else {
|
||||
return selectedItems;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set value of input. If the input is a select box, a choice will be created and selected otherwise
|
||||
* an item will created directly.
|
||||
|
@ -395,31 +421,6 @@ export class Choices {
|
|||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get value(s)
|
||||
* @param {Boolean} onlyValue Get only values of selected items, otherwise return selected items
|
||||
* @return {Array/String} selected value (select-one) or array of selected items (inputs & select-multiple)
|
||||
* @public
|
||||
*/
|
||||
getValue(onlyValue){
|
||||
const items = this.passedElement.type === 'text' ? this.store.getState().items : this.store.getState().choices;
|
||||
let selectedItems = [];
|
||||
|
||||
items.forEach((item) => {
|
||||
if (this.passedElement.type === 'text'){
|
||||
selectedItems.push(onlyValue ? item.value : item);
|
||||
}else if(item.selected) {
|
||||
selectedItems.push(onlyValue ? item.value : item);
|
||||
}
|
||||
});
|
||||
|
||||
if (this.passedElement.type == 'select-one') {
|
||||
return selectedItems[0];
|
||||
} else {
|
||||
return selectedItems;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Select value of select box via the value of an existing choice
|
||||
|
|
|
@ -163,6 +163,8 @@
|
|||
removeItemButton: true
|
||||
});
|
||||
|
||||
console.log(choices1.getValue());
|
||||
|
||||
var choices2 = new Choices('#choices-2', {
|
||||
paste: false,
|
||||
duplicateItems: false,
|
||||
|
@ -265,6 +267,7 @@
|
|||
{value: 'Three', label: 'Label Three'},
|
||||
],
|
||||
}).setValueByChoice('Two');
|
||||
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
|
Loading…
Reference in a new issue