unit-2e-html program
unit-2e-html program
The current state of your data table as of Figure 7.5 is that it's
serviceable but not very good-looking. It's a bit difficult to read,
to boot. You can improve these aspects of the table through
some judiciously applied CSS and JavaScript.
Here are some of the issues that it would be good to address in
the current table:
There are no visible divisions between the cells.
There are no large visual distinctions other than font weight
between the headers and the normal cells.
The numeric columns would look better right aligned.
It can be difficult for the eye to travel along a row of the table
without slipping to an adjacent row.
Listing 7-4
<!DOCTYPE html>
<html>
<head>
<title>Table With Styled Semantic Markup</title>
<style type="text/css">
/* Remove excess interstitial borders and padding
establish an outer border */
table {
border-collapse: collapse;
border: 1px solid #4C4C4C;(dark gray)
}
/* Add a dotted border around both header cells
and normal cells */
th, td {
border: 1px dotted #707070; (medium gray)
}
/* Add some padding around the header content
removing the cramped spacing. Make the background
color of the headers green. */
th {
background: #C2F0C2;
((padding: 5px;
}
/* Darken the green background for just the column
headers so that they use a different green than the
row headers. The fact that the column headers are
within a thead element helps to discriminate them
from the row headers. */
thead th {
background: #9BC09B;
}
/* Set the background behind the caption to a dark
gray and the caption text to white. */
caption {
background: #4C4C4C;
padding: 5px;
color: white;
}
/* This sets a solid bottom border to just the column
headers. This is achieved by using an attribute selector
to select just the th elements with scope set to col. */
th[scope=col] {
border-bottom: 2px solid #707070;
}
/* This sets a solid right border to just the row headers.
This is achieved by using an attribute selector to
select just the th elements with scope set to row. It
also left aligns these headers */
th[scope=row] {
border-right: 2px solid #707070;
text-align: left;
}
/* This right aligns all the normal cell content and
adds some padding to them */
td {
text-align: right;
padding: 5px;
}
/* This sets a different background to just the row
header that resides in the footer row. */
tfoot th {
background: #BBBBD1;
}
/* This sets a different background to just the
normal cells that reside in the footer row. */
tfoot td {
background: #CFCFDF;
color: #8D1919;
}
</style>
</head>
<body>
<table>
<caption>Sales By Region</caption>
<colgroup>
<col class="col-header">
<col class="col-amount">
<col class="col-mean">
</colgroup>
<thead>
<tr>
<th scope="col">Region</th>
<th scope="col">Sales</th>
<th scope="col">Mean</th>
</tr>
</thead>
<tfoot>
<tr>
<th scope="row">Sum</th>
<td>$1,000,000</td>
<td></td>
</tr>
</tfoot>
<tbody>
<tr>
<th scope="row">Northeast</th>
<td>$100,000</td>
<td>$142,857</td>
</tr>
<tr>
<th scope="row">Southeast</th>
<td>$75,000</td>
<td>$142,857</td>
</tr>
<tr>
<th scope="row">Midwest</th>
<td>$125,000</td>
<td>$142,857</td>
</tr>
<tr>
<th scope="row">Mid-Atlantic</th>
<td>$125,000</td>
<td>$142,857</td>
</tr>
<tr>
<th scope="row">Southwest</th>
<td>$75,000</td>
<td>$142,857</td>
</tr>
<tr>
<th scope="row">Northwest</th>
<td>$100,000</td>
<td>$142,857</td>
</tr>
<tr>
<th scope="row">California</th>
<td>$400,000</td>
<td>$142,857</td>
</tr>
</tbody>
</table>
</body>
</html>
Listing 7-5
<!DOCTYPE html>
<html>
<head>
<title>Table With Alternating Row Highlight</title>
<script src="jquery/jquery-1.11.1.min.js"></script>
<style type="text/css">
table {
border-collapse: collapse;
border: 1px solid #4C4C4C;
}
th, td {
border: 1px dotted #707070;
}
th {
background: #C2F0C2;
padding: 5px;
}
thead th {
background: #9BC09B;
}
caption {
background: #4C4C4C;
padding: 5px;
color: white;
}
th[scope=col] {
border-bottom: 2px solid #707070;
}
th[scope=row] {
border-right: 2px solid #707070;
text-align: left;
}
td {
padding: 5px;
}
tfoot th {
background: #BBBBD1;
}
tfoot td {
background: #CFCFDF;
color: #8D1919;
}
.row-odd {
background: #DBDBDB;
}
.row-odd th {
background: #A2D0A2;
}
.cell-number {
text-align: right;
}
</style>
<script type="text/javascript">
function usCurrencyToFloat(currency) {
currency = currency.replace(/\$/g, '');
currency = currency.replace(/,/g, '');
return parseFloat(currency);
}
$(function () {
$("tbody").find("tr:odd").addClass("row-odd");
$("tbody").find("td").filter(function (index) {
return !isNaN(usCurrencyToFloat($(this).text()));
}).addClass("cell-number");
});
</script>
</head>
<body>
<table>
<caption>Sales By Region</caption>
<colgroup>
<col class="col-header">
<col class="col-amount">
<col class="col-mean">
</colgroup>
<thead>
<tr>
<th scope="col">Region</th>
<th scope="col">Sales</th>
<th scope="col">Mean</th>
</tr>
</thead>
<tfoot>
<tr>
<th scope="row">Sum</th>
<td>$1,000,000</td>
<td></td>
</tr>
</tfoot>
<tbody>
<tr>
<th scope="row">Northeast</th>
<td>$100,000</td>
<td>$142,857</td>
</tr>
<tr>
<th scope="row">Southeast</th>
<td>$75,000</td>
<td>$142,857</td>
</tr>
<tr>
<th scope="row">Midwest</th>
<td>$125,000</td>
<td>$142,857</td>
</tr>
<tr>
<th scope="row">Mid-Atlantic</th>
<td>$125,000</td>
<td>$142,857</td>
</tr>
<tr>
<th scope="row">Southwest</th>
<td>$75,000</td>
<td>$142,857</td>
</tr>
<tr>
<th scope="row">Northwest</th>
<td>$100,000</td>
<td>$142,857</td>
</tr>
<tr>
<th scope="row">California</th>
<td>$400,000</td>
<td>$142,857</td>
</tr>
</tbody>
</table>
</body>
</html>
This defines a CSS style rule that targets any element that has the
class row- odd. You use jQuery to make sure that the odd rows have
this class assigned. This rule assigns a different background color
to the odd rows so that the background color alternates as you
achieved earlier through the nth-child selector.
Next is the following rule, which targets only <th> elements that
are in odd
rows, coloring their background a darker version of the row
header color.
.row-odd th {
background: #A2D0A2;
}
This rule assumes that you'll later use jQuery to assign a class
called cell- number to any cell that appears to contain a number,
in order that they all be right-aligned.
The following is the first actual JavaScript you'll employ to
manipulate the table:
<script type="text/javascript">
function usCurrencyToFloat(currency) {
currency = currency.replace(/\$/g, '');
currency = currency.replace(/,/g, '');
return parseFloat(currency);
}
$(function () {
$("tbody").find("tr:odd").addClass("row-odd");
$("tbody").find("td").filter(function (index) {
return !isNaN(usCurrencyToFloat($(this).text()));
}).addClass("cell-number");
});
</script>
uses jQuery to invoke some code when the DOM is ready. In the
ready callback, it invokes:
$("tbody").find("tr:odd").addClass("row-odd");
which uses jQuery to find just the odd rows from within the body
of the table and adds the class row-odd to them. Then it calls
$("tbody").find("td").filter(function (index) {
return !isNaN(usCurrencyToFloat($(this).text()));
}).addClass("cell-number");