|
1 | 1 | <template lang="pug">
|
2 | 2 | select.form-control(v-model="value", :disabled="disabled", :name="schema.inputName", :id="getFieldID(schema)")
|
3 | 3 | option(v-if="!selectOptions.hideNoneSelectedText", :disabled="schema.required", :value="null", :selected="value == undefined") {{ selectOptions.noneSelectedText || "<Nothing selected>" }}
|
4 |
| - option(v-for="item in items", :value="getItemValue(item)") {{ getItemName(item) }} |
| 4 | + |
| 5 | + template(v-for="item in items") |
| 6 | + optgroup(v-if="schema.group && item.group", :label="getGroupName(item)") |
| 7 | + option(v-if="schema.group && item.ops", v-for="i in item.ops", :value="getItemValue(i)") {{ getItemName(i) }} |
| 8 | + |
| 9 | + option(v-if="!item.group", :value="getItemValue(item)") {{ getItemName(item) }} |
5 | 10 | </template>
|
6 | 11 |
|
7 | 12 | <script>
|
8 |
| - import {isObject} from "lodash"; |
| 13 | + import {isObject, find} from "lodash"; |
9 | 14 | import abstractField from "../abstractField";
|
10 | 15 |
|
11 | 16 | export default {
|
|
21 | 26 | if (typeof(values) == "function") {
|
22 | 27 | return values.apply(this, [this.model, this.schema]);
|
23 | 28 | } else
|
| 29 | +
|
| 30 | + if(this.schema.group){ |
| 31 | + let array = []; |
| 32 | + let arrayElement = {}; |
| 33 | +
|
| 34 | + values.forEach((item) => { |
| 35 | +
|
| 36 | + arrayElement = null; |
| 37 | +
|
| 38 | + if(item.group){ |
| 39 | + // There is in a group. |
| 40 | + |
| 41 | + // Find element with this group. |
| 42 | + arrayElement = find(array, i => {return i.group == item.group}); |
| 43 | +
|
| 44 | + if(arrayElement){ |
| 45 | + // There is such a group. |
| 46 | +
|
| 47 | + arrayElement.ops.push({ |
| 48 | + id: item.id, |
| 49 | + name: item.name |
| 50 | + }); |
| 51 | + }else{ |
| 52 | + // There is not such a group. |
| 53 | + |
| 54 | + // Initialising. |
| 55 | + arrayElement = { |
| 56 | + group:"", |
| 57 | + ops:[] |
| 58 | + }; |
| 59 | +
|
| 60 | + // Set group. |
| 61 | + arrayElement.group = item.group; |
| 62 | +
|
| 63 | + // Set Group element. |
| 64 | + arrayElement.ops.push({ |
| 65 | + id: item.id, |
| 66 | + name: item.name |
| 67 | + }); |
| 68 | +
|
| 69 | + // Add array. |
| 70 | + array.push(arrayElement); |
| 71 | + } |
| 72 | + }else{ |
| 73 | + // There is not in a group. |
| 74 | + array.push(item); |
| 75 | + } |
| 76 | + }); |
| 77 | +
|
| 78 | + // With Groups. |
| 79 | + return array; |
| 80 | + } |
| 81 | +
|
| 82 | + // Without Group. |
24 | 83 | return values;
|
25 |
| - } |
| 84 | + } |
26 | 85 | },
|
27 | 86 |
|
28 | 87 | methods: {
|
| 88 | +
|
| 89 | + getGroupName(item){ |
| 90 | + if(item && item.group){ |
| 91 | + return item.group; |
| 92 | + } |
| 93 | +
|
| 94 | + throw "Group name is missing! https://icebob.gitbooks.io/vueformgenerator/content/fields/select.html#select-field-with-object-items"; |
| 95 | + }, |
| 96 | +
|
29 | 97 | getItemValue(item) {
|
30 | 98 | if (isObject(item)){
|
31 | 99 | if (typeof this.schema["selectOptions"] !== "undefined" && typeof this.schema["selectOptions"]["value"] !== "undefined") {
|
|
0 commit comments