Search code examples
jqueryjquery-ui-sortableborder-spacing

The table row gets offset when dragging, using JQuery sortable


I have a table that uses border-spacing to separate rows.

When using JQuery sortable - and it works - the row jumps down when beeing moved, can this be fixed?

This code demonstrates:

$(function() {
  $("#items").sortable();
  $("#items").disableSelection();
});
table {
  border-spacing: 0 20px;
  background-color: #cda;
}

td {
  width: 170px;
  border: 2px solid gray;
}
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>

<table>
  <tbody id="items">
    <tr>
      <td class="list">1</td>
    </tr>
    <tr>
      <td class="list">2</td>
    </tr>
    <tr>
      <td class="list">3</td>
    </tr>
  </tbody>
</table>


Solution

  • I found a solution. I added a class to the dragged element,

    .up{
    margin-top: -20px;
    }
    

    (It seems to correspond to the value of border-spacing)

    The adding was by making the sortable call like this:

    $(function () {
    $("#items").sortable({
    placeholder: "highlight",
    start: function (event, ui) {
    ui.item.toggleClass("up");
    },
    stop: function (event, ui) {
    ui.item.toggleClass("up");
    }
    });
    $("#items").disableSelection();
    });