Skip to content

Commit be09146

Browse files
author
Lionel Bijaoui
committed
checklist field now support array of string or array of objects (with name and value properties instead of name and id) by default. If checklistOptions is defined, these key can be replaced by other name. Little change to the error message in radios fields. Both fields are harmonized.
1 parent 0f48c30 commit be09146

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

src/fields/core/fieldChecklist.vue

+35-18
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<template lang="pug">
22
.wrapper
33
.listbox.form-control(v-if="schema.listBox", :disabled="disabled")
4-
.list-row(v-for="item in items", :class="{'is-checked': getItemIsChecked(item)}")
4+
.list-row(v-for="item in items", :class="{'is-checked': isItemChecked(item)}")
55
label
6-
input(type="checkbox", :checked="getItemIsChecked(item)", :disabled="disabled", @input="onChanged($event, item)")
6+
input(type="checkbox", :checked="isItemChecked(item)", :disabled="disabled", @input="onChanged($event, item)")
77
| {{ getItemName(item) }}
88

99
.combobox.form-control(v-if="!schema.listBox", :disabled="disabled")
@@ -12,9 +12,9 @@
1212
.arrow
1313

1414
.dropList
15-
.list-row(v-if="comboExpanded", v-for="item in items", :class="{'is-checked': getItemIsChecked(item)}")
15+
.list-row(v-if="comboExpanded", v-for="item in items", :class="{'is-checked': isItemChecked(item)}")
1616
label
17-
input(type="checkbox", :checked="getItemIsChecked(item)", :disabled="disabled", @input="onChanged($event, item)")
17+
input(type="checkbox", :checked="isItemChecked(item)", :disabled="disabled", @input="onChanged($event, item)")
1818
| {{ getItemName(item) }}
1919
</template>
2020

@@ -49,22 +49,39 @@
4949
},
5050
5151
methods: {
52-
getItemID(item) {
53-
if (isObject(item) && item.id)
54-
return item.id;
55-
56-
return item;
52+
getItemValue(item) {
53+
if (isObject(item)){
54+
if (typeof this.schema["checklistOptions"] !== "undefined" && typeof this.schema["checklistOptions"]["value"] !== "undefined") {
55+
return item[this.schema.checklistOptions.value];
56+
} else {
57+
if (typeof item["value"] !== "undefined") {
58+
return item.value
59+
} else {
60+
throw "value is not defined. If you want to use another key name, add a `value` property under `checklistOptions` in the schema. https://icebob.gitbooks.io/vueformgenerator/content/fields/checklist.html#checklist-field-with-object-values";
61+
}
62+
}
63+
} else {
64+
return item;
65+
}
5766
},
58-
5967
getItemName(item) {
60-
if (isObject(item) && item.name)
61-
return item.name;
62-
63-
return item;
68+
if (isObject(item)){
69+
if (typeof this.schema["checklistOptions"] !== "undefined" && typeof this.schema["checklistOptions"]["name"] !== "undefined") {
70+
return item[this.schema.checklistOptions.name];
71+
} else {
72+
if (typeof item["name"] !== "undefined") {
73+
return item.name
74+
} else {
75+
throw "name is not defined. If you want to use another key name, add a `name` property under `checklistOptions` in the schema. https://icebob.gitbooks.io/vueformgenerator/content/fields/checklist.html#checklist-field-with-object-values";
76+
}
77+
}
78+
} else {
79+
return item;
80+
}
6481
},
6582
66-
getItemIsChecked(item) {
67-
return (this.value && this.value.indexOf(this.getItemID(item)) != -1);
83+
isItemChecked(item) {
84+
return (this.value && this.value.indexOf(this.getItemValue(item)) != -1);
6885
},
6986
7087
onChanged(event, item) {
@@ -73,9 +90,9 @@
7390
}
7491
7592
if (event.target.checked) {
76-
this.value.push(this.getItemID(item));
93+
this.value.push(this.getItemValue(item));
7794
} else {
78-
this.value.splice(this.value.indexOf(this.getItemID(item)), 1);
95+
this.value.splice(this.value.indexOf(this.getItemValue(item)), 1);
7996
}
8097
},
8198

src/fields/core/fieldRadios.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
if (typeof item["value"] !== "undefined") {
3737
return item.value
3838
} else {
39-
throw "value is not defined.\r If you want to use another key name, add a `value` property under `radiosOptions` in the schema.\r https://icebob.gitbooks.io/vueformgenerator/content/fields/radios.html#radios-field-with-object-values";
39+
throw "value is not defined. If you want to use another key name, add a `value` property under `radiosOptions` in the schema. https://icebob.gitbooks.io/vueformgenerator/content/fields/radios.html#radios-field-with-object-values";
4040
}
4141
}
4242
} else {
@@ -51,7 +51,7 @@
5151
if (typeof item["name"] !== "undefined") {
5252
return item.name
5353
} else {
54-
throw "name is not defined.\r If you want to use another key name, add a `name` property under `radiosOptions` in the schema.\r https://icebob.gitbooks.io/vueformgenerator/content/fields/radios.html#radios-field-with-object-values";
54+
throw "name is not defined. If you want to use another key name, add a `name` property under `radiosOptions` in the schema. https://icebob.gitbooks.io/vueformgenerator/content/fields/radios.html#radios-field-with-object-values";
5555
}
5656
}
5757
} else {

0 commit comments

Comments
 (0)