Skip to main content

jQuery: Move Table Row?



Say I had to links with up/down arrows for moving a table row up or down in order. What would be the most straightforward way to move that row up or down one position (using jQuery)?





There doesn't seem to be any direct way to do this using jQuery's built in methods, and after selecting the row with jQuery, I haven't found a way to then move it. Also, in my case, making the rows draggable (which I have done with a plugin previously) isn't an option.



Source: Tips4all

Comments

  1. You could also do something pretty simple with the adjustable up/down..

    given your links have a class of up or down you can wire this up in the click handler of the links. This is also under the assumption that the links are within each row of the grid.

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

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


    Added by: Robert Pitt: Demo - JsFiddle

    ReplyDelete
  2. This might be useful:
    http://jqueryui.com/demos/sortable/

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?