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

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

我有一个 HTML 表,我正在尝试使用 jQuery UI Sortable Widget 来启用行的重新排序。我已经设置了可排序的 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;
        }
    });
});

但是,当有多个行高的行时,containment 的行为并不像预期的那样:当拖动较大的行时,表格的高度会降低,containment 会进一步缩小,以至于无法移动大行到表格的底部。其他行工作正常。

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

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

最佳答案

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

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

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

例如:

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/

关于javascript - 将包含应用于 jQuery UI 可排序表可防止将高行移动到最后一个位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36551105/

相关文章:

JavaScript 混淆和缩小

javascript - 将事件处理程序附加到每个 li

javascript - jQuery 自动完成小部件源 - 参数来自哪里?

javascript - jQuery-UI 可排序/可删除

Javascript addEventListener 未按预期工作

javascript - 各种 calc() polyfill 库在 IE8 中不起作用

javascript - 在 Electron JS 应用程序中使用自定义标题栏

javascript - jquery : How to use the . css() 方法

javascript - 获取原始背景图像 - JQuery

jquery - MVC 5 与 @Scripts.Render ("~/bundles/jquery") 和 JQuery 对话框