Search code examples
jquery-jtable

Adding fields dynamically in JQuery-Jtable


How can I add fields dynamically in Jtable. I want to have multiple values for Cities Please Refer the image attached

Thanksenter image description here


Solution

  • Yes this is not built-in with jQuery jTable. To deal with this I've created a script for the same purpose. This handles (a) adding more controls OR group of controls and (b) remove control(s).

    Here is the script:

    //add '.add_more' class to
    $(".add_more").on('click', function () {
    
        // creates unique id for each element
        var _uniqueid_ = +Math.floor(Math.random() * 1000000);
        var new_ele_id = $(this).attr("data-clone-target") + _uniqueid_;
    
        var cloneObj = $("#" + $(this).attr("data-clone-target"))
        .clone()
        .val('')
        .attr("id", new_ele_id);
    
        // if the control is grouped control
        if ($(this).hasClass('group_control') == true) {
            $($(cloneObj).children()).each(function () {
                $(this).attr("id", $(this).attr("id") + _uniqueid_).val("");
            });
        }
    
        $(cloneObj).insertBefore($(this));
    
        //creates a 'remove' link for each created element or grouped element
        $("<a href='javascript:void(0);' class='remove_this' data-target-id='" + new_ele_id + "'></a>")
        .on('click', function () {
            if ($(this).is(":visible") == true) {
                if (confirm("Are you sure?")) {
                    $("#" + $(this).attr("data-target-id")).remove();
                    $(this).remove();
                }
            }
            else {
                $("#" + $(this).attr("data-target-id")).remove();
                $(this).remove();
            }
    
        }).insertBefore($(this));
        $("#" + new_ele_id).focus();
    
    
    
    });
    
    //remove element script
    $(".remove_this").on('click', function () {
        if ($(this).is(":visible") == true) {
            if (confirm("Are you sure?")) {
                $("#" + $(this).attr("data-target-id")).remove();
                $(this).remove();
            }
        }
        else {
            $("#" + $(this).attr("data-target-id")).remove();
            $(this).remove();
        }
    });
    

    Usage: Single Element http://jsfiddle.net/vkscorpion1986/ktbn4qLg/2/

    <input class="" id="<ELEMENT-ID>" type="text" name="input1">
    <a href="javascript:void(0);" data-clone-target="<ELEMENT-ID>" title="Add More" class="add_more">Add More</a>
    

    Usage: Grouped Elements http://jsfiddle.net/vkscorpion1986/ktbn4qLg/4/

    <div id="<ELEMENT-ID>">
        <input class="" id="input1" type="text" name="input1">
        <input class="" id="input2" type="text" name="input2">
    </div>
    <a href="javascript:void(0);" data-clone-target="<ELEMENT-ID>" title="Add More" class="add_more group_control">Add More</a>
    

    attributes

    href = javascript:void(0); // just to disable the anchor tag default behaviour 
    data-clone-target = id of the target element
    

    css classes

    .add_more = to implement the add more/remove controls functionality
    .group_control  = for indicating that this is group of elements which have to be repeted
    

    Hope this works for you.