From 3e3548d14dff43c0275756552ebfd32499e0a9da Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Tue, 10 Mar 2015 17:24:59 +0100 Subject: [PATCH] add frontend support for multi select boxes --- .../views/layout_partials/form_input_tr.volt | 2 ++ src/opnsense/www/js/opnsense.js | 36 ++++++++++++++++--- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/opnsense/mvc/app/views/layout_partials/form_input_tr.volt b/src/opnsense/mvc/app/views/layout_partials/form_input_tr.volt index 6b0313b3a..1c2dae4e7 100644 --- a/src/opnsense/mvc/app/views/layout_partials/form_input_tr.volt +++ b/src/opnsense/mvc/app/views/layout_partials/form_input_tr.volt @@ -10,6 +10,8 @@ {% elseif type == "checkbox" %} + {% elseif type == "select_multiple" %} + {% endif %} {% if help|default(false) %} diff --git a/src/opnsense/www/js/opnsense.js b/src/opnsense/www/js/opnsense.js index 2fa28b2cd..47c3cb6f0 100644 --- a/src/opnsense/www/js/opnsense.js +++ b/src/opnsense/www/js/opnsense.js @@ -7,7 +7,7 @@ function getFormData(parent) { data = {}; - $( "#"+parent+" input" ).each(function( index ) { + $( "#"+parent+" input,select" ).each(function( index ) { node = data ; keyparts = $(this).attr('id').split('.'); for (var i in keyparts) { @@ -17,13 +17,25 @@ function getFormData(parent) { if (i < keyparts.length - 1 ) { node = node[keyparts[i]]; } else { - if ($(this).prop("type") == "checkbox") { + if ($(this).is("select")) { + // selectbox, collect selected items + var tmp_str = ""; + $(this).children().each(function(index){ + if ($(this).prop("selected")){ + if (tmp_str != "") tmp_str = tmp_str + ","; + tmp_str = tmp_str + $(this).val(); + } + node[keyparts[i]] = tmp_str; + }); + } else if ($(this).prop("type") == "checkbox") { + // checkbox input type if ($(this).prop("checked")) { node[keyparts[i]] = 1 ; } else { node[keyparts[i]] = 0 ; } } else { + // regular input type node[keyparts[i]] = $(this).val(); } } @@ -46,7 +58,9 @@ function getFormData(parent) { * @param data named array structure */ function setFormData(parent,data) { - $( "#"+parent+" input" ).each(function( index ) { + //alert( JSON.stringify(data['general']['interfaces']) ); + + $( "#"+parent+" input,select" ).each(function( index ) { node = data ; keyparts = $(this).attr('id').split('.'); for (var i in keyparts) { @@ -56,14 +70,26 @@ function setFormData(parent,data) { if (i < keyparts.length - 1 ) { node = node[keyparts[i]]; } else { - if ($(this).prop("type") == "checkbox") { + // data node found, handle per type + if ($(this).is("select")) { + // handle select boxes + $(this).empty(); // flush + for (var key in node[keyparts[i]]) { + if (node[keyparts[i]][key]["selected"] != "0") { + $(this).append(""); + } else { + $(this).append(""); + } + } + } else if ($(this).prop("type") == "checkbox") { + // checkbox type if (node[keyparts[i]] != 0) { $(this).prop("checked",true) ; } else { $(this).prop("checked",false) ; } - } else { + // regular input type $(this).val(node[keyparts[i]]); } }