0
good. with this code I can add and remove lines but I’m having a hard time locking the first one so it won’t be deleted. how can I do?
var table = $( '#table-data' )[0];
$( table ).delegate( '.tr_clone_add', 'click', function () {
var thisRow = $( this ).closest( 'tr' )[0];
$( thisRow ).clone().insertAfter( thisRow ).find( 'input:text' ).val( '' );
});
$(table).delegate( '.tr_clone_del', 'click', function () {
var thisRow = $(this).closest("tr")[0];
thisRow.remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table width="100%" border="0" cellspacing="0" cellpadding="0" id="table-data">
<tr>
<td>header1</td>
<td>header2</td>
<td> </td>
<td> </td>
</tr>
<tr class="tr_clone">
<td>
<select name="campo1[]">
<option value="0">-</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</td>
<td><input type="text" name="campo2[]" class="input" value="0"></td>
<td><button alt="" class="tr_clone_add" style="cursor:pointer;" title="Adicionar">+</button></td>
<td><button alt="" class="tr_clone_del" style="cursor:pointer;" title="Remover">-</button>
</td>
</tr>
</table>
If you put a validation in the delete Function? So:
if($('.tr_clone_del').length <= 1) return
Let me know if it works, see the example here: https://jsfiddle.net/9vfxn201/1/– Ricardo Pontual
@Ricardopunctual, perfect, worked right. Only one doubt, when adding new line, has in the input keep the initial value 0?
– Groot
@Ricardopunctual, already done, in this line $( thisRow ).clone(). insertAfter( thisRow ).find( 'input:text' ).val( '0' );. thanks for the help!
– Groot
Good, I was going to post an answer, but already posted the same solution, good luck with the codes :)
– Ricardo Pontual