ok Klaus,

Thanks for this...it truly is amazing what jquery can do.  A quick
question for you though regarding the index property.  If I want to
make this more dynamic...do you know how I would find out the value of
the rowIndex property for the table row I want to move?



On Aug 2, 1:17 pm, Klaus Hartl <[EMAIL PROTECTED]> wrote:
> Klaus Hartl wrote:
>
> > Mike Miller wrote:
> >> Ok...so this is working somewhat...the issue I see now is that the
> >> find() in the plugins are finding the "wrong row".
>
> >> My table structure looks like this:
>
> >> table
> >>  - row1
> >>      - cell 1
> >>           -table
> >> row 2
> >>      -cell 1
> >>            -table
> >> row3
> >>      -cell 1
> >>          -table
>
> >> When we use the parents("table eq:(0)) I get a reference to the main
> >> table containing all of the nested tables...however when using the
> >> find...it is finding rows within the nested table.
>
> >> Is there a way to make the find pertain only to the parent?
>
> >> Mike
>
> > Yes, change this.find('tr') to this.find('>tr')
>
> > (function($) {
> >     $.fn.moveRowAfter = function(index, afterIndex)
> >     {
>
> > this.find(">tr").eq(index).insertAfter(this.find(">tr").eq(afterIndex));
> >         return this;
> >     };
>
> >     ...
>
> > })(jQuery);
>
> > Also the code could be optimized a bit to only perform one search for
> > the rows:
>
> > (function($) {
> >     $.fn.moveRowAfter = function(index, afterIndex) {
> >         var trs = this.find(">tr");
> >         trs.eq(--index).insertAfter(trs.eq(--afterIndex));
> >         return this;
> >     };
> >     $.fn.moveRowBefore = function(index, beforeIndex) {
> >         var trs = this.find(">tr");
> >         trs.eq(--index).insertBefore(trs.eq(--beforeIndex));
> >         return this;
> >     };
> > })(jQuery);
>
> > Next iteration! :-) The code of both methods looks very similiar, why
> > not make a single function out of it, save bytes and reduce possible
> > maintenancing:
>
> > jQuery.fn.moveRow = function(from, to, useBefore) {
> >     var trs = this.find(">tr");
> >     trs.eq(--from)['insert' + (useBefore && 'Before' ||
> > 'After')](trs.eq(--to));
> >     return this;
> > };
>
> > Usage:
>
> > $('table tbody').moveRow(4, 1); // insert after is default
>
> > $('table tbody').moveRow(4, 1, true); // insert before
>
> > Cheers, Klaus
>
> Excuse me, to keep the zero based index, use this:
>
> jQuery.fn.moveRow = function(from, to, useBefore) {
>      var trs = this.find(">tr");
>      trs.eq(from)['insert' + (useBefore && 'Before' ||
> 'After')](trs.eq(to));
>      return this;
>
> };
>
> Usage:
> $('table tbody').moveRow(3, 0);
>
> --Klaus- Hide quoted text -
>
> - Show quoted text -

Reply via email to