-
Notifications
You must be signed in to change notification settings - Fork 0
/
summary.js
32 lines (28 loc) · 1.02 KB
/
summary.js
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
function watchOn(element, dynForm, index, bindProperty) {
dynForm.addEventListener('change', (e) => {
if (!e.detail) return;
const selectedOption = Array.from(e.detail.selectedOptions)[index];
let value;
if (!bindProperty) {
value = selectedOption.value;
} else
if (bindProperty == 'text') {
value = selectedOption.innerHTML;
} else
{
value = selectedOption.getAttribute(bindProperty);
}
element.innerHTML = value;
});
}
const summaryFields = document.querySelectorAll('[summary]');
const dynForm = document.querySelector('[dyn-form]');
summaryFields.forEach((summaryField) => {
const index = summaryField.getAttribute("dyn-index");
const bindProperty = summaryField.getAttribute("dyn-bind");
if (!index || !bindProperty) {
console.error("When using the [summary] attribute please specify [dyn-index] attr (the select field index) and [dyn-bind] attr (to specify what value should be displayed)");
return;
}
watchOn(summaryField, dynForm, index, bindProperty);
});