tags:

views:

55

answers:

4

Is it possible to use the more than operator in a jquery criteria For e.g say i want to get all rows where the rowindex is more than a certain value. Is it possible somehow or do i need create my own function once I have the result set from jquery?

+1  A: 
var $filtered = $('table').find('tr').filter(function(){
     return (this.rowIndex > 5);
});

Works.

It does not work with $('table').children('tr'), I have no clue why not. Somebody please?

jAndy
$('table').children('tr') finds nothing because tr are children of tbody: table > tbody > tr
Mario Menger
@jAndy: See the docs for [`table.tBodies`](https://developer.mozilla.org/en/DOM/table.tBodies), a `<tbody>` element is automatically inserted into the DOM if it's not specified. The `<tr>` elements will be children of that `<tbody>`.
Andy E
A: 

I would try something similar to the following:

$('tr.selector').each(function(){
    var rowId = $(this).index();
    if (rowId > 5){
        $(this).addClass('red');
    }
 });
HurnsMobile
+3  A: 

For the example mentioned in your question, you can just use the :gt() selector:

// Get all rows with a rowIndex greater than 5:
var rowsPlus5 = $('#mytable tr:gt(5)');

For other scenarios, you might need to use the .filter() method with a callback function.

Andy E
This looks good, any ideas how I can chain multuiple critieria in jquery.For e.g i'm alreay using a td find: table.find("td[ColumnName=" + searchColumn + "]:contains('" + searchVal + "')")if i want to add a tr gt() critieria too then is it possible to chain them somehow?
Rubans
@Rubans: it's probably bordering on obscene and I try and avoid such complex selectors because they don't always work, e.g. `table.find("tr:gt(5) > td[ColumnName=" + searchColumn + "]:contains('" + searchVal + "')")`. You could also use the `.has()` method or `:has()` selector if you just wanted a reference to the matching TRs and not the TD elements.
Andy E
How do I use has()?The following doesn't seem to return anything:table.find("td[ColumnName=" + searchColumn + "]:icontains('" + searchVal + "')").has("tr:gt(2)");
Rubans
I guess it's because the intial returned return set are all td cells and now I want to get back use the parent element (tr) row index, anyway around this?
Rubans
@Rubans: not sure, you might be able to use the `.parent("tr:gt(5)")` method. It's not quite the same thing, but you could perform your `.has()` first, `table.find("tr:gt(5)").has("`td[ColumnName=" + searchColumn + "]:contains('" + searchVal + "')");`
Andy E
Thanks that works, also liked the each(function) example too but this was nice to use.
Rubans
A: 

you may want:

.slice( start, [ end ] ); Reduce the set of matched elements to a subset specified by a range of indices.

start An integer indicating the 0-based position after which the elements are selected. If negative, it indicates an offset from the end of the set.
end An integer indicating the 0-based position before which the elements stop being selected. If negative, it indicates an offset from the end of the set. If omitted, the range continues until the end of the set.

Reigel