我正在使用this bootsnipp for BootStrap.JS来创建动态字段并从表单中捕获数据。
保存之后,让我说我从服务器获取数据作为键值对,我如何构建这个动态表单,以便用户可以再次更新它。
任何示例代码段都会有所帮助。
答案 0 :(得分:1)
您所要做的就是编写一些数据传播方法,相当简单,这里很简单(来自服务器的数据存储在变量requestData
中,我也删除了不必要的代码):
var requestData = [{"name":"phone0", "value":"890890809"},{"name":"fax1", "value":"1111"},{"name":"skype2", "value":"anonononono"},{"name":"email3", "value":"huhuhuhu@huhuhu.pl"}];
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
(function ($) {
$(function () {
var addFormGroup = function (event) {
[...]
};
var removeFormGroup = function (event) {
[...]
};
var selectFormGroup = function (event) {
[...]
}
var countFormGroup = function ($form) {
[...]
};
var collectData = function(ele) {
[...]
}
var propagateData = function(data){
var $formGroup = $('body').find('.form-group');
$.each(data, function(k,v){
var $formGroupClone = $formGroup.clone();
$formGroupClone.find('.btn-add').toggleClass('btn-success btn-add btn-danger btn-remove').html('–');
$formGroupClone.find('input').val(v.value);
$formGroupClone.find('.concept').text(capitalize(v.name.replace(/[0-9]/g, '')));
$formGroupClone.find('.input-group-select-val').val(v.name.replace(/[0-9]/g, ''));
$formGroupClone.insertBefore($formGroup);
});
}
$(document).on('click', '.btn-add', addFormGroup);
$(document).on('click', '.btn-remove', removeFormGroup);
$(document).on('click', '.dropdown-menu a', selectFormGroup);
propagateData(requestData);
$('#data').click(function(e){
collectData('.cache');
e.preventDefault();
});
});
})(jQuery);
好吧,重置表单的所有代码都隐藏在传播数据中,你只需要反转它,在你的html中你应该添加类似这样的<a href="#" class="btn btn-warning form-reset" style="margin: 10px;">Reset</a>
然后完整的js在这里:
var requestData = [{"name":"phone0", "value":"890890809"},{"name":"fax1", "value":"1111"},{"name":"skype2", "value":"anonononono"},{"name":"email3", "value":"huhuhuhu@huhuhu.pl"}];
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
(function ($) {
$(function () {
var addFormGroup = function (event) {
event.preventDefault();
var $formGroup = $(this).closest('.form-group');
var $multipleFormGroup = $formGroup.closest('.multiple-form-group');
var $formGroupClone = $formGroup.clone();
$(this)
.toggleClass('btn-success btn-add btn-danger btn-remove')
.html('–');
$formGroupClone.find('input').val('');
$formGroupClone.find('.concept').text('Phone');
$formGroupClone.insertAfter($formGroup);
var $lastFormGroupLast = $multipleFormGroup.find('.form-group:last');
if ($multipleFormGroup.data('max') <= countFormGroup($multipleFormGroup)) {
$lastFormGroupLast.find('.btn-add').attr('disabled', true);
}
};
var removeFormGroup = function (event) {
event.preventDefault();
var $formGroup = $(this).closest('.form-group');
var $multipleFormGroup = $formGroup.closest('.multiple-form-group');
var $lastFormGroupLast = $multipleFormGroup.find('.form-group:last');
if ($multipleFormGroup.data('max') >= countFormGroup($multipleFormGroup)) {
$lastFormGroupLast.find('.btn-add').attr('disabled', false);
}
$formGroup.remove();
};
var selectFormGroup = function (event) {
event.preventDefault();
var $selectGroup = $(this).closest('.input-group-select');
var param = $(this).attr("href").replace("#","");
var concept = $(this).text();
$selectGroup.find('.concept').text(concept);
$selectGroup.find('.input-group-select-val').val(param);
}
var countFormGroup = function ($form) {
return $form.find('.form-group').length;
};
/* collectData Function~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/
This function given a group of elements (in classic selector format) will assign each element
the values of the closest fields as a unique name and the user entered value (if any.)
Use the console to see it gather data.
*/
var collectData = function(ele) {
var $tgt = $(ele);
$tgt.each(function(index) {
// $tgt == $(this)
var $fieldNames = $(this).next('.input-group-select-val').val();
var $fieldValues = $(this).closest('.form-group').find('.form-control').val();
// Including index to each $fieldName in order to make each key/value pair unique.
$(this).attr('name', $fieldNames+index);
console.log('name: '+$(this).attr('name'));
$(this).val($fieldValues);
console.log('value: '+$(this).val());
});
}
var propagateData = function(data){
var $formGroup = $('body').find('.form-group');
$.each(data, function(k,v){
var $formGroupClone = $formGroup.clone();
$formGroupClone.find('.btn-add').toggleClass('btn-success btn-add btn-danger btn-remove').html('–');
$formGroupClone.find('input').val(v.value);
$formGroupClone.find('.concept').text(capitalize(v.name.replace(/[0-9]/g, '')));
$formGroupClone.find('.input-group-select-val').val(v.name.replace(/[0-9]/g, ''));
$formGroupClone.insertBefore($formGroup);
});
}
var clearData = function(elms){
$(elms).not(':last').remove();
var $el = $(elms);
$el.find('input').val('');
$el.find('.concept').text('Phone');
$el.find('.input-group-select-val').val('phone');
}
$(document).on('click', '.btn-add', addFormGroup);
$(document).on('click', '.btn-remove', removeFormGroup);
$(document).on('click', '.dropdown-menu a', selectFormGroup);
propagateData(requestData);
// Clicking the #data button will call the collectData('.cache') function.
$('#data').click(function(e){
collectData('.cache');
e.preventDefault();
});
$('.form-reset').click(function(e){
clearData('.multiple-form-group');
e.preventDefault();
});
});
})(jQuery);