0
function ViewTable(data)
     {

         var html = '<table>';
         html += '<tr>';

         for (var j in data[0]) {
             html += '<th>' + j + '</th>';
         }
         html += '</tr>';
         for (var i = 0; i < data.length; i++) {
             html += '<tr>';
             for (var j in data[i]) {

                 html += '<td>' + data[i][j] + '</td>';
             }

         }
         html += '</table>';
         document.getElementById('divRecords').innerHTML = html;

     }

I want to add edit and delete bottons on end of each row Please someone help me to get out of this.

1 Answer 1

1

The following should work.

    var html = '<table>';
     html += '<tr>';

     for (var j in data[0]) {
         html += '<th>' + j + '</th>';
     }
     html += '<th colspan="2">Actions</th>'
     html += '</tr>';
     for (var i = 0; i < data.length; i++) {
         html += '<tr>';
         for (var j in data[i]) {

             html += '<td>' + data[i][j] + '</td>';
         }
        html += '<td><a href="#">edit</td><td> | <a href="#">delete</td></tr>';
     }
     html += '</table>';
     document.getElementById('divRecords').innerHTML = html;

The sample data:

var data = [
  [1, 2],
  [3, 4],
  [5, 6]
];

The table appeared as follows

enter image description here

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

1 Comment

Thanks dear its really helpful.

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.