1
I’m having trouble making an increment in a javascript function.
I have a table with the following structure:
<table id="products-table">
<tbody>
<tr>
<th>Produto</th>
<th>Código</th>
<th>Quantidade</th>
<th>Preço</th>
<th>Ações</th>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td>
<button onclick="RemoveTableRow(this)" type="button">Remover</button>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="5" style="text-align: left;">
<button onclick="AddTableRow()" type="button">Adicionar Produto</button>
</td>
</tr>
</tfoot>
</table>
The Removetablerow(this) function has the following commands:
(function($) {
remove = function(item) {
var tr = $(item).closest('tr');
tr.fadeOut(400, function() {
tr.remove();
});
return false;
}
})(jQuery);
The Addtablerow() function has the following content:
var inicio = 1;
var maximo = 5;
(function($) {
if (inicio <= maximo) {
AddTableRow = function() {
var newRow = $("<tr>");
var cols = "";
cols += '<td> </td>';
cols += '<td> </td>';
cols += '<td> </td>';
cols += '<td> </td>';
cols += '<td>';
cols += '<button onclick="RemoveTableRow(this)" type="button">Remover</button>';
cols += '</td>';
newRow.append(cols);
$("#products-table").append(newRow);
return false;
};
}
inicio++;
})(jQuery);
These two functions insert and remove dynamic lines from a table. I need to create a condition so that it is allowed to insert only 5 lines and that when a line is deleted the counter is updated.
At the beginning of the Addtablerow() function I declared two variables and made a loop but the "start" value is always 1 and does not increment.
+Tobias Mesquita thanks for the suggestion. I don’t know the Vuejs you indicated, as this project is almost finished I adopted another solution but I will use Vuejs in another project.
– Flávio Kowalske