Looking for some jquery help with this scenario...
I am displaying a list of sports rankings on a page with a table that could potentially contain a couple of thousand players. For display purposes, I only want to show the five rows before and the five rows after the currently logged in user.
For example, if I had a table with this info:
<table>
<tr><td>Person 1</td><td>999</td></tr>
<tr><td>Person 2</td><td>998</td></tr>
<tr><td>Person 3</td><td>997</td></tr>
<tr><td>Person 4</td><td>994</td></tr>
<tr><td>Person 5</td><td>992</td></tr>
<tr><td>Person 6</td><td>980</td></tr>
<tr><td>Person 7</td><td>970</td></tr>
<tr><td class="me">Person 8</td><td>960</td></tr>
<tr><td>Person 9</td><td>950</td></tr>
<tr><td>Person 10</td><td>940</td></tr>
<tr><td>Person 11</td><td>930</td></tr>
<tr><td>Person 12</td><td>920</td></tr>
<tr><td>Person 13</td><td>910</td></tr>
<tr><td>Person 14</td><td>900</td></tr>
<tr><td>Person 15</td><td>890</td></tr>
</table>
I'm logged in as Person 8. So I should only see the rows from Person 3 to Person 13.
I'm hoping to find a solution using something like a loop with jquery's prev() and next() functions.
Any help would be greatly appreciated. Thanks in advance!
Lots of indexes here, check this code... (preview on jsfiddle)
// First we get the index of the row containing the user
var me_tr_index = $('tr')
.index($('tr')
.filter( function (){
return $('td.me', this).length;
})
);
// Now we have our index, we can use it inside conditions
$('tr').each( function (i) {
if(i < me_tr_index -2 || i > me_tr_index +2)
$(this).css('color', 'red');
if(i === me_tr_index )
$(this).css('font-weight', 'bold');
});