mirror of
https://github.com/Choices-js/Choices.git
synced 2024-06-03 06:22:25 +02:00
getValue()
This commit is contained in:
parent
677d3a5582
commit
1758943d7c
|
@ -395,6 +395,31 @@ export class Choices {
|
||||||
|
|
||||||
return this;
|
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
|
* Select value of select box via the value of an existing choice
|
||||||
|
|
Loading…
Reference in a new issue