do not remove if first line html java

Asked

Viewed 26 times

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>&nbsp;</td>
            <td>&nbsp;</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/

  • @Ricardopunctual, perfect, worked right. Only one doubt, when adding new line, has in the input keep the initial value 0?

  • @Ricardopunctual, already done, in this line $( thisRow ).clone(). insertAfter( thisRow ).find( 'input:text' ).val( '0' );. thanks for the help!

  • Good, I was going to post an answer, but already posted the same solution, good luck with the codes :)

1 answer

0


Just add this in your delete method, a small check of how many tr exist.

$(table).delegate( '.tr_clone_del', 'click', function () {
      if($('.tr_clone').length > 1) {
        var thisRow = $(this).closest("tr")[0];
        thisRow.remove();
      }
  });

Follow the full example.

$(function() {
	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 () {
      if($('.tr_clone').length > 1) {
        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>&nbsp;</td>
            <td>&nbsp;</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"></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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.