Permalink
Browse files

fixed checkbox and added radioButtonList

  • Loading branch information...
1 parent 834fe3f commit 3bba2bf9260e31e8d9486770ea1e198e2691e813 @janez89 committed Aug 28, 2013
Showing with 20 additions and 7 deletions.
  1. +20 −7 lib/form.js
View
@@ -67,19 +67,22 @@ module.exports = function(fn) {
}
var output = '<select'+ fn.toAttr(fn.mergeObject({name: name}, opts)) +'>\n'+ empty;
-
+
if (value) {
+ // json elements in array version
for (var i in elements) {
var el = elements[i];
if (el.hasOwnProperty(value) && el.hasOwnProperty(label)) {
output += '\t<option'+ fn.toAttr({ value: el[value], selected: el[value] == val ? 'selected' : null }) +'>'+ el[label] +'</option>\n';
}
}
} else if (Array.isArray(elements)) {
+ // simple array
for (var i in elements) {
output += '\t<option'+ fn.toAttr({ value: i, selected: i == val ? 'selected' : null }) +'>'+ elements[i] +'</option>\n';
}
} else {
+ // simple json
for (var i in elements) {
if (elements.hasOwnProperty(i)) {
output += '\t<option'+ fn.toAttr({ value: i, selected: i == val ? 'selected' : null }) +'>'+ elements[i] +'</option>\n';
@@ -102,28 +105,38 @@ module.exports = function(fn) {
throw new Error('Not implemented method!');
};
- form.radioButtonList = function (name, value, list, opts) {
- throw new Error('Not implemented method!');
- };
-
form.checkBox = function (name, checked, opts) {
opts = opts || {};
opts.type = 'checkbox';
opts.name = name;
+ opts.value= 1;
opts.checked = checked ? 'checked': null;
return form.inputField(opts);
};
- form.radioButton = function (name, selected, opts) {
+ form.radioButton = function (name, val, opts) {
opts = opts || {};
opts.type = 'radio';
opts.name = name;
- opts.checked = checked ? 'checked': null;
+ opts.value = val || '';
+ opts.checked = opts.checked ? 'checked': null;
return form.inputField(opts);
};
+ form.radioButtonList = function (name, value, list, opts) {
+ opts = opts || {};
+ var template = opts.template || '{input} {label}';
+ delete opts.template;
+ var out = '';
+ for (var val in list) {
+ label = list[val];
+ out += template.replace('{input}', form.radioButton(name, val, { checked: val == value })).replace('{label}', label)+'\n';
+ }
+ return out;
+ };
+
form.fileField = function (name, opts) {
opts = opts || {};
opts.type = 'file';

0 comments on commit 3bba2bf

Please sign in to comment.