开发者

Get collection of class "b" rows after row class "a" until next non-"b" row?

开发者 https://www.devze.com 2023-02-14 16:09 出处:网络
I\'m not exactly a jQuery selector ninja yet, so I\'m asking this in the hopes somebody can take a stab at it.

I'm not exactly a jQuery selector ninja yet, so I'm asking this in the hopes somebody can take a stab at it.

I'm using jQuery Templates to create a table with rows based on nested dictionaries (they're nested 5 layers deep or so). It's based on data regarding organizational structure, and the people in each group. What I aim to do, based on the partial 开发者_开发技巧code below, is -- for each "team" row -- get the row of "persons" belonging to that team. Here's what I've got, indented for ease-of-reading.

<tr class="division">...</tr>
    <tr class="team">...</tr>
        <tr class="person">...</tr>
        <tr class="person">...</tr>
    <tr class="team">...</tr>
        <tr class="person">...</tr>
        <tr class="person">...</tr>
        <tr class="person">...</tr>
<tr class="division">...</tr>
    <tr class="team">...</tr>

What I have now is a jQuery selector that gets all class="team" rows, and then for each row, I'm trying to find the selector to get each subsequent class="person" row, until the next non-person row. I can't get it right. Does something need to go in the place of the .nextAll() method?

$('table tr.team').each(function () {
    var personRows = $(this).nextUntil('tr.team');
    console.log('row count: ' + personRows.length);
});

I'm logging to the console for the time being so console output for the HTML above should match something like...

    > row count: 2
    > row count: 3

Unfortunately, I'm getting something more along the lines of this, because of that "division"-class row.

    > row count: 2
    > row count: 4

I've been racking my brain trying to look through the API to get what I want, but I can't get the desired effect. Any ideas? Any elegant solutions? Am I going to have to throw in an if clause somewhere to determine if the row is one I want to include in my selector?


Okay, I suppose I just figured it out. I tried for a long time to figure it out and couldn't until after I posted a question... you know how that goes.

In either case, .filter() is my friend.

 var personRows = $(this).nextUntil('tr.team').filter('tr.person');
0

精彩评论

暂无评论...
验证码 换一张
取 消