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