Commit 3e3548d1 authored by Ad Schellevis's avatar Ad Schellevis

add frontend support for multi select boxes

parent 0a92399b
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
<input type="text" class="form-control" size="{{size|default("50")}}" id="{{ id }}" > <input type="text" class="form-control" size="{{size|default("50")}}" id="{{ id }}" >
{% elseif type == "checkbox" %} {% elseif type == "checkbox" %}
<input type="checkbox" id="{{ id }}" > <input type="checkbox" id="{{ id }}" >
{% elseif type == "select_multiple" %}
<select multiple="multiple" size="{{size|default(2)}}" id="{{ id }}"></select>
{% endif %} {% endif %}
{% if help|default(false) %} {% if help|default(false) %}
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
function getFormData(parent) { function getFormData(parent) {
data = {}; data = {};
$( "#"+parent+" input" ).each(function( index ) { $( "#"+parent+" input,select" ).each(function( index ) {
node = data ; node = data ;
keyparts = $(this).attr('id').split('.'); keyparts = $(this).attr('id').split('.');
for (var i in keyparts) { for (var i in keyparts) {
...@@ -17,13 +17,25 @@ function getFormData(parent) { ...@@ -17,13 +17,25 @@ function getFormData(parent) {
if (i < keyparts.length - 1 ) { if (i < keyparts.length - 1 ) {
node = node[keyparts[i]]; node = node[keyparts[i]];
} else { } 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")) { if ($(this).prop("checked")) {
node[keyparts[i]] = 1 ; node[keyparts[i]] = 1 ;
} else { } else {
node[keyparts[i]] = 0 ; node[keyparts[i]] = 0 ;
} }
} else { } else {
// regular input type
node[keyparts[i]] = $(this).val(); node[keyparts[i]] = $(this).val();
} }
} }
...@@ -46,7 +58,9 @@ function getFormData(parent) { ...@@ -46,7 +58,9 @@ function getFormData(parent) {
* @param data named array structure * @param data named array structure
*/ */
function setFormData(parent,data) { function setFormData(parent,data) {
$( "#"+parent+" input" ).each(function( index ) { //alert( JSON.stringify(data['general']['interfaces']) );
$( "#"+parent+" input,select" ).each(function( index ) {
node = data ; node = data ;
keyparts = $(this).attr('id').split('.'); keyparts = $(this).attr('id').split('.');
for (var i in keyparts) { for (var i in keyparts) {
...@@ -56,14 +70,26 @@ function setFormData(parent,data) { ...@@ -56,14 +70,26 @@ function setFormData(parent,data) {
if (i < keyparts.length - 1 ) { if (i < keyparts.length - 1 ) {
node = node[keyparts[i]]; node = node[keyparts[i]];
} else { } 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("<option value='"+key+"' selected>" + node[keyparts[i]][key]["value"] + " </option>");
} else {
$(this).append("<option value='"+key+"'>" + node[keyparts[i]][key]["value"] + " </option>");
}
}
} else if ($(this).prop("type") == "checkbox") {
// checkbox type
if (node[keyparts[i]] != 0) { if (node[keyparts[i]] != 0) {
$(this).prop("checked",true) ; $(this).prop("checked",true) ;
} else { } else {
$(this).prop("checked",false) ; $(this).prop("checked",false) ;
} }
} else { } else {
// regular input type
$(this).val(node[keyparts[i]]); $(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