【问题标题】:Jquery UI sorting multiple table row at onceJquery UI一次排序多个表行
【发布时间】:2016-11-08 09:13:03
【问题描述】:

jqueryui 排序有问题。

现在我有一个在 tr 上排序的表,并且一切正常,但现在我必须一次对 2 行进行排序。在下面的示例中,我必须将行与 G1 或 G2 或 G3 一起排序。

在我的头撞墙几个小时后,我来这里寻求帮助。

Queryui 排序有选项item,我尝试使用它但无法使其在表中工作。

有办法做到这一点吗?谁能帮帮我?

我在这里试试https://jsfiddle.net/n29ekt42/

    <table>
    <thead>
        <tr>
            <th>T1</th>
            <th>T1</th>
            <th>T1</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>G1</td>
            <td>G1</td>
            <td>G1</td>
        </tr>
        <tr>
            <td>G1</td>
            <td>G1</td>
            <td>G1</td>
        </tr>
        <tr>
            <td>G2</td>
            <td>G2</td>
            <td>G2</td>
        </tr>
        <tr>
            <td>G2</td>
            <td>G2</td>
            <td>G2</td>
        </tr>
        <tr>
            <td>G3</td>
            <td>G3</td>
            <td>G3</td>
        </tr>
        <tr>
            <td>G3</td>
            <td>G3</td>
            <td>G3</td>
        </tr>
    </tbody>
    </table>

   function assignSortAttach() {

    $("table tbody").sortable({
        start: function( event, ui ) {
            var aa = $this.attr('data-row');
        }
    })
}

// TR sortable
$(function () {
    assignSortAttach()
});

【问题讨论】:

  • 您实际上并没有在这里提出问题,也没有告诉我们您要做什么......?
  • 看看将 Selectable 与 Sortable 结合使用。

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


【解决方案1】:

我发现这比我预期的要困难,所以我尝试了一下。我分叉了你的小提琴并做了一些更新。

工作示例:https://jsfiddle.net/Twisty/n29ekt42/13/

我添加了一个句柄列。

HTML

<table>
  <thead>
    <tr>
      <th></th>
      <th>T1</th>
      <th>T1</th>
      <th>T1</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><span class="handle ui-icon ui-icon-grip-dotted-vertical"></span></td>
      <td>G1</td>
      <td>G1</td>
      <td>G1</td>
    </tr>
...

大量的 CSS,如果你喜欢的话。

CSS

table {
  border-collapse: collapse;
}

table thead tr {
  margin-left: 20px;
}

table tbody tr {
  border: 1px solid #ccc;
}

tr.ui-selecting {
  background: #FECA40;
}

tr.ui-selected {
  background: #F39814;
  color: white;
}

.hidden {
  display: none;
}

jQuery

function assignSortAttach() {
  $("table tbody").sortable({
      axis: "y",
      cursor: "grabbing",
      handle: ".handle",
      opacity: 0.6,
      helper: function(e, item) {
        console.log("Parent-Helper: ", item);
        if (!item.hasClass("ui-selected")) {
          item.addClass("ui-selected");
        }
        // Clone selected elements
        var elements = $(".ui-selected").not('.ui-sortable-placeholder').clone();
        console.log("Making helper: ", elements);
        // Hide selected Elements
        item.siblings(".ui-selected").addClass("hidden");
        var helper = $("<table />");
        helper.append(elements);
        console.log("Helper: ", helper);
        return helper;
      },
      start: function(e, ui) {
        var elements = ui.item.siblings(".ui-selected.hidden").not('.ui-sortable-placeholder');
        ui.item.data("items", elements);
      },
      update: function(e, ui) {
        console.log("Receiving: ", ui.item.data("items"));
        $.each(ui.item.data("items"), function(k, v) {
          console.log("Appending ", v, " before ", ui.item);
          ui.item.before(v);
        });
      },
      stop: function(e, ui) {
        ui.item.siblings(".ui-selected").removeClass("hidden");
        $("tr.ui-selected").removeClass("ui-selected");
      }
    })
    .selectable({
      filter: "tr",
      cancel: ".handle"
    })
}

$(function() {
  assignSortAttach();
});

这是改编自这里的代码:jQuery Sortable - drag and drop multiple items 和这里:jQuery UI sortable & selectable

同时使用 Sort 和 Select 的功劳归于 mhu。这非常有效,因为您可以拖动选择或 CTRL+单击来选择您喜欢的行。您可以拖动并选择一个组,或单击取消对拖动进行分组的项目。然后使用拖动手柄,用户可以对选定的行进行排序。

TJ 使我们能够对多个项目进行排序。他的 Demo 旨在在单独的列表之间移动元素,因此我将事件 receive 切换为 update

从轻率的评论到回答,我希望能有所帮助。

评论后更新


工作示例:https://jsfiddle.net/Twisty/Lbu7ytbj/

我将 HTML 更改为对行进行分组:

<table>
  <thead>
    <tr>
      <th>&nbsp;</th>
      <th>T1</th>
      <th>T1</th>
      <th>T1</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td rowspan="2"><span data-group="1" class="handle ui-icon ui-icon-grip-dotted-vertical"></span></td>
      <td>G1</td>
      <td>G1</td>
      <td>G1</td>
    </tr>
    <tr>
      <td>G1</td>
      <td>G1</td>
      <td>G1</td>
    </tr>
    </tbody>
    <tbody>
    <tr>
      <td rowspan="2"><span data-group="3" class="handle ui-icon ui-icon-grip-dotted-vertical"></span></td>
      <td>G2</td>
      <td>G2</td>
      <td>G2</td>
    </tr>
    <tr>
      <td>G2</td>
      <td>G2</td>
      <td>G2</td>
    </tr>
    </tbody>
    <tbody>
    <tr>
      <td rowspan="2"><span data-group="5" class="handle ui-icon ui-icon-grip-dotted-vertical"></span></td>
      <td>G3</td>
      <td>G3</td>
      <td>G3</td>
    </tr>
    <tr>
      <td>G3</td>
      <td>G3</td>
      <td>G3</td>
    </tr>
  </tbody>
</table>

现在我所要做的就是调整sortable 以使用表格而不是使用thead

function assignSortAttach() {
  $("table").sortable({
    axis: "y",
    cursor: "grabbing",
    handle: ".handle",
    cancel: "thead",
    opacity: 0.6,
    placeholder: "two-place",
    helper: function(e, item) {
      if (!item.hasClass("selected")) {
        item.addClass("selected");
      }
      console.log("Selected: ", $(".selected"));
      var elements = $(".selected").not(".ui-sortable-placeholder").clone();
      console.log("Making helper from: ", elements);
      // Hide selected Elements
      $(".selected").not(".ui-sortable-placeholder").addClass("hidden");
      var helper = $("<table />");
      helper.append(elements);
      console.log("Helper: ", helper);
      return helper;
    },
    start: function(e, ui) {
      var elements = $(".selected.hidden").not('.ui-sortable-placeholder');
      console.log("Start: ", elements);
      ui.item.data("items", elements);
    },
    update: function(e, ui) {
      console.log("Receiving: ", ui.item.data("items"));
      ui.item.before(ui.item.data("items")[1], ui.item.data("items")[0]);
    },
    stop: function(e, ui) {
      $('.selected.hidden').not('.ui-sortable-placeholder').removeClass('hidden');
      $('.selected').removeClass('selected');
    }
  });
}

$(function() {
  assignSortAttach();
  $(".handle").attr("title", "Use me to drag and sort.");
});

我还为占位符添加了 CSS:

.two-place {
  display: block;
  visibility: visible;
  height: 2.5em;
  width: 0;
}

.two-place::after {
  content: "";
  border: 1px dashed #ccc;
  float: left;
  background-color: #eee;
  height: 2.5em;
  width: 100px;
}

【讨论】:

  • 那你在找什么?
  • 必须同时移动两个特定的行。第 1 行和第 2 行、第 3 和第 4 行、第 5 和第 6 行等等。
  • @GermanoPlebani 你没有在你的问题中解释这一点。所以我不知道这是一个要求。这使它变得容易得多。稍后我会提供更新。
  • @GermanoPlebani 会有超过 2 行的行组吗?还是总是 2 人一组?
  • @GermanoPlebani 丢弃分组项目时,他们是否应该拆分另一个组?
猜你喜欢
  • 2014-01-07
  • 2016-06-19
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2012-12-07
  • 2014-09-15
  • 1970-01-01
相关资源
最近更新 更多