tags:

views:

34

answers:

1

Given a table with a column "DueDate", what would be the approximate jQuery code for say, setting the backcolor of the table rows based on the value of each DueDate cell with respect to another date?

I am most specifically interested in the pitfalls involving how to properly handle the dates....how to consume a string value explicitly as a date in javascript, do I have to be very strict with the date formatting, etc.

A: 

Given that you are able to convert the dates to seconds (or milliseconds) using Date.parse(), you can then use something like this:

var min = false, max = false;

// Find the minimum and maximum date
$('tr').each(function() {
    // Acquire a numeric presentation somehow
    var milliseconds = parseInt(Date.parse($(this).find('td.date').html()));

    // Determine the min and max
    min = milliseconds < min || !min ? milliseconds : min;
    max = milliseconds > max || !max ? milliseconds : max;
});


$('tr').each(function() {
    // Get the numeric presentation for current row
    var milliseconds = parseInt(Date.parse($(this).find('td.date').html()));

    // The relative position of the date, 0 = min date, 1 = max date
    var position = (milliseconds - min) / (max - min);

    // Generate color components
    var red   = Math.round(((0 - position) + 1) * 255).toString(16);
    var green = Math.round(position * 255).toString(16);

    // Build hex color string
    red     = red.length == 1   ? '0' + red   : red;
    green   = green.length == 1 ? '0' + green : green;
    var hex = '#'+red+green+'00';

    // Apply the color
    $(this).find('td').css('background-color', hex);
});

If it isn't clear from the code, this snippet will first find the minimum and maximum dates, then color the rows so that the minimum date is red and maximum green.

Hope this helps.

Tatu Ulmanen
Just to clarify what this code is doing.It looks at all of the dates and finds the maximum and minimum.It then looks at each row and colours it with a mixture of red and green such that the maximum is green, the minimum is red and everything else is a mixture of the two.It's worth noting that the human eye is very poor at distinguishing variations in colour and assigning them meaning... it's much better at distinguishing variations in Brightness and Saturation.Still, the principle behind the code snippet should help you work out a solution.
Dancrumb
@Dancrumb, I noticed it myself also that the colouring scheme is not very good but it gives some starting points. This would be especially hard to distinguish for people with red-green color blindness.. :)
Tatu Ulmanen
var milliseconds = parseInt($(this).find('td.date').html()); This to me doesn't seem like it wouldn't handle dates properly in at least some scenarios....it must be assuming a default date format? Wouldn't some usage of Date.Parse() be more useful?
tbone
@tbone, you're right, that was just a placeholder. I've added the Date.parse() part for clarity.
Tatu Ulmanen
Thanks Tatu. Are you familiar with date.js? It seems to solve many of my problems, and many more (Date.parse('today'), etc), but the downloadable code seems to be behind what is demonstrated in the code examples on the website.
tbone