Skip to content

Commit 07eb7b1

Browse files
author
Simone Todaro
committed
Allow user to specify the value property when options are objects
1 parent 860e439 commit 07eb7b1

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

src/components/Select.vue

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,16 @@
499499
default: 'label'
500500
},
501501
502+
/**
503+
* Tells vue-select what key to use when generating option
504+
* values when each `option` is an object.
505+
* @type {String}
506+
*/
507+
index: {
508+
type: String,
509+
default: null
510+
},
511+
502512
/**
503513
* Callback to generate the label text. If {option}
504514
* is an object, returns option[this.label] by default.
@@ -521,6 +531,15 @@
521531
return option[this.label]
522532
}
523533
}
534+
if(this.index) {
535+
let label = option
536+
this.options.forEach((val) => {
537+
if (val[this.index] == option) {
538+
label = val[this.label]
539+
}
540+
})
541+
return label
542+
}
524543
return option;
525544
}
526545
},
@@ -765,7 +784,15 @@
765784
if (this.taggable && !this.optionExists(option)) {
766785
option = this.createOption(option)
767786
}
768-
787+
if(this.index) {
788+
if (!option.hasOwnProperty(this.index)) {
789+
console.warn(
790+
`[vue-select warn]: Index key "option.${this.index}" does not` +
791+
` exist in options object ${JSON.stringify(option)}.\n`;
792+
)
793+
}
794+
option = option[this.index]
795+
}
769796
if (this.multiple && !this.mutableValue) {
770797
this.mutableValue = [option]
771798
} else if (this.multiple) {
@@ -787,7 +814,7 @@
787814
if (this.multiple) {
788815
let ref = -1
789816
this.mutableValue.forEach((val) => {
790-
if (val === option || typeof val === 'object' && val[this.label] === option[this.label]) {
817+
if (val === option || (this.index && val === option[this.index]) || (typeof val === 'object' && val[this.label] === option[this.label])) {
791818
ref = val
792819
}
793820
})

0 commit comments

Comments
 (0)