Unable to clone table row with select2 correctly
Asked Answered
O

2

6

I have a form for Invoice where I need to add items as required. On clicking on Add button on respective row the row has been appended to previous row. But Select2 not working normally after that.

I have tried solution from SO in which destroy select before clone then again initialize it. But it is initializing two select2 after first row.

<tr class="tr_clone">
    <td>
        <div class="input select">
            <select required="required" id="Item" class="items select2-hidden-accessible" name="data[Invoice][item_id][]" tabindex="-1" aria-hidden="true">
                <option value=""></option>
                <option value="1">item 1</option>
                <option value="2">Item 2</option>
                <option value="3">Item 1+2</option>
                <option value="4">Set 1</option>
                <option value="5">NURSERY Books</option>
            </select><span class="select2 select2-container select2-container--default" dir="ltr" style="width: 231px;"><span class="selection"><span aria-expanded="false" aria-haspopup="true" aria-autocomplete="list" role="combobox" class="select2-selection select2-selection--single" tabindex="0" aria-labelledby="select2-Item-container"><span class="select2-selection__rendered" id="select2-Item-container" title=""></span><span role="presentation" class="select2-selection__arrow"><b role="presentation"></b></span></span>
            </span><span aria-hidden="true" class="dropdown-wrapper"></span></span>
        </div>
        <input type="hidden" id="ItemName" class="item_name" name="data[Invoice][item][]">
        <input type="hidden" id="ItemName" class="item_group_id" name="data[Invoice][item_group_id][]"> </td>
    <td>
        <div class="input text">
            <input type="number" required="" min="0" name="data[InvoiceDetail][quantity][]" class="quantity small" id="quantity][">
            <p class="quantity_a"></p>
        </div>
    </td>
    <td>
        <div class="input text">
            <input type="text" readonly="readonly" required="" name="data[InvoiceDetail][price][]" class="price small" id="price">
        </div>
    </td>
    <td>
        <div class="input text">
            <input type="text" readonly="readonly" required="" name="data[InvoiceDetail][unit][]" class="unit small" id="unit][">
        </div>
    </td>
    <td>
        <div class="input text">
            <input type="text" readonly="readonly" required="required" id="Amount" class="amount small" name="data[Invoice][amount][]">
        </div>
    </td>
    <td>
        <input type="button" class="tr_clone_add" value="Add" name="add">
    </td>
    <td class="remove">X</td>
</tr>

Jquery Code:

$("table").on('click','.tr_clone_add' ,function() {
        $('.items').select2("destroy");        
        var $tr = $(this).closest('.tr_clone');
        var $clone = $tr.clone();
        $clone.find(':text').val('');
        $tr.after($clone);      
        $('.items').select2();      
    });

Result:

enter image description here

Ostensory answered 23/3, 2016 at 12:25 Comment(7)
From what i know select2 will generate a div with the class items, try applying the select2() function on the select tag like this $("select.items").select2(). If that doesn't work try also removing the html generated by the library beforeDoerr
Destroy Select2, clone, and then reinitialize Select2.Fingerboard
@KevinBrown already tried it. But its not working fine. it gives the above (shown in image) resultSpies
@عثمان غني have you fix it?Nutritious
not yet. no one answered it correctlySpies
can you provide full jsfiddle?Denby
Actually my same code working fine in jsfiddle and also on a page where there is not another code. But In my page where I want to use it, it is giving error so trying to find the problem at that page. Thanks all for your time.Spies
O
0

Solved the problem using following code:

$("select.items").select2();
var a = 0;
$(".datepicker").datepicker({
    format: 'dd-mm-yyyy',
});
$("table").on('click','.tr_clone_add' ,function() {
    $(this).closest('.tr_clone').find(".items").select2("destroy");
    var $tr = $(this).closest('.tr_clone');
    var $clone = $tr.clone();
    $clone.find(':text').val('');
    $tr.after($clone);
    $("select.items").select2();
});

What I changed:

I just destroy the select2 on the row which I want to clone. Then initialize the select2 on all inputs with class items.

Ostensory answered 22/4, 2016 at 8:11 Comment(0)
D
9

try this

$('.items').select2();
$("table").on('click','.tr_clone_add' ,function() {
   $('.items').select2("destroy");        
   var $tr = $(this).closest('.tr_clone');
   var $clone = $tr.clone();
   $tr.after($clone);
   $('.items').select2();
   $clone.find('.items').select2('val', '');
});

JSFIDDLE example

/* EDIT */

If I removed from SELECT attribute id and css class select2-hidden-accessible and on bottom completly removed also select2 select2-container it seems to be working fine.

JSFIDDLE example with your row

Denby answered 9/4, 2016 at 13:8 Comment(1)
Actually my same code working fine in jsfiddle and also on a page where there is not another code. But In my page where I want to use it, it is giving error so trying to find the problem at that page. Thanks all for your time.Spies
O
0

Solved the problem using following code:

$("select.items").select2();
var a = 0;
$(".datepicker").datepicker({
    format: 'dd-mm-yyyy',
});
$("table").on('click','.tr_clone_add' ,function() {
    $(this).closest('.tr_clone').find(".items").select2("destroy");
    var $tr = $(this).closest('.tr_clone');
    var $clone = $tr.clone();
    $clone.find(':text').val('');
    $tr.after($clone);
    $("select.items").select2();
});

What I changed:

I just destroy the select2 on the row which I want to clone. Then initialize the select2 on all inputs with class items.

Ostensory answered 22/4, 2016 at 8:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.