Ali Alhamaly Ali Alhamaly - 3 months ago 23
jQuery Question

Append button to a table row dynamically using jquery

I have an array of JavaScript JSON objects which is being parsed from a JSON formatted-string. Now what I'm doing is that I'm looping through the array and append the data to a table within the page.

jQuery Code:



$.each(objArr, function(key, value) {
var tr = $("<tr />");
$.each(value, function(k, v) {
tr.append($("<td />", {
html: v
}));
$("#dataTable").append(tr);
})
})


The code works perfectly and the table is getting populated successfully

But what I'm looking for is that, I want to add a delete button at the end of the row, by which the user will delete the row, and it also important to handle the click event in order to perform the required action

I've done this in another way, but it is not that efficient, I want to use the code above to accomplish that as it is more efficient:

for (var i = 0; i < objArr.length; i++) {
var tr = "<tr>";
var td1 = "<td>" + objArr[i]["empID"] + "</td>";
var td2 = "<td>" + objArr[i]["fname"] + "</td>";
var td3 = "<td>" + objArr[i]["lname"] + "</td>";
var td4 = "<td>" + objArr[i]["phone"] + "</td>";
var td5 = "<td>" + objArr[i]["address"] + "</td>";
var td6 = "<td >" + objArr[i]["deptID"] + "</td>";
var td7 = "<td>" + objArr[i]["email"] + "</td>";
var td8 = "<td>" + '<button id="' + objArr[i]["empID"] + '" value="' + objArr[
i]["empID"] + '" onclick="onClickDelete(' + objArr[i]["empID"] +
')">Delete</button>' + "</td></tr>";
$("#dataTable").append(tr + td1 + td2 + td3 + td4 + td5 + td6 + td7 + td8);
}


Any suggestions please?

Answer

Try this, i've include event handler for the each buttons inside the table.


CHANGES:

  • Adding Event Listener for each buttons inside the table.
  • Call method (function) with parameters.

Note: I am using, fadeOut method for fading purposes only. So you can see the changes. You can change the script as your need.

$(document).ready(function() {
  var jsonData = [
    {id: 'A01', name: 'Fadhly'},
    {id: 'A02', name: 'Permata'},
    {id: 'A03', name: 'Haura'},
    {id: 'A04', name: 'Aira'}
  ];

  $.each(jsonData, function(key, val) {
    var tr = '<tr>';
  
    tr += '<td>' + (key + 1) + '</td>';
    tr += '<td>' + val.id + '</td>';
    tr += '<td>' + val.name + '</td>';
  
    tr += '<td><button class="delete" data-key="'+ (key + 1) +'">Delete</button></td>';
  
    tr += '</tr>';
  
    $('tbody').append(tr);
  });
  
  $('button.delete').on('click', function() {
    var cRow = $(this).parents('tr');
    var cId = $('td:nth-child(2)', cRow).text();
    var intKey = $(this).data('key');
    
    cRow.fadeOut('slow', function() {
      doDelete(cId, intKey);
    });
  });
  
  function doDelete(param1, param2) {
    alert('Data with\n\nID: [' + param1 + ']\nKey: [' + param2 + ']\n\nRemoved.');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table border="1" width="100%">
  <thead>
    <tr>
      <th>#</th>
      <th>Id</th>
      <th>Name</th>
      <th>Action</th>
    </tr>
  </thead>
  
  <tbody>
  </tbody>
</table>