Click here to Skip to main content
15,905,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an HTML page that displays Books and their status as to whether they are In Stock, Out of Stock, Arriving Soon, or Preorder. The HTML page reads this information from a XML file. Snippets are below:
HTML
HTML
<body>
<table width="30%" border="0" align="left" cellspacing="1" font size="10">
  <tr bgcolor="#CCCCCC">
    <td colspan="2">
<table width="100%"  datasrc="#nb_fict" font size="1" border="0" align="center" bordercolor="#FFFFFF">
<caption>
<span>Fiction</span>
</caption>
<thead>
<tr>
<th span class="style1">ProductID</th></span>
<th span class="style1">Title</th></span>
<th span class="style1">Author</th></span>
<th span class="style1">Pub. Date</th></span>
<th span class="style1">Stock</th></span>
<th span class="style1">No. In Stock</th></span>
<th span class="style1">Holding</th></span>
</tr>
</thead>
<tbody>
<tr>
<td><span class="style1" datafld="prodID"></td></span>
<td><span class="style1" datafld="Title"></td></span>
<td><span class="style1" datafld="Author"></td></span>
<td><span class="style1" datafld="Date"></td></span>


<td><span class="style1" datafld="Stock"></td></span>
<td><span class="style1" datafld="NumberStock"></td></span>
<td><span class="style1" datafld="holding"></td></span>

					
					
				  </tr>
				  </tbody>
				</table>
				
	</td>
  </tr>


Now, I have have different XML file names by the Genre.
What I want to do is change the row color in the table depending on what is in the Stock field. For example, if it's IS for "Instock" I want it to be green. If it's PO for "Preorder" I want it to display orange. I'm assuming I would have to maybe use Java Script for this, but I don't know how to do it. Any help would be much appreciated. Thanks!

Whoops! Sorry for using the "Answer Question" button for my own comments. Wasn't really sure how to do that, but thanks for letting me know.
If someone has any ideas, please read through the "answers" as some of those posts were mine.

Come on. I know someone can answer this. I think it's so close and probably just something small I'm missing. Any help would really be appreciated.
Posted
Updated 11-Apr-12 3:06am
v3

take asp table it has some more properties and methods...
 
Share this answer
 
I would hate to have to re-code all those tables. Is there any way it can be done the way I have it?
 
Share this answer
 
Hi there, you can use javascript to achieve the changing of row color depending on a particular data. You can change the style of the element in javascript like this:

JavaScript
function changeRowColor {
  el = document.findElementById('elName');
  // then you can add a condition here. if the value of the element is "InStock" then the color is green otherwise set the color to whatever color you prefer.
  // you can set the color of the element using,
  el.style.color = "green";
}

and then you can invoke this function in the appropriate event.
But, the most effective way of doing this is to use AJAX.
 
Share this answer
 
v2
Thanks whiz.....I know your solution will work, but I can't seem to get it there. Here is what I've written, if you could take a look and tell me what I have wrong?

function changeRowColor {
e1 = document.findElementByID('Stock');
if (e1='IS')
{
e1.style.color = "green"
}
}

I apprecitate it new to javascript.
 
Share this answer
 
Ok, so I was given the following code to put in the head section. It looks like it should work, but it doesn't. Can someone take a look and see why it may not be working?


C#
<script type="text/javascript">
function fixStatus( )
{
    var rows = document.getElementsByTagName("tr");
    for ( var r = 0; r < spans.length; ++r )
    {
        var row = rows[r];
        var spans = row.getElementsByTagName("span");
        for ( var s = 0; s < spans.length; ++s )
        {
            var span = spans[s];
            if ( span.datafld != null && span.datafld == "status" )
            {
                var val = span.innerHTML.toUpperCase();
                switch ( val )
                {
                    case "IS":
                        row.style.color = "green";
                        break;
                    case "OS":
                        row.style.color = "red";
                        break;
                    case "PO":
                        row.style.color = "blue";
                        break;
                    default:
                        row.style.color = "orange"; // ?? unknown status ??
                        break;
                } // end of switch
                break; // out of for loop on spans
            } // end of if-status-span found
        } // end of spans loop
    } // end of rows loop
}

window.onload = fixStatus;
</script>
 
Share this answer
 
Comments
Nelek 10-Apr-12 15:11pm    
Please, don't get used to add answers when you are answering other people. The "add answer" is to give a solution to the problem of the question.

If you want to add information to the original question, use the link "improve question" on the right side below your original message.

If you want to answer or comment anything to someone that answered you, then use the "have a question or comment?" button (as I am doing right now with you)

That will increase the readability of the thread, the answers might no stay in chronological order, so it all ends a bit messy.
You can try my sample code below. I used javascript function that takes a parameter id from the onload event of the body tag. Now, all you have to do is to have a workaround on how to get the value of each child elements of the rows/cells if such rows/cells contain child elements like the <span> element and assign the appropriate color for each rows/cells.
XML
<html>
<head>
</head>
<script type="text/javascript">
   function setRowColor(id) {
    var table = document.getElementById("myTable");
    for (var i = 1; i < table.rows.length; i++) {
     var value = table.rows[i].cells[0].innerHTML.toUpperCase();
     var color = "";
     var bgcolor = "white";
     switch(value) {
       case "IS": color = "green"; bgcolor = "black"; break;
       case "OS": color = "red"; break;
       case "PO": color = "blue"; break;
       default: color = "orange"; bgcolor="black"; break;
     }
     table.rows[i].cells[0].style.color = color;
     table.rows[i].cells[0].style.backgroundColor = bgcolor;
     table.rows[i].cells[1].style.color = color;
     table.rows[i].cells[1].style.backgroundColor = bgcolor;
    }
   };
</script>
<body onload="setRowColor('myTable')">
 <table border=1 id="myTable">
   <tr>
     <th>Name</th>
     <th>Color</th>
   </tr>
   <tr>
     <td>IS</td>
     <td>Green</td>
   </tr>
   <tr>
     <td>OS</td>
     <td>Red</td>
   </tr>
   <tr>
     <td>PO</td>
     <td>Blue</td>
   </tr>
   <tr>
     <td>Unknown</td>
     <td>Orange</td>
   </tr>
 </table>
</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900