Javascript index

Asked

Viewed 54 times

1

I have a table where I can add lines dynamically, every time I click the button I add a new row to fill the field, my first row comes with index 0

<td><input type="text" name="data[Ticket][0][descricao_ticket]" size="20" placeholder="Informe aqui"></td>

My first information I write normally in the database, but when I need to add a new line I need to go to JS and change the index 0 to 1 so that it does not overwrite the information, how do I leave this index increment alone when requesting a new line? I don’t know much about javascript

(function($) {
    remove = function(item) {
      var tr = $(item).closest('tr');

      tr.fadeOut(400, function() {
        tr.remove();  
      });

      return false;
    }
  })(jQuery);

(function($) {
    AddTableRow = function() {

      var newRow = $("<tr>");
      var cols = "";
      //var indice = 0;

      cols += '<td><input type="text" name="data[Ticket][1][descricao_ticket]" size="20" placeholder="Informe aqui"></td>';

      cols += '<td>';
      cols += '<button type="button" class="btn btn-danger" onclick="remove(this)">Remover</button>';
      cols += '</td>';

      newRow.append(cols);
      $("#details-table").append(newRow);

      return false;
    };
  })(jQuery);

1 answer

1


You can count how many elements input to id #details-table own and adding +1 altogether:

var indice = $("#details-table input[type='text']").length+1;

And concatenate into the variable:

cols += '<td><input type="text" name="data[Ticket]['+indice+'][descricao_ticket]" size="20" placeholder="Informe aqui"></td>';

Tip

Can concatenate the variable cols this way below to dry the code:

cols += '<td><input type="text" name="data[Ticket]['+indice+'][descricao_ticket]" size="20" placeholder="Informe aqui"></td>'
+'<td>'
+'<button type="button" class="btn btn-danger" onclick="remove(this)">Remover</button>'
+'</td>';
  • 1

    Ball show, straight up, thanks for your help

  • @Henrique Valeu! Just one observation: when you remove a line, the Dice will decrease again. It will give problem?

  • No problem, the important thing is just not to overwrite the information, in this way suits me perfectly.

  • @And Henrique, a question: do you need to worry about the Dice? Because you can use the array feature of the input text name attribute: name="data[Ticket][descricao_ticket][]". Thus all inputs with the name 'name_input[]' are submitted as an array.

  • @pss1support Theoretically it makes sense, but it would have to do with Henrique if it fits in his programming.

  • @That’s why I didn’t post my answer. Yours became more coherent with the question, that is, more pertinent. And it’s not theory. Usually I use it anyway! Whenever I need this type of solution, I use the array in the name of the same input text and not over write the data.

  • @pss1support I think it is valid to post your answer suggesting what you think is best, even if it is not exactly what you were asked, and, of course, explaining why you do it in the way you suggest. Even if it does not help the question or is not useful to the questioner, it may be valid in the future for other people.

Show 2 more comments

Browser other questions tagged

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