jquery - 嵌套可排序表行 jQuery

标签 jquery nested html-table rows jquery-ui-sortable

我正在尝试对表的行进行排序,并将它们作为表行的“子”元素

我发现了这个:http://code.google.com/p/nestedsortables/这适用于 ul li 列表,但我想为表格构建它。

<table>
  <thead>
    <th>tablehead</th>
  </thead>
  <tbody>
    <tr><td>somevalue</td></tr>
    <tr><td>somevalue2</td></tr>
  </tbody>
</table>

因此,您可以使用 jquery.sortable() 并对行进行排序,但如果您将“somevalue”拖动到“somevalue2”上,我希望“somevalue”成为“somevalue2”的子元素

我不知道是否可以使用表格。

有人可以帮助我吗?

最佳答案

好的,我找到了这个插件:http://ludo.cubicphuse.nl/jquery-plugins/treeTable/doc/index.html#example-1

这使用表格并允许嵌套。

现在我只需要对其进行排序并发布带有父 ID 的表元素,以便我可以将其保存到数据库中。

我希望这对其他人有帮助。

这就是我想出的办法。 我不想要超过 2 个级别(只有 parent 和 child ),所以我将其更改为让 parent 只接受 child ,而不是让 parent 可拖动。

<html>
    <head>
        <link href="jquery.treeTable.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript" src="jquery-ui.js"></script>
        <script type="text/javascript" src="jquery.treeTable.js"></script>
        <script type="text/javascript">

    $(document).ready(function()  {
        $("#dragndrop").treeTable();

        // Drag & Drop Code
        $("#dragndrop .child").draggable({
          helper: "clone",
          opacity: .75,
          refreshPositions: true, // Performance?
          revert: "invalid",
          revertDuration: 300,
          scroll: true
         }
        );

        $("#dragndrop .parent").each(function() {
          $($(this).parents("tr")[0]).droppable({
            accept: ".child",
            drop: function(e, ui) {
              $($(ui.draggable).parents("tr")[0]).appendBranchTo(this);
              setNewParent($($(ui.draggable).parents("tr")[0]).attr("id"), $(this).attr("id"));
            },
            hoverClass: "accept",
            over: function(e, ui) {
              if(this.id != $(ui.draggable.parents("tr")[0]).id && !$(this).is(".expanded")) {
                $(this).expand();
              }
            }
          });
        });

        function setNewParent(child, parent)
        {
            // do ajax call here
            alert(child);
            alert(parent);
        }
    });

    </script>
</head>
<body>

    <table id="dragndrop">
      <thead>
        <tr> 
          <th>Title</th>
          <th>Size</th>
          <th>Kind</th>
        </tr>
      </thead>
      <tbody>
        <tr id="node-1">
          <td><span class="parent">CHANGELOG</span></td>
          <td>4 KB</td>
          <td>Parent</td>
        </tr>

        <tr id="node-3" class="child-of-node-1">
          <td><span class="child">images</span></td>
          <td>--</td>
          <td>child</td>
        </tr>

        <tr id="node-2">
          <td><span class="parent">doc</span></td>
          <td>--</td>
          <td>Parent</td>
        </tr>

        <tr id="node-4" class="child-of-node-2">
          <td><span class="child">bg-table-thead.png</span></td>
          <td>52 KB</td>
          <td>child</td>
        </tr>

        <tr id="node-5" class="child-of-node-2">
          <td><span class="child">folder.png</span></td>
          <td>4 KB</td>
          <td>child</td>
        </tr>

      </tbody>
    </table>
</body>

要使整个表格可排序,您可以使用 jQuery 的 .sortable() 函数,但我将其保留在此处以使其更加清晰。

关于jquery - 嵌套可排序表行 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2433773/

相关文章:

javascript - 如何解决调用单击按钮而不在按键按下时发出警报的问题?

javascript - 选择前一个 "this"元素

arrays - 如何在 $lookup 之后反转 $unwind 或重新组装?

C++ - 知道类型/类是否嵌套?

html - 使 <div> 填充其父 <td> 高度,而不在 <td> 上指定高度

html - IE7/8中的表格宽度

Angular 模板 : How to display the row item as a normal table row

javascript - Jquery FullCalendar,在输出中显示周数和天数名称

javascript - 如何通过标签进行计数并对每个标签应用 +1rem 填充?

objective-c - 嵌套单引号