2

How can I add a dynamic row to the existing records in jQuery. I am getting some records in php. At the bottom there is an 'Add More Rows', which clones the last row. There are some existing calculations happening in the rows on a trigger like changing the selection will do some calculations, this newly created dynamic rows should adapt those calculations. Here in this case when I click Add New Rows, its adding the complete table. I just need the last row to append. Also the new created row is not taking any calculations. Anyone to guide me plsss? I have uploaded the script to the fiddle.

Fiddle

This is the script iam using to create dynamic rows:

$(window).load(function () {
    jQuery(document).ready(function ($) {
        $("#add_more").on('click', function (e) {
            e.preventDefault();
            var clone = $(".clone_row").eq(0).clone();
            $("#cart1").append(clone);
        });
        $("#submit").on('click', function (e) {
            e.preventDefault();
            alert($("#cart1").serialize());
        })
    })
});
6
  • div can't be placed inside tbody Commented Mar 18, 2016 at 11:11
  • var clone = $("#table tbody tr").last().clone(); $("#table").append(clone); Commented Mar 18, 2016 at 11:11
  • @Arun, the cloning part is fine now. Its appending the last row. Is there a way, the new appended rows takes the calculations also.? Commented Mar 18, 2016 at 11:18
  • I guess its not incrementing the array values [2]. is there a way, the newly appended rows takes the new array value[4]..[5] etc...? Commented Mar 18, 2016 at 11:26
  • Use event delegation stackoverflow.com/questions/203198/… Commented Mar 18, 2016 at 11:33

2 Answers 2

1

div can't be placed inside tbody.

Use following js

$("#add_more").on('click', function (e) {
    e.preventDefault();
    var clone = $("#table tbody tr:last").clone();
    $("#table tbody").append(clone);
});

UPDATE use delegated event for dynamically generated element like following.

$('#cart1').on('change', '.currency-selec', function (event, processing) {
    console.log("A row changed", processing, event);
    var row = $(this).closest('tr');
    var table = row.closest('table');
    var currency = $(this).val();
    if (processing) {
        // Calculate me
        calculateRowTotals($(row));
    } else {

        // Calculate other if not processing yet
        table.find("tr").each(function () {
            $(this).find("select").val(currency).trigger("change", 1);
        });
    }
});


$('#cart1').on('change', '#table :input', function () {
    var $tr = $(this).closest('tr'),
      $totInr = $tr.find('[name="total_inr[]"]'),
      unitprice = +$tr.find('[name="unitprice[]"]').val() || 0,
      qty = +$tr.find('[name="item_quantity_sup[]"]').val() || 0,
      $currency = $tr.find('[name="currency_change[]"] option:selected'),
      currency = $currency.val(),
      inr = $('#inrvalue').val();

    var total = unitprice * qty;
    $tr.find('[name="total[]"]').val(total);
})

UPDATED FIDDLE

Sign up to request clarification or add additional context in comments.

5 Comments

Its perfect now, its appending the last row. Is there a way, the new appended rows takes the calculations also.from the top. Which means if i change any currency there is some calculations taking place. But when i change the currency, the newly created rows is not taking the calculations
I guess its not incrementing the array values [2]. is there a way, the newly appended rows takes the new array value[4]..[5] etc...?
Its working fine now. Is there a way i can put Remove rows to the existing script? Really thank you for your time
What do you mean? @SanjuMenon
You are adding some Rows. In case if i need to delete any particular Rows? In each New Row created, i need a X mark, if i click that delete that particular row.
0

Yes. Azim is right. And if you want to clear values in new row. You can try this.

    var clone = $("#table tr:last").clone();
    $("#table").append(clone).find('tr:last input').val('');

1 Comment

Yeah that's fine, but the problem is this newly created rows is not taking the calculations. U can try to change any currency values from the dropdown. With the old data the newly created rows should also take the calculations from jquery.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.