1
votes

I m trying to drag n drop rows between two tables. First table has 3 columns and the second table has 4 columns. I have the drag n drop working.

All the examples I have seen generally appendTo at the dropped location. What I would like to do is replace the contents of the dragged row and replace/update the cells in the dropped location.

I was thinking if I can maybe write a new tablerow( ..) and then remove the current dropped row and replace it with the generated row. Or maybe just update the columns in the new location.

(function ($) {
  $(document).ready(function () {
    var tb1 = $("#table1 tr");
    var tb2 = $("#table2 tr");
    tb1.draggable({
      appendTo: "body",
      helper: "clone",
      opacity: "0.35",
      revert: "invalid"
    });
    tb2.droppable({
      accept: '#table1 tr',
      tolerance: "pointer",
      drop: function (event, ui) {
        //loop through all items in the row
        var $col1, $col2;
        tb1.each(function () {
          $col1 = $("span:eq(0)", this).text();
          $col2 = $("span:eq(1)", this).text();
        });
        $droppedRow = $(this).children('td');
        var $destCol1 = $droppedRow.find('span')[0].innerHTML;
        var $destCol2 = $droppedRow.find('span')[1].innerHTML;
        var $row = "<tr><td>" & $destCol1 & "</td><td>" & $destCol2 & "</td><td>" & $col1 & "</td><td>" & $col2 & "</td></tr>";

      }

http://jsfiddle.net/S2yC2/7/

Any suggestions how to proceed. thanks

1

1 Answers

2
votes

I think you are trying to do this : http://jsfiddle.net/S2yC2/15/

  drop: function (event, ui) {

    //Retrieve relevant data of the dragged line
    var $draggedCol = $(ui.draggable).children('td');
    var col1 = $($draggedCol[0]).html();
    var col2 = $($draggedCol[1]).html();      

    //Assign data to the void cell of the dropped line
    var $droppedRow = $(this).children('td');
    $($droppedRow[2]).html(col1);
    $($droppedRow[3]).html(col2);

  }