showDialog() now supports mutiple form fields in modal; removed some debug code

This commit is contained in:
eclipse 2025-05-08 22:47:37 +02:00
parent 201ca67409
commit 61c8a72e4f

View File

@ -14,7 +14,6 @@ function initDataTable(table_id) {
// create "New"-element and append it to the <div> containing the DataTables search field // create "New"-element and append it to the <div> containing the DataTables search field
function initCreateButton(opts) { function initCreateButton(opts) {
console.log(`initCreateButton: opts are ${JSON.stringify(opts)}`);
let a = document.createElement("a"); let a = document.createElement("a");
a.id = "create-button"; a.id = "create-button";
a.setAttribute("title", opts.title); a.setAttribute("title", opts.title);
@ -25,14 +24,23 @@ console.log(`initCreateButton: opts are ${JSON.stringify(opts)}`);
} }
function showDialog(opts) { function showDialog(opts) {
console.log(`showDialog: opts are ${JSON.stringify(opts)}`);
// if form action includes the string "update", the id at the end of the URL is a dummy and must be replaced with the correct id // if form action includes the string "update", the id at the end of the URL is a dummy and must be replaced with the correct id
if ( opts.url_id && opts.form_action.includes("update") ) { if ( opts.url_id && opts.form_action.includes("update") ) {
opts.form_action = opts.form_action.slice(0, opts.form_action.lastIndexOf("/") + 1) + opts.url_id; opts.form_action = opts.form_action.slice(0, opts.form_action.lastIndexOf("/") + 1) + opts.url_id;
} }
console.log(`showDialog: opts.form_action is ${opts.form_action}`); // if input id or input value is not an array, make it a single-element array
opts.input_id = Array.isArray(opts.input_id) ? opts.input_id : [ opts.input_id ];
if ( opts.input_value ) {
opts.input_value = Array.isArray(opts.input_value) ? opts.input_value : [ opts.input_value ];
} else {
opts.input_value = new Array(opts.input_id.length).fill("");
}
console.log(`id[] is ${JSON.stringify(opts.input_id)}, value[] is ${JSON.stringify(opts.input_value)}`);
document.getElementById("dialog-heading").textContent = opts.heading; document.getElementById("dialog-heading").textContent = opts.heading;
document.getElementById(opts.input_id).value = opts.input_value || ""; for (var i = 0; i < opts.input_id.length; i++ ) {
document.getElementById(opts.input_id[i]).value = opts.input_value[i] || "";
}
document.getElementById("form_submit").formAction = opts.form_action; document.getElementById("form_submit").formAction = opts.form_action;
document.getElementById(opts.modal_id).showModal(); document.getElementById(opts.modal_id).showModal();
} }