Jquery上下移动行

时间:2013-05-13 14:26:37

标签: jquery gridview

我使用给定here的代码在网格视图中使用jquery上下移动行,这非常有效,但如何实现将行移动到表中的第一个位置或最后一个位置?

3 个答案:

答案 0 :(得分:17)

添加顶部和底部链接,并在第一行/最后一行之后/之前插入:

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>

答案 1 :(得分:1)

你可以做到

$(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

答案 2 :(得分:0)

$('#getmessage').on('click', function () {
    var text = '';
    $('#discussion>li').each(function () {
        text += $(this).text();
        text += '\n'
    })
    console.log(text)

    $.ajax({
        url: 'File/Index?data=' + text, // Just make change here..
        type: 'POST',
        data: text,
        success: function (result) {
            console.log("data send");
        }
    })
})