jquery 如何移动/重新排序html表格行

yftpprvb  于 2023-01-25  发布在  jQuery
关注(0)|答案(4)|浏览(113)

其思想是将特定的表行移动到不同的位置
超文本标记语言

<table id="grid1" class="table table-zebra">
    <tbody>
    <tr>
        <td>Cont 1 tr 1</td>
    </tr>
    <tr>
        <td>Cont 2 tr 2</td>
    </tr>
    <tr>
        <td>Cont 3 tr 3</td>
    </tr>
    <tr>
        <td>Cont 4 tr 4</td>
    </tr>
    <tr>
        <td>Cont 5 tr 5</td>
    </tr>
    <tr>
        <td>Cont 6 tr 6</td>
    </tr>
    <tr>
        <td>Cont 6 tr 6</td>
    </tr>
    <tr>
        <td>Cont 7 tr 7</td>
    </tr>
    <tr>
        <td>Cont 8 tr 8</td>
    </tr>
    <tr>
        <td>Cont 9 tr 9</td>
    </tr>
    <tr>
        <td>Cont 10 tr 10</td>
    </tr>
    <tr>
        <td>Cont 11 tr 11</td>
    </tr>

    <tr>
        <td>Cont 12 tr 12</td>
    </tr>

    <tr>
        <td>Cont 13 tr 13</td>
    </tr>

    <tr>
        <td>Cont 14 tr 14</td>
    </tr>
    </tbody>
</table>

这是基本的表,现在,我怎么才能移动TR 11在一个远离,它将在TR 4下,我很抱歉,我不张贴任何JS,但我不知道如何做到这一点...我正在看这个JSbin这是很好的,但不能使用它...

roqulrg3

roqulrg31#

将第11个tr移动到第4个tr之下:

$("tbody tr:nth-child(11)").insertAfter("tbody tr:nth-child(4)");

Working demo
如果你喜欢香草味的,你需要选择器反过来。

document.querySelector("tbody tr:nth-child(4)").insertAdjacentElement("afterend", document.querySelector("tbody tr:nth-child(11)"));
ep6jt1vc

ep6jt1vc2#

在包含如下所示行的HTML表格的上下文中:

<tr class="form-grid-view-row">
    <td> 
        <a class="up" href="#">⇑</a>
    </td>
    <td> 
        <a class="down" href="#">⇓</a>
    </td>
    <td> 
        <span id="index1" class="form-label">1</span>
    </td>
    <td> 
        <span id="span1" class="form-label">Value 1</span>
    </td>
</tr>

这个脚本:

$('.up,.down').click(function () {
    var row = $(this).parents('tr:first');
    if ($(this).is('.up')) {
        row.insertBefore(row.prev());
    } else {
        row.insertAfter(row.next());
    }
});

$('. up,. down').单击(函数(){
选择类为“up”的每个DOM元素和类为“down”的每个元素,例如$('.up,.down').click(function () {。该函数为每个元素设置一个单击处理程序。
变量行= $(this).父对象('tr:first');
$(this)引用作为单击处理程序的目标的DOM元素(上面选择的“up”或“down”元素之一)。.parents()查找tr:first,即第一个<tr>元素,它从<a>开始,沿着DOM向上移动。它将最终选择整行。
如果($(this).is('. up')){
这是检查所选元素是否具有类“up”。
插入前一行();
这将获取被单击的行,并将其移动到被单击行的最上面的同级行的正上方。
使用的jQuery方法(insertBefore、prev、is、parents等)在jQuery documentation中有更详细的描述。

3yhwsihp

3yhwsihp3#

您可以使用JQuery库来实现这一点。

// selecting tbody is bad instead you need to give an id or class to this tag and select according to this

var fourth = $( "tbody tr:nth-child(4)" ),
    eleventh = $( "tbody tr:nth-child(11)" );
$( "tbody tr:nth-child(11)" ).insertAfter(fourth);
uubf1zoe

uubf1zoe4#

为了动态地移动你的元素,你可以给所有tbody子元素添加一个事件。我基本上是在第一次点击时选择一个元素,然后在第二次点击后移动它。

$(document).on("ready", function () {
    var $tbody = $("#grid");
    var selected = null;
    $tbody.children().on("click", function () {
       if (selected == null)
           selected = this;
       else
       {
           $(selected).insertAfter(this);
           selected = null;
       }
    });
});

它移动后的一个元素,但只是一个想法,你可以自定义它.
你的html应该是这样的。

<table>
    <tbody id="grid">
        <tr> <td> 1 </td> </tr>
        <tr> <td> 2 </td> </tr>
        <tr> <td> 3 </td> </tr>
        <tr> <td> 4 </td> </tr>
    </tbody>
</table>

相关问题