Skip to content

Commit 07bbb07

Browse files
committed
Update selection field with group options.
1 parent 5099907 commit 07bbb07

File tree

1 file changed

+71
-3
lines changed

1 file changed

+71
-3
lines changed

src/fields/core/fieldSelect.vue

+71-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
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="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) }}
510
</template>
611

712
<script>
8-
import {isObject} from "lodash";
13+
import {isObject, find} from "lodash";
914
import abstractField from "../abstractField";
1015
1116
export default {
@@ -21,11 +26,74 @@
2126
if (typeof(values) == "function") {
2227
return values.apply(this, [this.model, this.schema]);
2328
} 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.
2483
return values;
25-
}
84+
}
2685
},
2786
2887
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+
2997
getItemValue(item) {
3098
if (isObject(item)){
3199
if (typeof this.schema["selectOptions"] !== "undefined" && typeof this.schema["selectOptions"]["value"] !== "undefined") {

0 commit comments

Comments
 (0)