Skip to content

Commit 42b4fcb

Browse files
committed
Fixed #234
1 parent f7043b6 commit 42b4fcb

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

dev/full/schema.js

+3
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,9 @@ module.exports = {
457457
values: [{
458458
id: "admin",
459459
name: "Administrator"
460+
}, {
461+
id: 0,
462+
name: "Zero"
460463
}, {
461464
id: "moderator",
462465
name: "Moderator"

src/fields/core/fieldSelect.vue

+29-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template lang="pug">
22
select.form-control(v-model="value", :disabled="disabled", :name="schema.inputName", :id="getFieldID(schema)")
33
option(v-if="!selectOptions.hideNoneSelectedText", :disabled="schema.required", :value="null", :selected="value == undefined") {{ selectOptions.noneSelectedText || "&lt;Nothing selected&gt;" }}
4-
option(v-for="item in items", :value="getItemID(item)") {{ getItemName(item) }}
4+
option(v-for="item in items", :value="getItemValue(item)") {{ getItemName(item) }}
55
</template>
66

77
<script>
@@ -26,18 +26,37 @@
2626
},
2727
2828
methods: {
29-
getItemID(item) {
30-
if (isObject(item) && item.id)
31-
return item.id;
32-
33-
return item;
29+
getItemValue(item) {
30+
if (isObject(item)){
31+
if (typeof this.schema["selectOptions"] !== "undefined" && typeof this.schema["selectOptions"]["value"] !== "undefined") {
32+
return item[this.schema.selectOptions.value];
33+
} else {
34+
// Use 'id' instead of 'value' cause of backward compatibility
35+
if (typeof item["id"] !== "undefined") {
36+
return item.id;
37+
} else {
38+
throw "id is not defined. If you want to use another key name, add a `value` property under `selectOptions` in the schema. https://icebob.gitbooks.io/vueformgenerator/content/fields/select.html#select-field-with-object-items";
39+
}
40+
}
41+
} else {
42+
return item;
43+
}
3444
},
3545
3646
getItemName(item) {
37-
if (isObject(item) && item.name)
38-
return item.name;
39-
40-
return item;
47+
if (isObject(item)){
48+
if (typeof this.schema["selectOptions"] !== "undefined" && typeof this.schema["selectOptions"]["name"] !== "undefined") {
49+
return item[this.schema.selectOptions.name];
50+
} else {
51+
if (typeof item["name"] !== "undefined") {
52+
return item.name;
53+
} else {
54+
throw "name is not defined. If you want to use another key name, add a `name` property under `selectOptions` in the schema. https://icebob.gitbooks.io/vueformgenerator/content/fields/select.html#select-field-with-object-items";
55+
}
56+
}
57+
} else {
58+
return item;
59+
}
4160
}
4261
}
4362
};

0 commit comments

Comments
 (0)