Fadly Dzil Fadly Dzil - 1 month ago 6
jQuery Question

Clone or copy from one tbody table into another tbody table using jquery

I have one table with selector class='container-items'.
Cause this table is have a lot form input, I just want to get them each value.

Then I clone or copy into another table in modal bootstrap,
The code like this :

$('#modal').on('show.bs.modal', function (e) {
// looping in container-table
$('.container-items > tbody tr').each(function (i, tr) {
var ini = $(this);
var baris = ini.closest('tr');
var item = ini.find('.item_id option:selected').text();
var by = ini.find('.karyawan_id option:selected').text();
var harga_jual = ini.find('.harga-jual').val();
var qty = ini.find('.jumlah-jual').val();
var subtotal = ini.find('.subtotal').val();
});

/*Copy it...*/
$('#table-struk > tbody').after(" ?");
});


Is it possible ? I so appreciated for the help.

Answer

See Below Code

 $('#modal').on('show.bs.modal', function (e) {
 // looping in container-table
 var html ='';
 $('.container-items > tbody tr').each(function (i, tr) {   
  var ini = $(this);
  var baris = ini.closest('tr');
  var item = ini.find('.item_id option:selected').text();
  var by = ini.find('.karyawan_id option:selected').text();
  var harga_jual = ini.find('.harga-jual').val();
  var qty = ini.find('.jumlah-jual').val();
  var subtotal = ini.find('.subtotal').val();
  html   += '<tr><td>'+item+'</td>' //You can use any html tags; 
         + '<td>'+by+'</td>'
         + '<td>'+harge_jual+'</td>' 
         + '<td>'+qty+'</td>'
         + '<td>'+subtotal+'</td></tr>';
 });

 /*Copy it...*/  
  $('#target_table tbody').append(html); //you can also huse html() if you overwrite it
 });
Comments