简体   繁体   中英

How to set the data from server into this dynamic form?

I am using this bootsnipp for BootStrap.JS to create dynamic fields and capture data from the form.

After save, lets say I get back data as JSON from server as key value pairs, how I can construct this dynamic form so the user can update it again.

Any sample snippet would be helpful.

All you have to do is to write some data propagating method, rather easy, here's a quickie (Data from server are stored in variable requestData , also I removed unnecessary code):

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);

EDIT

well, all the code to reset the form is hidden inside propagate data, you just have to reverse it, in your html you should add something like this <a href="#" class="btn btn-warning form-reset" style="margin: 10px;">Reset</a> and then the complete js is here:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM