Anubhavk,
Develop classnames and associated CSS directives to give the various visual effects you want. Test out your CSS with hard-coded HTML.
Build your real table with :
- <table class="highlightTable">
- <td class="time"> (your time cell in each row)
- <td class="aaaa"> (your other key data cell in each row)
Now start to develop your jQuery. The general format of the code will be :
$(function(){
$('.highlightTable tr').each(function(){
var $this = $(this);
var timeValue = $this.find('td.time').html();//the value in the time cell in this row
var aaaaValue = $this.find('td.aaaa').html();//the value some other cell in this row
$this.addClass(time_class(timeValue));
$this.addClass(sidebar_class(aaaaValue));
});
});
By implementing the tests in functions (time_class() and sidebar_class() above), you will keep the master routine nice and clean.
Your major effort will be in developing the CSS, then in developing the javascript in time_class() and sidebar_class() . These functions should return classnames (or "") to apply the CSS directives you developed earlier.
Airshow
Airshow
WiFi Lounge Lizard
2,782 posts since Apr 2009
Reputation Points: 370
Solved Threads: 389
Skill Endorsements: 10
It doesn't matter that the table is already developed by some other fellow. The worst case is that you will need to modify his work slightly, but maybe not depending on what he wrote.
I have given you a master javascript routine. To make it work, you need to write the subroutines time_class() and sidebar_class().
Each function must return one of several classnames and thus apply the required CSS style to each table row.
If I do the whole job for you then you will not have learned anything. Give it a try and ask specific questions when you get stuck.
Airshow
Airshow
WiFi Lounge Lizard
2,782 posts since Apr 2009
Reputation Points: 370
Solved Threads: 389
Skill Endorsements: 10
Anubhavk,
What is the format of the times in the time column please?
Airshow
Airshow
WiFi Lounge Lizard
2,782 posts since Apr 2009
Reputation Points: 370
Solved Threads: 389
Skill Endorsements: 10
Is the time always in minutes or can it ever be hours/minutes or even days/hours/minutes?
If so, can you provide examples please.
Airshow
WiFi Lounge Lizard
2,782 posts since Apr 2009
Reputation Points: 370
Solved Threads: 389
Skill Endorsements: 10
OK, the string probably needs processing to find an actual date/time. This may be necessary because Month length varies (29/30/31 days).
However, if your color coding requirement goes to a maximum at less than a month then interpreting the sting simplifies considerably.
Another line of thought ... when the "xxxx ago" string is built (server-side), presumably it starts out as a date (in PHP, JSP, ASP or whatever). If you have access to the server-side source, then we code the original date invisibly into the HTML and avoid having to parse out the "xxxx ago" string to get back to the date. That would be ideal but clearly only possible if you control the server-side source, not if you screen-scrape.
I'm away for a few hours, will look in again when I get back.
Airshow
Airshow
WiFi Lounge Lizard
2,782 posts since Apr 2009
Reputation Points: 370
Solved Threads: 389
Skill Endorsements: 10
Anubhavk,
OK, I have found a way to do this without working out the original date/time. Simply parse the "n unit ago" string to extract n and unit, then decide on a className accordingly.
Here's the function:
//color values picked from: http://www.somacon.com/cgi/colorchart.pl
function time_class(agoString) {
var agoValues = agoString.split(/\s+/);
var n = Number(agoValues[0]);
var unit = agoValues[1].toLowerCase();
switch(unit){
//minutes
case 'm':
case 'min':
case 'mins':
case 'minute':
case 'minutes':
clss = (n<5) ? 'minutes_A' : (n<15) ? 'minutes_B' : (n<30) ? 'minutes_C' : 'minutes_D';
break;
//hours
case 'h':
case 'hr':
case 'hrs':
case 'hour':
case 'hours':
clss = (n<3) ? 'hours_A' : (n<6) ? 'hours_B' : (n<12) ? 'hours_C' : 'hours_D';
break;
//days
case 'd':
case 'day':
case 'days':
clss = (n<4) ? 'days_A' : (n<8) ? 'days_B' : (n<15) ? 'days_C' : 'days_D';
break;
//months
case 'month':
case 'months':
clss = (n<3) ? 'months_A' : (n<6) ? 'months_B' : (n<12) ? 'months_C' : 'months_D';
break;
default:
clss = '';
};
return clss;
};
As you can see I have allows for some variation in the unit string (eg. m, min, mins, minute, minutes), just in case.
I tested with the following CSS :
/* color values picked from: http://www.somacon.com/cgi/colorchart.pl */
.minutes_A { background-color: #D2FFBF; }
.minutes_B { background-color: #A6FF7F; }
.minutes_C { background-color: #49EE00; }
.minutes_D { background-color: #3FCD00; }
.hours_A { background-color: #FF9F3F; }
.hours_B { background-color: #FF7F00; }
.hours_C { background-color: #EE7700; }
.hours_D { background-color: #CD6600; }
.days_A { background-color: #7FBFFF; }
.days_B { background-color: #3F9FFF; }
.days_C { background-color: #007FFF; }
.days_D { background-color: #0066CD; }
.months_A { background-color: #FF7F7F; }
.months_B { background-color: #FF3F3F; }
.months_C { background-color: #EE0000; }
.months_D { background-color: #CD0000; }
If you want to style the text rather than the background, then change background-color to color all through.
After that, the world is your oyster with regard to styles; bold, italic, letter spacing etc. etc.
Airshow
Airshow
WiFi Lounge Lizard
2,782 posts since Apr 2009
Reputation Points: 370
Solved Threads: 389
Skill Endorsements: 10
Anubhavk,
Let's not worry about server-side for now. If the solution in my post above at top of page 2 works, then it avoids needing to know the original date, hence avoids needing to address server-side code.
Please let me know if post at top of page 2 is any good.
Airshow
Airshow
WiFi Lounge Lizard
2,782 posts since Apr 2009
Reputation Points: 370
Solved Threads: 389
Skill Endorsements: 10
Anubhavk,
If you want to do the whole thing server-side then, you need to build your HTML to achieve similar to what is performed client-side by my code above. ie. add a className to each <td class="time"> to give it additional style directives. Choose the stylename in accordance with the staleness of the data, eg. <td class="time staleness_X"> .
Of course, this server-side approach would provide strictly one-off styling for the page. With a little extra work, the client-side approach would allow the color coding to be "live", ie. to change colors as the data gets staler and staler without refreshing the page. Given the nature of the data, I think that would be an end-user expectation.
To facilitate that, you would need to go back to my earlier idea of encoding time-stamps in the HTML. By far the simplest encoding is a UNIX epoch representation of the time. ie, seconds since 1 January 1970. This is available in just about all server-side languages and you may well be using it already to perform date subtraction where the "xxx ago" string is computed, and/or in a database 'timestamp' field.
You can encode a time-stamp as a custom attribute of the table row or table cell to which it applies.
<tr class="..." timestamp="1326966381">...</tr>
This will be readily picked up by jQuery and converted to a javascript Date() object (which works in milliseconds rather than seconds). Here's a sample code fragment which returns a javascript Date object.
var timestamp = new Date(Number($('tr-selector').attr('timestamp')) * 1000);
You then have many Date() methods available to you for the client-side testing, manipulation and display of the timestamp.
Airshow
Airshow
WiFi Lounge Lizard
2,782 posts since Apr 2009
Reputation Points: 370
Solved Threads: 389
Skill Endorsements: 10
I am sure the end result will be very cool :cool:
Airshow
Airshow
WiFi Lounge Lizard
2,782 posts since Apr 2009
Reputation Points: 370
Solved Threads: 389
Skill Endorsements: 10