Swapping rows in JQuery

前端 未结 10 1560
后悔当初
后悔当初 2020-12-08 19:58

If I have a table as shown below, and have a up and down arrow that moves rows up and down, how would I go about swapping rows in JQuery?



        
相关标签:
10条回答
  • 2020-12-08 20:27

    Here is the code to swap the rows. Lets take #Row1 and #Row3

    $('#Row1').replaceWith($('#Row3').after($('#Row1').clone(true)));
    

    The clone(true) is used so that events are also taken into account.

    If you want to move row up and down then use this code. To move row UP

    var tableRow = $("#Row1");
    tableRow.insertBefore(tableRow.prev());
    

    To move row DOWN

    var tableRow = $("#Row1");
    tableRow.insertAfter(tableRow.next());
    
    0 讨论(0)
  • 2020-12-08 20:28

    I am using drag and drop plugin for swap cathegories in table (with subcathegories), and after not working. insertAfter works. similiar topic

    0 讨论(0)
  • 2020-12-08 20:32

    Here's another solution.

    To move a row down:

    jQuery("#rowid").next().after(jQuery("#rowid"));
    

    To move a row up:

    jQuery("#rowid").prev().before(jQuery("#rowid"));
    
    0 讨论(0)
  • 2020-12-08 20:36
    $("#Row1").after($("#Row2"));
    

    will work

    0 讨论(0)
提交回复
热议问题