Swap rows between tables

4

I try to make it possible to send a row to another table from a modal with a table.

Here is what I have advanced. link

However, even if you send the row to another table it does not return to its original table when it is deleted. And I would like to know what I need to do it.

Thanks in advance.

JS Jquery 2.1.0:

$('table>tbody>tr>td>.a').one('click',function(){

    $(".othertable").append("<tr><td>"+
    $(this).closest('tr').children()[0].textContent+"</td><td><input type='text' placeholder='Ingresar cantidad'/></td><td>"+
    $(this).closest('tr').children()[1].textContent+"</td><td><input type='button' class='btneli' value='Eliminar'></td></tr>");
    $(this).parent().parent().remove();
    $('tr .btneli').one('click',function(){
        $('.btneli').parent().remove();
    });
});

HTML:

<div class="row">
    <div class="col-xs-12 col-md-12">
        <table class="table table-condensed table-hover table-bordered">
            <thead>
            <tr>
                <th>Firstname</th>
                <th>Lastname</th>
                <th>Accion</th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td>John</td>
                <td>Doe</td>
                <td><button class="a">Aqui</button></td>

            </tr>
            <tr>
                <td>Sam</td>
                <td>Smith</td>
                <td><button class="a">Aqui</button></td>
            </tr>
            </tbody>
        </table>
    </div>
</div>
<table class="othertable table-condensed table-hover table-bordered">
            <thead>
            <tr>
                <th>Firstname</th>
                <th>Precio</th>
                <th>Lastname</th>
                <th>Accion</th>
            </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    
asked by Raphael 22.06.2016 в 08:02
source

1 answer

2

Responding to your question I've made some changes to your code, basically it would look like this:

  • First let's change the way you are thinking about your code, instead of removing the row of the Here button, let's just hide it. that is, we comment the line; $(this).parent().parent().remove(); and add var tr = $(this).closest("tr"); . In the end you should have something like this:

    //  $(this).parent().parent().remove();
        var tr = $(this).closest("tr");
    

    and then we add the code that I leave below in the button with the class; .btneli

    $('tr .btneli').one('click',function(){
      $('.btneli').parent().remove();
      var tr = $(this).closest("tr"); //con la función closest llegamos hasta el tr
      var td = $(tr[0]).children(0); //Solicitamos el primer TR y obtenemos su primer hijo TD
      var texto = $(td[0]).text(); //Extramos el texto del TD
      $(".table tr td:contains("+texto+")").closest("tr").show(); //Buscamos el TD que contenga el texto, subimos hasta el TR y mostramos toda la fila.
    });
    
  • Likewise I leave you the Snippet.

    $('.a').one('click',function(){
    
        $(".othertable").append("<tr><td>"+
        $(this).closest('tr').children()[0].textContent+"</td><td><input type='text' placeholder='Ingresar cantidad'/></td><td>"+
        $(this).closest('tr').children()[1].textContent+"</td><td><input type='button' class='btneli' value='Eliminar'></td></tr>");
      //  $(this).parent().parent().remove();
      var tr = $(this).closest("tr");
      $(tr[0]).hide();
        $('tr .btneli').one('click',function(){
        	$('.btneli').parent().remove();
          var tr = $(this).closest("tr");
          var td = $(tr[0]).children(0);
          var texto = $(td[0]).text();
          $(".table tr td:contains("+texto+")").closest("tr").show();
        });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
    
    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
    
    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
    
    <div class="row">
        <div class="col-xs-12 col-md-12">
            <table class="table table-condensed table-hover table-bordered">
                <thead>
                <tr>
                    <th>Firstname</th>
                    <th>Lastname</th>
                    <th>Accion</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <td>John</td>
                    <td>Doe</td>
                    <td><button class="a">Aqui</button></td>
    
                </tr>
                <tr>
                    <td>Sam</td>
                    <td>Smith</td>
                    <td><button class="a">Aqui</button></td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
    <table class="othertable table-condensed table-hover table-bordered">
                <thead>
                <tr>
                    <th>Firstname</th>
                    <th>Precio</th>
                    <th>Lastname</th>
                    <th>Accion</th>
                </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
    
    <div class="modal fade" id="myModal">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">EDIT</h4>
          </div>
          <div class="modal-body">
            <p><input type="text" class="input-sm" id="txtfname"/></p>
            <p><input type="text" class="input-sm" id="txtlname"/></p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div><!-- /.modal-content -->
      </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->

    Update by comment tracking.

    Well, I'm going to leave you a code that I made based on the particular need. It should be noted that there were many things that I did not work with Jquery, I did in JS Nativo, this because of the ease of the native JS.

    $('.a').on('click',function(){
    var trPrincipal = this.offsetParent.parentElement; //Buscamos el TR principal
    var firstName = trPrincipal.children[0].outerText; //Capturamos el FirstName
    var lastName = trPrincipal.children[1].outerText; //Capturamos el LastName
    $(".othertable").append("<tr><td>"+
    firstName+"</td><td><input type='text' placeholder='Ingresar cantidad'/></td><td>"+
    lastName+"</td><td><input type='button' class='btneli' value='Eliminar'></td></tr>");
      trPrincipal.style.display = "none"; //Ocultamos el TR de la Primer Tabla
      var btn_ = document.querySelectorAll(".btneli"); // Buscamos todos los elementos que tengan la clase .btneli
      for(var a in btn_){ //Iteramos la variable btn_
    var b = btn_[a];
    if(typeof b == "object"){ //Solo necesitamos los objetos
      b.onclick = function (){ //Asignamos evento click
        var trBtn = this.offsetParent.parentElement; // buscamos el tr principal de la segunda tabla
        var firstNameBtn = trBtn.children[0].outerText; //Capturamos el FirstName de la segunda tabla
        trBtn.remove(); // eliminamos toda la fila de la segunda tabla
        var tbl = document.querySelectorAll(".table td:first-child"); //Obtenemos todos los primeros elementos td de la primera tabla
        for(var x in tbl){ //Iteramos los elementos obtenidos
          var y = tbl[x];
          if(typeof y == "object"){ //solo nos interesan los object
            if (y.outerText == firstNameBtn){ //comparamos el texto de la variable y vs el firstNameBtn
               var t = y.parentElement; //capturamos el elemento de la coincidencia
              t.style.display = ""; //actualizamos el estilo display dejándolo en vacío y esto mostrará nuevamente la fila tr de la primera tabla
            }
          }
        }
      }
    }
      }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
    
    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
    
    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
    
    <div class="row">
        <div class="col-xs-12 col-md-12">
            <table class="table table-condensed table-hover table-bordered">
                <thead>
                <tr>
                    <th>Firstname</th>
                    <th>Lastname</th>
                    <th>Accion</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <td>John</td>
                    <td>Doe</td>
                    <td><button class="a">Aqui</button></td>
    
                </tr>
                <tr>
                    <td>Sam</td>
                    <td>Smith</td>
                    <td><button class="a">Aqui</button></td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
    <table class="othertable table-condensed table-hover table-bordered">
                <thead>
                <tr>
                    <th>Firstname</th>
                    <th>Precio</th>
                    <th>Lastname</th>
                    <th>Accion</th>
                </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
    
    <div class="modal fade" id="myModal">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">EDIT</h4>
          </div>
          <div class="modal-body">
            <p><input type="text" class="input-sm" id="txtfname"/></p>
            <p><input type="text" class="input-sm" id="txtlname"/></p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div><!-- /.modal-content -->
      </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
        
    answered by 22.06.2016 / 23:52
    source