forked from vue-generators/vue-form-generator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfieldSelectEx.vue
93 lines (83 loc) · 2.64 KB
/
fieldSelectEx.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<template lang="pug">
select.selectpicker(v-model="value", :disabled="disabled", :multiple="fieldOptions.multiSelect", :title="placeholder", data-width="100%", :name="inputName")
option(:disabled="schema.required", v-if="fieldOptions.multiSelect !== true", :value="null", :selected="value == undefined")
option(v-for="item in items", :value="getItemValue(item)") {{ getItemName(item) }}
</template>
<script>
/* global $ */
import { isObject } from "lodash";
import abstractField from "../abstractField";
export default {
name: "field-selectex",
mixins: [abstractField],
computed: {
items() {
let values = this.schema.values;
if (typeof values == "function") {
return values.apply(this, [this.model, this.schema]);
} else return values;
}
},
methods: {
getItemValue(item) {
if (isObject(item)) {
if (typeof this.fieldOptions["value"] !== "undefined") {
return item[this.fieldOptions.value];
} else {
// Use 'id' instead of 'value' cause of backward compatibility
if (typeof item["id"] !== "undefined") {
return item.id;
} else {
throw "`id` is not defined. If you want to use another key name, add a `value` property under `fieldOptions` in the schema. https://icebob.gitbooks.io/vueformgenerator/content/fields/select.html#select-field-with-object-items";
}
}
} else {
return item;
}
},
getItemName(item) {
if (isObject(item)) {
if (typeof this.fieldOptions["name"] !== "undefined") {
return item[this.fieldOptions.name];
} else {
if (typeof item["name"] !== "undefined") {
return item.name;
} else {
throw "`name` is not defined. If you want to use another key name, add a `name` property under `fieldOptions` in the schema. https://icebob.gitbooks.io/vueformgenerator/content/fields/select.html#select-field-with-object-items";
}
}
} else {
return item;
}
}
},
watch: {
model: function() {
if (typeof $.fn !== "undefined" && $.fn.selectpicker) $(this.$el).selectpicker("refresh");
}
},
mounted() {
this.$nextTick(() => {
if (typeof $.fn !== "undefined" && $.fn.selectpicker) {
$(this.$el)
.selectpicker("destroy")
.selectpicker(this.fieldOptions);
} else {
console.warn(
"Bootstrap-select library is missing. Please download from https://silviomoreto.github.io/bootstrap-select/ and load the script and CSS in the HTML head section!"
);
}
});
},
beforeDestroy() {
if ($.fn.selectpicker) $(this.$el).selectpicker("destroy");
}
};
</script>
<style lang="scss">
.vue-form-generator .field-selectEx .bootstrap-select {
.dropdown-menu li.selected .text {
font-weight: bold;
}
}
</style>