MVC / form handling (opnsense.js) - regression in optgroup handling introduced in 9206823d60

Two issues found, when optgroup is not used and filled with an empty item, the length of the group seems to be 0. To retrieve the selected values, we should use val() as iterating the children of the select won't return the options anymore.
This commit is contained in:
Ad Schellevis 2023-10-18 20:27:57 +02:00
parent 3b2a0c9a75
commit 0d724ec850

View File

@ -77,14 +77,15 @@ function getFormData(parent) {
}
}
// selectbox, collect selected items
var tmp_str = "";
sourceNode.children().each(function(index){
if ($(this).prop("selected")){
if (tmp_str !== "") tmp_str = tmp_str + separator;
tmp_str = tmp_str + $(this).val();
}
});
node[keypart] = tmp_str;
if (!Array.isArray(sourceNode.val())) {
node[keypart] = sourceNode.val();
} else {
node[keypart] = "";
$.each(sourceNode.val(), function(idx, value){
if (node[keypart] !== "") node[keypart] = node[keypart] + separator;
node[keypart] = node[keypart] + value;
});
}
} else if (sourceNode.prop("type") === "checkbox") {
// checkbox input type
if (sourceNode.prop("checked")) {
@ -178,7 +179,7 @@ function setFormData(parent,data) {
});
}
for (const [group, items] of Object.entries(optgroups)) {
if (group == '' && optgroups.length == 1) {
if (group == '' && optgroups.length <= 1) {
targetNode.append(items);
} else {
targetNode.append($("<optgroup/>").attr('label', group).append(items));