473,466 Members | 1,349 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Change table background based on content

First of all let me say that I know very little about javascript.
what i need to do is to write a javascript functin which will change
the background color of the table rows based on entrees in the table.
See an example of the webpage that the script will work with here:
http://igorpetrusky.awardspace.com/RunStats.html

now, at the top there will be 3 buttons, which will call javascript
functions that will operate on tables 2 through N. Clicking the 1st
one will highlight rows of the table based on total clock cycles, which
is the entry of the 1st column of the table, 2nd button will highlight
based on values of 2nd column and 3rd button highlight based on values
of 3rd column. The highlighting will be done in such a way that the row
in EACH table with largest number in the controlling cell, 1st, 2nd or
3rd, will be shaded red, that is color =#FF0000 and the row with the
least number in the controlling cell will be shaded white, #FFFFFF.

question is, how do i do this?

If you show me an example for say a 2nd column, i can extrapolate to
what i need to do for 1rst and 3rd.

It is also desirable that the java script will have the smallest size,
because i have to encode it into a parser written by me in C, to output
a C code, which will then write the HTML file.

I am not posting any code here, but not because I am too lazy to write
it myself, I simply have no idea where to even start.

Thanks ahead.

Sep 28 '06 #1
11 2332
fr**********@yahoo.com wrote:

Hi,
First of all let me say that I know very little about javascript.
what i need to do is to write a javascript functin which will change
the background color of the table rows based on entrees in the table.
See an example of the webpage that the script will work with here:
http://igorpetrusky.awardspace.com/RunStats.html

now, at the top there will be 3 buttons, which will call javascript
functions that will operate on tables 2 through N. Clicking the 1st
one will highlight rows of the table based on total clock cycles, which
is the entry of the 1st column of the table, 2nd button will highlight
based on values of 2nd column and 3rd button highlight based on values
of 3rd column. The highlighting will be done in such a way that the row
in EACH table with largest number in the controlling cell, 1st, 2nd or
3rd, will be shaded red, that is color =#FF0000 and the row with the
least number in the controlling cell will be shaded white, #FFFFFF.
I don't really know whether the following will suffice, however it
should definitely get you started with your project. Simply copy paste
after the opening of the BODY tag, before your tables.

---
<form action="" onsubmit="return false">
<input type="button" value="Clock Cycles" onclick="highlight(0)">
<input type="button" value="Times Exec." onclick="highlight(1)">
<input type="button" value="Cycles per Exec." onclick="highlight(2)">
</form>

<script type="text/javascript">
with({
COLOR_MAX_VALUE:"#f00",
COLOR_MIN_VALUE:"#fff",
COLOR_NRM_VALUE:""
}){

var highlight=function(N){
var tables, ii, j, k, rows, v, mx, mn;

// For each table, except the first one
tables=document.getElementsByTagName("table");
for(ii=1;ii<tables.length;ii++) {
rows=tables[ii].rows;
v=[];

// store values in 'v'
for(j=1/*skip headers*/; j<rows.length; j++) {
v.push({
value:Number(getInnerText(rows[j].cells[N]))||0,
cells:rows[j].cells
});
highlightCells(rows[j].cells, COLOR_NRM_VALUE);
}

// sort
v.sort( function(a, b) { return (a.value<b.value) ? -1:1; } );
mn=v[0];
mx=v[v.length-1];

// highlight min
for(j=0; j<v.length; j++) {
if(v[j].value==mn.value) {
highlightCells(v[j].cells, COLOR_MIN_VALUE);
} else { break; }
}

// highlight max
for(j=v.length-1; j>=0; j--) {
if(v[j].value==mx.value) {
highlightCells(v[j].cells, COLOR_MAX_VALUE);
} else { break; }
}
}

function highlightCells(cells, color) {
for(var ii=0; ii<cells.length; ii++)
cells[ii].style.backgroundColor=color;
}

function getInnerText(el){
if(typeof el.innerText!="undefined") {
return (getInnerText=function(el){
return el.innerText;
})(el);
} else if(typeof el.textContent!="undefined") {
return (getInnerText=function(el){
return el.textContent;
})(el);
} else {
return (getInnerText=function(el){
var v="";
switch (el.nodeType) {
case 3:
v=el.nodeValue;
break;
case 1:
for(var ii=0; ii<el.childNodes.length; ii++)
v+=getInnerText(el.childNodes[ii]);
break;
}
return v;
})(el);
}
}

}
}
</script>
---
HTH.
Sep 28 '06 #2
Thank you ver much for you help, your scripts works wery well.
I was able to implement it with no problem. I will now try to extend it
to highlight the code with values less then max value but greater than
min value with a gradient of red to white. Ill post my progress as i
go.

Thanks again, very helpfull.

Sep 28 '06 #3
wrote on 28 Sep 2006 in comp.lang.javascript:
Thank you ver much for you help, your scripts works wery well.
"you" ??
I was able to implement it with no problem. I will now try to extend it
to highlight the code with values less then max value but greater than
min value with a gradient of red to white. Ill post my progress as i
go.
[please always quote on usenet]

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Sep 28 '06 #4
I have 2 more questions:

1. When i look at the HTML code created by FrontPage, it uses 6 symbols
for RGB color coding, for example white is FFFFFF, you use 3 symbols
FFF, does HTML interpreter automatically determine the colordepth,
based on the number of symbols used in RGB color code?

2. Lets say I have a color which in 256 color depth would be described
by a function 255^3+S1^2+S1, that is 255 red S1 Green and S1 Blue, S1 a
number between 0 and 255.

My question is, knowing S1, how could i assign the color to the cell
background?

Lets say I define a variable color as follows:

var color=255^3+S1^2+S1,

how would i assign that color to the cell? is the following a legal
call?

cells[ii].style.backgroundColor=color;

or do i need to somehow convert it to hex string?

Thanks ahead.

Sep 28 '06 #5
Google groups hates me, I tried to post a reply to your post earlier,
but its still did not gow through.

Anyways, here I go again

Thank you very much for the code you posted, it works very well. I
adjusted it a little bit, to allow gradient highlighting of all rows,
but it seems there is something wrong with my Color Map. I planned the
code to use RGB colormap, so then the rows with least value would be
shaded FFFFFF and with largest value shaded FF0000 here is the code i
changed in your function:

for(j=0; j<v.length; j++) {
var score=1-v[j].value/mx.value;
var color1=255*255*255+score*255*255+score*255;
highlightCells(v[j].cells, color1);
}
my next question is, how do i reset the color map to RGB? and also how
do I set its ColorDepth?

thanks ahead.

Sep 28 '06 #6
fr**********@yahoo.com wrote:

Hi,
Thank you very much for the code you posted, it works very well. I
adjusted it a little bit, to allow gradient highlighting of all rows,
but it seems there is something wrong with my Color Map.
First, you might want to get familiar with how colors are defined in
CSS, i.e. which values are authorized for the backgroundColor property:

<URL:http://www.w3.org/TR/CSS21/syndata.html#value-def-color>

I planned the
code to use RGB colormap, so then the rows with least value would be
shaded FFFFFF and with largest value shaded FF0000 here is the code i
changed in your function:

for(j=0; j<v.length; j++) {
var score=1-v[j].value/mx.value;
var color1=255*255*255+score*255*255+score*255;
highlightCells(v[j].cells, color1);
}
Would the following help?

---
//define variable "score" with other variables, at the top
//replace the min/max "for" loops by the following
for(j=0; j<v.length; j++) {
var score=Math.floor(
(1-(v[j].value-mn.value)/(mx.value-mn.value))*10
)/10;
highlightCells(
v[j].cells,
rgb2htmlColor(255, 255*score, 255*score)
);
}
---

.... with rgb2htmlColor defined as :

---
/* Source :
<URL:http://groups.google.fr/group/comp.lang.javascript/msg/8897dfcb5ff78736>
*/
function rgb2htmlColor(r,g,b){
var colorVal = (r << 16 | g << 8 | b).toString(16);
return "#"+("000000").substring(0,6-colorVal.length)+colorVal;
}
---
Regards,
Elegie.
Sep 28 '06 #7
JRS: In article <45***********************@news.free.fr>, dated Thu, 28
Sep 2006 10:58:30 remote, seen in news:comp.lang.javascript, Elegie
<el****@invalid.composted :
v.sort( function(a, b) { return (a.value<b.value) ? -1:1; } );
The comparison function of a .sort() is required to return 0 if the
elements are equal. If it does not do so, and elements are equal,
behaviour is undefined. It'll probably work in most systems, but not
necessarily in all.
The comparison function of a .sort() is required to return a Number, to
be negative, zero, or positive. There is no need to give a specific
value. If a.value and b.value are Numbers, the comparison can return
their difference. It'll work even if the difference is bigger than the
largest finite Number.

It's a good idea to read the newsgroup and its FAQ. See below.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 28 '06 #8
Dr John Stockton wrote:
> v.sort( function(a, b) { return (a.value<b.value) ? -1:1; } );
The comparison function of a .sort() is required to return 0 if the
elements are equal. If it does not do so, and elements are equal,
behaviour is undefined. It'll probably work in most systems, but not
necessarily in all.
Quite true, and actually the proposed implementation breaks the
transitivity rule; I do not think either (given my limited knowledge of
sort algorithms) that this should be a problem, however there is
certainly more in that code than a simply inaccurate formulation.
The comparison function of a .sort() is required to return a Number, to
be negative, zero, or positive. There is no need to give a specific
value. If a.value and b.value are Numbers, the comparison can return
their difference. It'll work even if the difference is bigger than the
largest finite Number.
Indeed; so in the end something like the following should be appropriate.

---
v.sort( function(a, b) { return a.value-b.value; } );
---

Thanks for pointing this out!
Sep 28 '06 #9
Thank you all for all your help with JavaScript.

I have updated my C profiler, which generates webpage to display
profile results and uses javascript on that page.

you can see the final result here:

http://igorpetrusky.awardspace.com/a...c_profiler.htm

scroll to the bottom of the page and click on "Output HTML file" link
to see the sample HTML file generated by my C profiler, that uses
JavaSrcipt you helped me with.

Best regards

Sep 29 '06 #10
yeah, but now the output looks way cool. Plus I have development
priorities to fixing the minor bugs, for example redoing the Code
string allocation by placing string constants in header of the file,
which should significantly improove execution time of profining app.

Sep 29 '06 #11
oops the last post was suppose to be for C/C++ board. sorry about that.

Sep 29 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Csaba2000 | last post by:
How do I detect when the font size has been changed (especially by user action: either Ctrl+Scroll wheel or View/Text Size)? This is just for use on IE 5.5+, but it would be great if there was a...
14
by: Reply Via Newsgroup | last post by:
Folks, Say I have a table, ten columns, ten rows - Each with a word in it. I want to change the values of some/all of the cells in the table via a hyperlink. How do I reference each cell and...
0
by: yurps | last post by:
Hello here is my html, if you click the missing image in the first column on the left, the div is shown, when clicked again the div disappears...but the bottom border disappears as well...Is there...
117
by: phil-news-nospam | last post by:
Is there really any advantage to using DIV elements with float style properies, vs. the old method of TABLE and TR and TD? I'm finding that by using DIV, it still involves the same number of...
5
by: outstretchedarm | last post by:
I'm extremely new to javascript and to programming in general. I am trying to create an interactive table. I have already created the table with constants, in the key of C (it is for music). ...
3
by: vj | last post by:
how to change images based on action. Even clicking changed images should do respective actions. and while displaying only one image at a time sholud get displayed. I am using three images for a...
1
by: MissMarie | last post by:
I've been playing around with DIV tables in myspace to better learn how to rewrite my own code for my business site without having to pay someone to design it. I've tried embedding a slideshow into...
1
by: rockdale | last post by:
Hi, all I am change my webpage to using div and css for my layout, it was using table before and I want to get rid of it. Basically it draws a box around my content using background pictures,...
3
dmjpro
by: dmjpro | last post by:
I think iText commonly used to create or manipulate PDF document in Java. So finally i decided to throw a question over here. For two days i am struggling. I encountered two types of table .....
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.