将包含应用于jQuery UI可排序表可防止将高行移动到最后位置

时间:2016-04-11 14:09:06

标签: javascript jquery jquery-ui jquery-ui-sortable

我有一个HTML表格,我正在尝试使用jQuery UI可排序窗口小部件来重新排序行。我设置了可排序的axis: 'y',使用helper函数在拖动行时保留行的宽度,并将containment设置为父表:

jQuery(function($) {
    $("#rows").sortable({
        handle: ".handle",
        axis: "y",
        containment: "#main-table",
        helper: function(e, row) {
            // From http://www.paulund.co.uk/fixed-width-sortable-tables. 
            // Sets the width of each cell to be its original width.
            // This prevents the row collapsing into a narrower stub when being dragged.
            row.children().each(function() {
                $(this).width($(this).width());
            });
            return row;
        }
    });
});

但是,当存在具有多个行高的行时,包含不会按预期运行:当拖动较大的行时,表的高度会减小,并且包含会收缩得更多,从而无法移动大行在桌子底部。其他行工作正常。

可以在此处找到显示此行为的JSFiddle:https://jsfiddle.net/AndrewBennet/rc5pnazc/3/

这是已知/预期的行为吗?有没有解决方法?

2 个答案:

答案 0 :(得分:7)

包含的设置方式是,当拖动开始时,计算元素的坐标并将其推送到数组,然后使用该数组查看是否应在鼠标移动时进行拖动。在您的情况下,会发生这种计算是在从表中删除行之后进行的,因此包含值不正确。

您可以做的一件事是在start事件上调整这些值,使它们在从计算中删除行之前匹配表的状态。您可以通过可排序的实例方法访问它们。

如果您想要更精确,您也可以调整此包含属性的顶部坐标,以便考虑点击偏移。

就像这样:

jQuery(function($) {
  $("#rows").sortable({
    handle: ".handle",
    axis: "y",
    containment: '#main-table',
    start: function(e, ui) {
      // get the instance of the sortable.
      // instance method is new to jquery ui 1.11, for previous versions
      // you can use $(this).data()['ui-sortable'];
      var sort = $(this).sortable('instance');

      // this makes the placeholder fit with the row that's being dragged
      ui.placeholder.height(ui.helper.height());

      // containment property of the sortable instance is not the same
      // as the containment option. The property is calculated
      // from the option. You need to adjust bottom coordinates
      // to take into account the row you just removed from it and the click offset.
      sort.containment[3] += ui.helper.height() * 1.5 - sort.offset.click.top;

      // Since your handle is centered, and pointer coordinates are used
      // for sortable, but top coordinates are used for containment
      // you can have issues with top row. Adjusting with the click offset
      // will resolve the issue.
      sort.containment[1] -= sort.offset.click.top;
    },

    helper: function(e, row) {
      // From http://www.paulund.co.uk/fixed-width-sortable-tables. 
      // Sets the width of each cell to be its original width.
      // This reverts the default behaviour of the row collapsing into a narrower stub when being dragged.
      row.children().each(function() {
        $(this).width($(this).width());

      });
      return row;
    }
  });
});

https://jsfiddle.net/f1nexjmz/4/

答案 1 :(得分:0)

jQuery(function($) {
  $("#rows").sortable({
    handle: ".handle",
    axis: "y",
    containment: "#main-table",
    helper: function(e, row) {
        row.children().each(function() {
            $(this).width($(this).width());
        });
        return row;
    },
    start: function (e, ui) {
        var sort = $(this).sortable('instance');
        sort.containment[3] += ui.helper.height();
    }
  });
});