Tengo el siguiente codigo donde elimino filas de una tabla, al momento que la tabla este vacia quiero habilitar el boton Agregar.
$(document).ready(function() {
$('#tbl_cotizacion').on('click', '.btn-danger', function(e) {
Swal.fire({
title: 'Are you sure?',
text: "Agregar Nueva Linea",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.value) {
$(this).closest('tr').remove();
addNewLine();
}
})
});
function addNewLine() {
$("#tbl_cotizacion tbody > tr").each(function() {
var tbody = $("#tbl_cotizacion tbody");
if (tbody.children().length == 0) {
alert('1');
$("#btn_save_line").attr("disabled", false);
return;
}
});
}
});
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@8"></script>
<button type="button" class="btn btn-primary" id="btn_save_line" disabled="disabled" >Agregar</button>
<table id="tbl_cotizacion" class="table table-bordered table-sm" cellspacing="0" width="100%">
<thead>
<tr class="table-warning">
<th width="5%"># Indicador</th>
<th width="5%">Cantidad</th>
<th width="10%">Componente</th>
<th width="5%"></th>
</tr>
</thead>
<tbody>
<tr>
<td> 1</td>
<td> 4 </td>
<td> Falso </td>
<td> <button class='btn btn-danger btn-sm'>x </button> <span class='glyphicon glyphicon-chevron-up arriba'></span> <span class='glyphicon glyphicon-chevron-down down abajo'> </span> </td>
</tr>
<tr>
<td> 2</td>
<td> 5 </td>
<td> Falso </td>
<td> <button class='btn btn-danger btn-sm'>x </button> <span class='glyphicon glyphicon-chevron-up arriba'></span> <span class='glyphicon glyphicon-chevron-down down abajo'> </span> </td>
</tr>
</tbody>
</table>
Espero haberme explicado bien.