954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help needed in coloring html tables

hi,
i have a jsp page, in this page i am displaying data retrieved from mysql database. Now the problem is i must color table cell based on cell value. how to implement this.please give me an sample or working code foe this problem.( any code is accepted) preferably html or javascript.
i.e
if cell value is "late" then display that cell in red color
else
display the cell in green color
please help me.

Thanks ad Regards
Akash

akash_msrit
Light Poster
30 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 

Set the color property of text for the given td based on the value fetched from the tabe.

if value is 'late'
  <td style="color: red;">late</td>
else
  <td style="color: green;">on time<td>


You can use normal conditional statements to do this work though it would be better if you use JSTL tags to do conditional branching.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

hi

thanks for the reply. i tried this but i am not able to achieve what i need. if u have any other alternate code please forward me.


Thanks and Regards
Manjunath.S

akash_msrit
Light Poster
30 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 

I can churn out alternatives only if you provide me with the code you have so far. What I presented in my previous post was a fairly easy thing to have done. If you are not able to achieve it, post the most recent code.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

hi

hello friends, i have problem here, please help me.
i have a jsp page in that jsp page i am displaying the data retrived from the database(mysql). i am displaying data in table format.
noe the problem is i want to color table cell based on cell value. that is if table cell consists value say "met" then i want to color that table cell in green color. like this for other cell.
first i want check the value and then select bgcolor for that cell accordingl .

i need working code for this . or sample code for checking condition in tag.
this is the code i am using to display the data in html form
Type
IdNUM
ProdNum
SolID
Title
DevManager
Owner
Severity
Status
Analysis Due
Agoal
Pgoal
Igoal
Fgoal

<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc:odbc:mmt","root","admin");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(
"select Type,idnum,ProdNum,SolID,Title,DevManager,Owner,Severity,Status,AnalysisDue,Agoal,Pgoal,Igoal,Fgoal from wmr where ( SolInDev > 5 AND AnalysisDone=' ' ) AND (IsExternal='y')");
try {
String bgColor = "silver";
while (rs.next()) {bgColor = bgColor.equals("silver") ? "white" : "silver";%>
<%= rs.getString("Type") %>
<%= rs.getString("Idnum") %>
<%= rs.getString("ProdNum") %>
<%= rs.getString("SolId") %>
<%= rs.getString("Title") %>
<%= rs.getString("DevManager") %>
<%= rs.getString("Owner") %>
<%= rs.getString("Severity") %>
<%= rs.getString("status") %>
<%= rs.getString("AnalysisDue") %>
<%= rs.getString("Agoal") %>
<%= rs.getString("Pgoal") %>
<%= rs.getString("Igoal") %>
<%= rs.getString("Fgoal") %>


<% }
rs.close();
conn.close();
}catch (Exception e) {
out.println("Exception: " + e.getMessage());
} %>

akash_msrit
Light Poster
30 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 
<%
  String pGoal = rs.getString("pGoal");
  if(pGoal.equals("met"))
    out.println("<td style='background-color: green;'>" + pGoal + "</td>")
  else
   out.println("<td style='background-color: red;'>" + pGoal + "</td>")
%>


I would not recommend scriptlets but I don't think you care about it though...

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Why not create a stylesheet class for each color?
.

.bred {background-color: red;}
.bgrn {background-color: green;}


Then just insert the correct classes when you put the values in the tables, either directly or through a script:

<td class="bgrn">met</td>
MidiMagic
Nearly a Senior Poster
3,319 posts since Jan 2007
Reputation Points: 730
Solved Threads: 182
 

hi
Thanks for your effort and spending time for me. I will try this now.

Thanks and Regards
Akash

akash_msrit
Light Poster
30 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 

hi
Thanks for the reply,
the problem is here i am not inserting any values to the table. i am retreiving data from the database and displaying.

so i have to check what that cell conmtains .if that cell contains "met" then i want change the bgcolor of that cell to green. like that

can u help me now
Thnaks and Regards
akash

akash_msrit
Light Poster
30 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 

i tried that java scrit but i am geetting some other problem. that is nothing is displayed in that coloumn 0r cell .
if possible please embed that javascript in my code.
if u give me ur mail id i will post some report in which i need.
Thanks and Regards
akash

akash_msrit
Light Poster
30 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 

I have already answered your question in post #6. If you still can't figure that out, try reading some good JSP and Servlet tutorials till you have a good grasp of the subject matter.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You