Jquery to move row up and down

邮差的信 提交于 2019-12-21 02:54:13

问题


I used the code given here to move rows up/down in a gridview using jquery and this works perfectly, but how can implement to move a row to first position or last in the table?


回答1:


Add top and bottom links, and insert after/before the first/last row:

DEMO

JS:

$(document).ready(function(){
    $(".up,.down,.top,.bottom").click(function(){
        var row = $(this).parents("tr:first");
        if ($(this).is(".up")) {
            row.insertBefore(row.prev());
        } else if ($(this).is(".down")) {
            row.insertAfter(row.next());
        } else if ($(this).is(".top")) {
            row.insertBefore($("table tr:first"));
        }else {
            row.insertAfter($("table tr:last"));
        }
    });
});

HTML:

<table>
    <tr>
        <td>One</td>
        <td>
            <a href="#" class="up">Up</a>
            <a href="#" class="down">Down</a>
            <a href="#" class="top">Top</a>
            <a href="#" class="bottom">Bottom</a>
        </td>
    </tr>
    <tr>
        <td>Two</td>
        <td>
            <a href="#" class="up">Up</a>
            <a href="#" class="down">Down</a>
            <a href="#" class="top">Top</a>
            <a href="#" class="bottom">Bottom</a>
        </td>
    </tr>
    <tr>
        <td>Three</td>
        <td>
            <a href="#" class="up">Up</a>
            <a href="#" class="down">Down</a>
            <a href="#" class="top">Top</a>
            <a href="#" class="bottom">Bottom</a>
        </td>
    </tr>
    <tr>
        <td>Four</td>
        <td>
            <a href="#" class="up">Up</a>
            <a href="#" class="down">Down</a>
            <a href="#" class="top">Top</a>
            <a href="#" class="bottom">Bottom</a>
        </td>
    </tr>
    <tr>
        <td>Five</td>
        <td>
            <a href="#" class="up">Up</a>
            <a href="#" class="down">Down</a>
            <a href="#" class="top">Top</a>
            <a href="#" class="bottom">Bottom</a>
        </td>
    </tr>
</table>



回答2:


you can do

$(document).ready(function(){
    $(".first,.last").click(function(){
       var row = $(this).parents("tr:first");
       var rows= row.parents().find("tr");
       if ($(this).is(".first")) {
           row.insertBefore(rows[0]);
       } else {
           row.insertAfter(rows[rows.length]);
       }
   });  
});     

jsfiddle




回答3:


$(document).ready(function(){
    $(".first,.last").click(function(){
       var row = $(this).parents("tr:first");
       var rows= row.parents().find("tr");
       if ($(this).is(".first")) {
           row.insertBefore(rows[0]);
       } else {
           row.insertAfter(rows[rows.length]);
       }
   });  
});    


来源:https://stackoverflow.com/questions/16524497/jquery-to-move-row-up-and-down

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!