That an alert tells me that Fila eliminate

3

Good morning.

I have this Jquery code:

$(document).ready(function() {

    $('#bt_add').click(function() {
          var check = [];
          $("#tabla").find('.selected').find('td').each(function(){
            if($.trim($(this).text()) === "")
              check.push($(this).attr('id'));
          });
          if(check.length === 0)
            agregar();
          else 
            alert('Registre los Datos de la Fila ');;
      });
      $('#bt_del').click(function() {

        eliminar();
      });

      $('#bt_delall').click(function() {
        eliminarTodasFilas();

      });

    });
    var cont = 0;
    var id_fila_selected = [];

    function agregar() {
      cont++;
      var fila =

        '<tr class="selected" id="fila' + cont + '" onclick="seleccionar(this.id);"><td> </td>' +

        '<td><input type="text" id="nombre"></td>' +

        '<td><input type="text" id="area"></td>' +

        '<td><input type="text" id="puesto"></td>' +

        '<td><input type="text" id="email" onkeyup="addToTable(event)"></td></tr>';

      $('#tabla').append(fila);
      reordenar();
    }

    function addToTable(e) {
      if (e.keyCode === 13) {
        e.preventDefault();
        const row = e.target.parentNode.parentNode;
        const inputs = row.querySelectorAll('input');
        const values = [].map.call(inputs, input => input.value);
        const tds = row.querySelectorAll('td');
        [].forEach.call(tds, (td, i) => {
          if (i === 0) { td.textContent = i + 1; }
          else { td.innerHTML = values[i - 1]; }
        });
        reordenar()
      }
    }

    function seleccionar(id_fila) {
      if ($('#' + id_fila).hasClass('seleccionada')) {
        $('#' + id_fila).removeClass('seleccionada');
        // borrar también el id del array de filas seleccionadas
        var existe_el_id = id_fila_selected.indexOf(id_fila); 
        id_fila_selected.splice(existe_el_id, 1);
      } else {
        $('#' + id_fila).addClass('seleccionada');
        // agregar id sólo si se hizo click
        id_fila_selected.push(id_fila); 
      }

    }

    function eliminar() {

      for (var i = 0; i < id_fila_selected.length; i++) {
        $('#' + id_fila_selected[i]).remove();
      }
      reordenar();
    }

    function reordenar() {
      var num = 1;
      $('#tabla tbody tr').each(function() {
        $(this).find('td').eq(0).text(num);
        num++;
      });
    }

    function eliminarTodasFilas() {
      $('#tabla tr.selected').each(function() {
        $(this).remove();
      });
    }

    $(function () {        
          $("table").on("dblclick", "td",function () {                  
              var OriginalContent = $(this).text();
              $(this).addClass("cellEditing");
              $(this).html("<input type='text' value='" + OriginalContent + "' >");
              $(this).children().first().focus();
              $(this).children().first().keypress(function (e) {
                  if (e.which == 13) {
                      var newContent = $(this).val();
                      $(this).parent().text(newContent);
                      $(this).parent().removeClass("cellEditing");
                      }
                  });
              $(this).children().first().blur(function(){
                  $(this).parent().text(OriginalContent);
                  $(this).parent().removeClass("cellEditing");
                  });
              });
          });

The problem here is that when the user deletes a row I get a warning that says: "I delete the row number of the row from the column Name of the column ", but I enter the following:

 $('#bt_del').click(function() {
  alert('Elimino la fila'+ cont );
    eliminar();
  });

Which does not tell me which row to delete, but rather tells me how many times I'm deleting a row.

Finally the fields of the columns are: Nº, NAME, AREA, POSITION, EMAIL, I hope you understand me, even so I show you my HTML code and I hope you can help me:

<div id="content">
  <label> Tabla de Ejemplo </label>
  <br>
  <div align="center" style="width:416px;">
            <button id="bt_add" class="btn btn-primary">Agregar</button>
            <button id="bt_del" class="btn btn-primary">Eliminar</button>
            <button id="bt_delall" class="btn btn-primary">Eliminar todo</button>
             </div>
            <table id="tabla" style="position:absolute;top:150px;left:75px" class="table table-bordered">

            <thead>

    <thead>
      <tr>
        <td>Nº</td>
        <td>NOMBRE</td>
        <td>AREA</td>
        <td>PUESTO</td>
        <td>EMAIL</td>
      </tr>
    </thead>
  </table>
</div>
    
asked by Reinos Ric 21.07.2017 в 16:56
source

3 answers

1

A part of id_fila_selected = []; is also added:

 function eliminar() {
     var dels = 0;
     var idfilas ="";
     for (var i = 0; i < id_fila_selected.length; i++) {
         idfilas += document.getElementById(id_fila_selected[i]).firstElementChild.textContent + " ... ";
         $('#' + id_fila_selected[i]).remove();
         dels++;

     }
    if (dels==1)
        alert(' Se ha eliminado la fila ' + idfilas);
    else 
        alert(' Se han eliminado las filas ' + idfilas);
     reordenar();
     id_fila_selected=[];


 }
    
answered by 26.07.2017 / 20:30
source
3

All you have to do is iterate the selected rows:

const ids = id_fila_selected.join();
const yes = confirm('¿Desea eliminar las filas ${ids}?');

if (yes) { eliminar(); }

The Array#join method allows you to join a array by means of a delimiter (optional, a comma is added by default). In this way, what you will get will be all the selected rows separated by commas inside a string.

Your delete function would not be changed, you only need to empty the array that contains the ids of the selected rows:

id_fila_selected = [];
    
answered by 21.07.2017 в 17:34
1

Since in id_fila_selected you have all the rows that will be eliminated (because the user selected them), you have to take advantage of that variable to show it in an alert.

As you already have the information in that arrangement, you can have it show with a cycle:

$('#bt_del').click(function() {
    var eliminadas = "";
    for (var i = 0; i < id_fila_selected.length; i++) {
        eliminadas = eliminadas + id_fila_selected[i] + ",";
    }
    alert("Elimino la(s) fila(s): "+eliminadas);
    eliminar();
});

In this case we add a comma, because they can be several, according to how your code is structured.

I do not know if you have it somewhere else in your code, but remember that you would have to empty the id_fila_selected fix once you finish deleting the rows.

I hope you serve, greetings.

P.D. I do not know if ToString() is necessary, you can try with or without it, it's just that right now I can not prove it.

    
answered by 21.07.2017 в 18:02