I know this question has been asked a lot but there is really no definite answer that I can find.
Here is my code. Basically I have everything set up. I'm just lost on where to get the rows of a table by index. I've tried the .index function but I that's not working. I'm trying to do something like $('#pkgLineTable').index(i).addClass('row_selected');
But that's not working correctly. Any help would be great. Thank you very much.
var lastSelected;
$('#pkgLineTable tr').live('click', function(event) {
var tableRow = $(this).closest("tr").prevAll("tr").length + 1;
if ($(this).hasClass('row_selected')) {
$(this).removeClass('row_selected');
}
else {
$(this).addClass('row_selected');
}
if (event.shiftKey) {
var table = $('#pkgLineTable');
var start = Math.min(tableRow, lastSelected);
var end = Math.max(tableRow, lastSelected);
for (var i = start; i < end; i++) {
//$(this).parent().parent().addClass('row_s开发者_开发知识库elected');
}
} else {
lastSelected = $(this).closest("tr").prevAll("tr").length + 1;
}
My start and end values are correct. If you select the 2 row and the 5 row you get back 2 and 5. It then iterates 2 - 5. I need to add a class to the rows 2 - 5.
Edit I used this:
for (var i = start; i < end; i++) {
$('#pkgLineTable').each(function() {
$(this).find("tr").eq(i).addClass('row_selected');
});
}
That's probably not the best way to do it.
instead of
for (var i = start; i < end; i++) {
//$(this).parent().parent().addClass('row_selected');
}
use
table.find('tr:gt('+(start-1)+'):lt('+(end)+')').addClass('row_selected');
精彩评论