Monday, December 7, 2009

[jQuery] Re: Filtering table cell values

Thank you, Brian. This is almost exactly what I needed. The only
change I had to make was that I don't want retired people highlighted

if ($('td:nth-child(7)', $(this)).text().text().toLowerCase() !=
'retired')

I'm still amazed at the power and flexibility of jQuery.

On Dec 6, 10:21 pm, brian <zijn.digi...@gmail.com> wrote:
> Try this:
>
>         jQuery('#Directorships tbody tr').each(function()
>         {
>                 if (jQuery('td:nth-child(7)', $(this)).text().toLowerCase() == 'retired')
>                 {
>                         var td12th = jQuery('td:nth-child(12)', $(this));
>
>                         if (td12th.text() == '0')
>                         {
>                                 td12th.addClass('bgHighlight');
>                         }
>                 }
>
>         });
>
> I added tbody to the selector so it'll ignore the head or foot if you
> have them. If you don't, just remove that.
>
> You want to avoid using '#Directorships' in the selector with your
> each() function because the context is already a specific row. So, we
> add $(this) as the 2nd param of jQuery() to say that we're looking for
> the 7th (or 12th) td of THIS row.
>
> Also, the test for 0 uses the string '0' to avoid any equivalence
> weirdness (since text() returns a string anyway).
>
> I suggest that you set a class ('Retired' or 'Active') on your 7th
> cell instead of relying on a comparison of the string content. That
> would also make it much simpler to select rows for highlighting.
>
>
>
> On Sun, Dec 6, 2009 at 5:55 PM, evanbu...@gmail.com <evanbu...@gmail.com> wrote:
> > I'm using this code hightlight any table cell in the 12th column with
> > a value of zero which works well.
>
> > //SharesHeld
> >        jQuery('#Directorships tr').each(function() {
> >        jQuery('#Directorships td:nth-child(12)').filter(function() {
> >        if (jQuery(this).text() == 0)
> >                jQuery(this).addClass("bgHighlight");
> >        });
> > });
>
> > I need to make this a bit more complex by hightlighting any table cell
> > in the 12th column with a value of zero except those rows with a value
> > of 'Retired' in the 7th column.  I tried this approach below but if
> > any value in the 7th column has a value of 'Retired' then none of the
> > cells in the 12th get highlighted.  I need to to evaluate it on a row-
> > by-row basis rather than the entire column.  In other words, I just
> > want to exclude anyone who is retired.
>
> > //SharesHeld
> >        jQuery('#Directorships tr').each(function() {
> >        jQuery('#Directorships td:nth-child(12)').filter(function() {
> >        if ((jQuery(this).text() == 0) && ('#Directorships td:nth-child
> > (7)').not(':contains("Retired")'))
> >                jQuery(this).addClass("bgHighlight");
> >        });
> > });
>
> > In this example, the first 2 zeros in the 1st and 2nd rows should be
> > highlighted but the third one should not be.
>
> > Column7 Column12
> > Active  0 (highlighted)
> > Active  0 (highlighted)
> > Retired 0 (not highlighted)
>
> > Thanks- Hide quoted text -
>
> - Show quoted text -

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate