I think I have it working now. It does what it's supposed to, but
doesn't really seem to me that it would be all the effecient, should a
table have lots of rows (unlikely, but may happen).

Here is my code:


$('a.moveup').click(function(event) {

        //Send request to server
        var href = $(this).attr('href');
        $.get(href);

        //Update table to show new layout
        var $thisRow = $(this).parents('tr:first');
        var $thisTable = $('#main_table');
        var $rows = $('#main_table tr');

        $thisRow.next().insertBefore($thisRow.prev().prev());
        $thisRow.insertBefore( $thisRow.prev().prev().prev());

        $rows.each(function(){
                $(this).find(".moveup").show();
                $(this).find(".movedown").show();
        });

        $thisTable.find("tr:nth-child(2)").find(".moveup").hide();
        $thisTable.find("tr:last").prev().find(".movedown").hide();

        return false;
});

Can anyone think of a more efficient way to do this?

Thanks,

Paul


On Jun 16, 8:34 pm, RobG <robg...@gmail.com> wrote:
> On Jun 17, 3:46 am, theprodigy <tigerseyet...@gmail.com> wrote:
>
> > I've been trying for a while to alter the second to last row of a
> > table. I've tried several ways. The number of rows is dynamic so I
> > can't hard code a number into nth-child. I used $rowNeeded =
> > $thisRow.parents('table:first').children().children().length - 1 to
> > get the second to last row, but it doesn't seem like I can pass this
> > variable into nth-child either.
>
> > How can I select the second to last row of a table?
>
> In browsers compliant with the W3C DOM 2 HTML specification, table
> elements have a rows collection that contains all the rows in the
> table.  That collection has a length attribute, so, where - table - is
> a reference to a table element:
>
>   var rows = table.rows;
>   var secondLastRow = rows[rows.length - 2];
>
> Since rows is a live collection, you can get a reference once and keep
> it, it will dynamically updated regardless of how many rows are added
> or removed from the table.  Note that the above will error if there
> are less than two rows in the table, use with care.
>
> --
> Rob

Reply via email to