60 lines
No EOL
2.1 KiB
HTML
60 lines
No EOL
2.1 KiB
HTML
<button type="submit" class="btn btn-primary" id="submit">Save</button>
|
|
<script>
|
|
function formToJson(form) {
|
|
const fd = new FormData(form);
|
|
const out = {};
|
|
|
|
fd.forEach((value, key) => {
|
|
if (key in out) {
|
|
if (!Array.isArray(out[key])) out[key] = [out[key]];
|
|
out[key].push(value);
|
|
} else {
|
|
out[key] = value;
|
|
}
|
|
});
|
|
|
|
form.querySelectorAll('input[type="checkbox"], input[type="radio"]').forEach(el => {
|
|
if (!el.name) return;
|
|
|
|
if (el.type === 'radio') {
|
|
if (out[el.name] !== undefined) return;
|
|
const checked = form.querySelector(`input[type="radio"][name="${CSS.escape(el.name)}"]:checked`);
|
|
if (checked) out[el.name] = checked.value ?? true;
|
|
}
|
|
|
|
if (el.type === 'checkbox') {
|
|
const group = form.querySelectorAll(`input[type="checkbox"][name="${CSS.escape(el.name)}"]`);
|
|
if (group.length > 1) {
|
|
const checkedVals = Array.from(group)
|
|
.filter(i => i.checked)
|
|
.map(i => i.value ?? true);
|
|
out[el.name] = checkedVals;
|
|
} else {
|
|
out[el.name] = el.checked;
|
|
}
|
|
}
|
|
});
|
|
|
|
return out;
|
|
}
|
|
|
|
document.getElementById("{{ field['attrs']['data-model'] }}_form").addEventListener("submit", async e => {
|
|
e.preventDefault();
|
|
const json = formToJson(e.target);
|
|
|
|
response = await fetch("{{ url_for('entry.update_entry', id=field['template_ctx']['values']['id'], model=field['attrs']['data-model']) }}", {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(json)
|
|
});
|
|
|
|
reply = await response.json();
|
|
if (reply['status'] === 'success') {
|
|
console.log("WELL DONE!")
|
|
} else {
|
|
console.log("YOU HAVE FAILED!")
|
|
}
|
|
});
|
|
</script> |