Commit f4055efd authored by Ad Schellevis's avatar Ad Schellevis

(mvc) use $.each for array iteration.... (fix for https://github.com/opnsense/core/issues/195 )

parent 55bee2fd
...@@ -45,38 +45,39 @@ function getFormData(parent) { ...@@ -45,38 +45,39 @@ function getFormData(parent) {
// we need an id. // we need an id.
return; return;
} }
var node = data ; var node = data ; // target node
var keyparts = $(this).prop('id').split('.'); var sourceNode = $(this); // document node to fetch data from
for (var i in keyparts) { var keyparts = sourceNode.prop('id').split('.');
if (!(keyparts[i] in node)) { $.each(keyparts,function(indx,keypart){
node[keyparts[i]] = {}; if (!(keypart in node)) {
node[keypart] = {};
} }
if (i < keyparts.length - 1 ) { if (indx < keyparts.length - 1 ) {
node = node[keyparts[i]]; node = node[keypart];
} else { } else {
if ($(this).is("select")) { if (sourceNode.is("select")) {
// selectbox, collect selected items // selectbox, collect selected items
var tmp_str = ""; var tmp_str = "";
$(this).children().each(function(index){ sourceNode.children().each(function(index){
if ($(this).prop("selected")){ if ($(this).prop("selected")){
if (tmp_str != "") tmp_str = tmp_str + ","; if (tmp_str != "") tmp_str = tmp_str + ",";
tmp_str = tmp_str + $(this).val(); tmp_str = tmp_str + sourceNode.val();
} }
}); });
node[keyparts[i]] = tmp_str; node[keypart] = tmp_str;
} else if ($(this).prop("type") == "checkbox") { } else if (sourceNode.prop("type") == "checkbox") {
// checkbox input type // checkbox input type
if ($(this).prop("checked")) { if (sourceNode.prop("checked")) {
node[keyparts[i]] = 1 ; node[keypart] = 1 ;
} else { } else {
node[keyparts[i]] = 0 ; node[keypart] = 0 ;
} }
} else { } else {
// regular input type // regular input type
node[keyparts[i]] = $(this).val(); node[keypart] = sourceNode.val();
} }
} }
} });
}); });
return data; return data;
...@@ -101,38 +102,38 @@ function setFormData(parent,data) { ...@@ -101,38 +102,38 @@ function setFormData(parent,data) {
return; return;
} }
var node = data ; var node = data ;
var targetNode = $(this); // document node to fetch data to
var keyparts = $(this).prop('id').split('.'); var keyparts = $(this).prop('id').split('.');
for (var i in keyparts) { $.each(keyparts,function(indx,keypart){
if (!(keyparts[i] in node)) { if (keypart in node) {
break; if (indx < keyparts.length - 1 ) {
} node = node[keypart];
if (i < keyparts.length - 1 ) { } else {
node = node[keyparts[i]]; // data node found, handle per type
} else { if (targetNode.is("select")) {
// data node found, handle per type // handle select boxes
if ($(this).is("select")) { targetNode.empty(); // flush
// handle select boxes $.each(node[keypart],function(indxItem, keyItem){
$(this).empty(); // flush if (keyItem["selected"] != "0") {
for (var key in node[keyparts[i]]) { targetNode.append("<option value='"+indxItem+"' selected>" + keyItem["value"] + " </option>");
if (node[keyparts[i]][key]["selected"] != "0") { } else {
$(this).append("<option value='"+key+"' selected>" + node[keyparts[i]][key]["value"] + " </option>"); targetNode.append("<option value='"+indxItem+"'>" + keyItem["value"] + " </option>");
}
});
} else if (targetNode.prop("type") == "checkbox") {
// checkbox type
if (node[keypart] != 0) {
targetNode.prop("checked",true) ;
} else { } else {
$(this).append("<option value='"+key+"'>" + node[keyparts[i]][key]["value"] + " </option>"); targetNode.prop("checked",false) ;
} }
}
} else if ($(this).prop("type") == "checkbox") {
// checkbox type
if (node[keyparts[i]] != 0) {
$(this).prop("checked",true) ;
} else { } else {
$(this).prop("checked",false) ; // regular input type
targetNode.val(node[keypart]);
} }
} else {
// regular input type
$(this).val(node[keyparts[i]]);
} }
} }
} });
}); });
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment