I have trying to make a functionality for adding, cloning and deleting of table row and column. My table has 4 columns initially.
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
I have mostly made the functionality. But, I'm facing an issue at the time of adding row. The main functionality of adding row I have written is:
if there is any existing row
Clone last Row and add this clone row after the last row
else
Add a row with 4 columns
Well, I am adding 4 columns inside else{}. Because, initially my table has 4 columns. This should be fine. But, the problem is there are options for deleting/adding column too. For example, If anybody delete a column, then total number of column will be 3. Then if anybody accidentally delete all the row, then try to add a row again, it will add a new row with 4 columns where the table has now 3 columns. To avoid this kind of situation, I should not add static 4 columns inside else {} But, I don't understand how to handle this issue. Please, help me to fix this issue.
****************************Update**************************
After seeing some solution, I think my problem is not clear to you.
(1) Well, consider there are 4 columns:

(2) After removing a column, there will be 3 columns now:

(3) Removing one more, there are 2 columns now:

(4) Now, let's remove all the rows:

(5) Now, let's try to add a new row:

Because, I always adding 4 fixed columns when there in no row for cloning (inside else{}). So, after total number of columns changing, when there is no row for cloning, it can't create a new row with exact column number.
Problematic jQuery:
$('body').on('click', '.add-row', function() {
var tr = $(this).parents('.table-content').find('.table tbody tr:last');
if(tr.length > 0) {
var clone = tr.clone();
clone.find(':text').val('');
tr.after(clone);
} else {
$(this).parents('.table-content').find('.table tbody').append('<tr> <td><span class="remove remove-row">x</span></td><td> <input type="text" class="form-control"> </td><td> static element </td><td> static element </td></tr>');
}
});