Professional Documents
Culture Documents
DV Unit-2 (Theory)
DV Unit-2 (Theory)
What is JSON?
2|Page
Reading JSON file using JavaScript & jQuery
● For Parsing JSON data to HTML Table, we are going to use Jquery’s getJSON() method.
● Example: here is the sample json file (sample.json). We want to display all the content of
this json file in the browser within the HTML Table.
● Sample.json file:
3|Page
Outputting Basic Table Data
● In previous topics, we have seen that how can we display dynamic txt, css, xml and json
data with the help of various jquery ajax methods.
● These strategies are useful and powerful, but they have the distinct problem that if the user’s
browser does not support JavaScript, or if the user has intentionally configured the browser
to not execute JavaScript, then you are not able to present them with any data.
● Meanwhile, if the data table is a static part of the page or is injected into the page by logic
on the server side, then it is easily handled by base-level user agents also.
● So that, now we are learning how you can create static html table, semantic html table, how
you can get maximum readability and many more topics.
‹!DOCTYPE html> //represents the document type, and helps browsers to display web pages
correctly.
<html>
<head>
//<head> tag is a container for <title>, <link>, <meta>, <style>, <script>, … etc.
</head>
<body>
//<body> tag is used to contain a web page's content, including hyperlinks, images, tables, text,
etc.
</body>
</html>
● The HTML <table> element represents tabular data — that is, information presented in a
two-dimensional table comprised of rows and columns of cells containing data.
● HTML tables allow web developers to arrange data into rows and columns.
4|Page
● The HTML tables are created using the <table> tag in which the <tr> tag is used to create
table rows and <td> tag is used to create data cells/columns. The elements under <td> are
regular and left aligned by default.
Example-1
<html>
<head>
</head>
<body>
<table>
<tr>
<td>No</td>
<td>Month</td>
<td>Savings</td>
</tr>
<tr>
<td>1</td>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>2</td>
<td>February</td>
<td>$80</td>
</tr>
</table>
</body>
5|Page
</html>
Output:
● There is an implication that the first (top) row contains cells that represent the headers for
the columns below each of them (title of the column).
● There is also an implication that the first cell in each row describes the subsequent row
content (Ex: sequence no. for data in table).
● For applying separate formatting to these headings, we have to use semantic markup.
● Basically, semantic markup means apply some style to the row/column heading so that
those can be viewed differently with compare to other table data.
● You can solve previous problem (separating data and heading), by better defining the
semantics, or meaning, of each piece of markup in the table.
● If the cells in the first row are meant to be the headers of the columns, then you can make
that content to heading explicitly by using the <th> element rather than the <td> element.
6|Page
otherwise you can use <th> element in any row. Headings, which are defined in <th> tag
are centered and bold by default.
● Example:
<html>
<head>
</head>
<body>
<table>
<tr>
<th>No</th>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>1</td>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>2</td>
<td>February</td>
<td>$80</td>
</tr>
</table>
</body>
</html>
● But now assume one scenario where you have two different sets of headers, and you want
to apply different formatting to both the headers. To resolve this ambiguity, you can add
scope attribute to the header elements.
Example: ‹th scope="row">No</th›
● it will make no difference in our table, but you’ve now able to separate that header by
appling separate css to that particular row header. Similarly, for column header
7|Page
‹th scope=“co1"›Month‹/th›
‹th scope=“co1"›savings‹/th›
● Here, the border is an attribute of <table> tag and it is used to put a border across all the
cells. If you do not need a border, then you can use border = "0".
● Example:
<table border=“1”>
….
</table>
● There are two attributes called cellpadding and cellspacing which you will use to adjust the
white space in your table cells. The cellspacing attribute defines space between table cells,
while cellpadding represents the distance between cell borders and the content within a cell.
● Example:
….
</table>
● You will use colspan attribute if you want to merge two or more columns into a single
column. Similar way you will use rowspan if you want to merge two or more rows.
● Example:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
8|Page
<table border = "1">
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr><tr>
<td rowspan = "2">Row 1 Cell 1</td>
<td>Row 1 Cell 2</td>
<td>Row 1 Cell 3</td>
</tr>
<tr>
<td>Row 2 Cell 2</td>
<td>Row 2 Cell 3</td>
</tr>
<tr>
<td colspan = "3">Row 3 Cell 1</td>
</tr>
</table>
</body>
</html>
Output:
Tables Backgrounds
You can set table background using one of the following two ways −
● bgcolor attribute − You can set background color for whole table or just for one cell.
● background attribute − You can set background image for whole table or just for one cell.
9|Page
You can also set border color using bordercolor attribute.
● You can set a table width and height using width and height attributes. You can specify
table width or height in terms of pixels or in terms of percentage of available screen area.
Example:
Table Caption
● The caption tag will serve as a title or explanation for the table and it shows up at the top
of the table.
● Example:
</table>
● Tables can be divided into three portions − a header, a body, and a foot. The head and foot
are rather similar to headers and footers in a word-processed document that remain the
same for every page, while the body is the main content holder of the table.
● The three elements for separating the head, body, and foot of a table are −
10 | P a g e
<tbody> − to indicate the main body of the table.
● A table may contain several <tbody> elements to indicate different pages or groups of data.
● Example:
<!DOCTYPE html> <html>
<head> </head>
<body>
<table border = "1" width = "100%">
<thead>
<tr>
<td colspan = "4">This is the head of the table</td> </tr>
</thead>
<tfoot>
<tr>
<td colspan = "4">This is the foot of the table</td>
</tr>
</tfoot>
<tbody>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td></Cell 3td>
<td>Cell 4</td>
</tr>
</tbody>
</table>
</body>
</html>
Output:
11 | P a g e
Nested Table
● You can use one table inside another table. Not only tables you can use almost all the tags
inside table data tag <td>.
● Example: Following is the example of using another table and other tags inside a table cell.
<head> </head>
<body>
<tr>
<td>
<tr>
<th>Name</th>
<th>Salary</th>
</tr>
<tr>
<td>ABC</td>
<td>30000</td>
</tr>
<tr>
<td>DEF</td>
<td>40000</td>
</tr>
12 | P a g e
</table>
</td>
</tr> </table>
</bod> </html>
Output:
What is CSS?
● Before CSS, tags like font, color, background style, element alignments, border and size
had to be repeated on every web page. This was a very long process. For example: If you
are developing a large website where fonts and color information are added on every single
page, it will be become a long and expensive process. CSS was created to solve this
problem.
● CSS style definitions are saved in external CSS files so it is possible to change the entire
website by changing just one file.
● CSS provides more detailed attributes than plain HTML to define the look and feel of the
website.
13 | P a g e
CSS Syntax
● Selector: Selector indicates the HTML element you want to style. It could be any tag like
<h1>, <title> etc.
● Declaration Block: The declaration block can contain one or more declarations separated
by a semicolon. For the above example, there are two declarations: color: yellow; font-size:
11 px;
● Each declaration contains a property name and value, separated by a colon.
● Property: A Property is a type of attribute of HTML element. It could be color, border etc.
● Value: Values are assigned to CSS properties. In the above example, value "yellow" is
assigned to color property.
● Selector{Property1: value1; Property2: value2; ..........;}
CSS Selector
● CSS selectors are used to select the content you want to style. Selectors are the part of CSS
rule set. CSS selectors select HTML elements according to its id, class, type, attribute etc.
● There are several different types of selectors in CSS.
1. CSS Element Selector
2. CSS Id Selector
3. CSS Class Selector
4. CSS Universal Selector
<!DOCTYPE html>
14 | P a g e
<html>
<head>
<style>
p{
text-align: center;
color: blue; }
</style>
</head>
<body>
<p>Paragraph<p>
<p>Paragraph<p>
</body>
</html>
2. CSS Id Selector
● The id selector selects the id attribute of an HTML element to select a specific element. An
id is always unique within the page so it is chosen to select a single, unique element.
● It is written with the hash character (#), followed by the id of the element.
● Let’s take an example with the id "para1".
<!DOCTYPE html>
<html>
<head>
<style>
#para1 {
text-align: center;
color: blue; }
15 | P a g e
</style>
</head>
<body>
<p id="para1">Hello</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
.center {
text-align: center;
color: blue; }
</style>
</head>
<body>
</body>
</html>
16 | P a g e
4. CSS Universal Selector
● The universal selector is used as a wildcard character. It selects all the elements on the
pages.
<!DOCTYPE html>
<html>
<head>
<style>
*{
color: green;
font-size: 20px; }
</style>
</head>
<body>
<h2>This is heading</h2>
</body>
</html>
<head>
<title>HTML Table</title>
<style>
17 | P a g e
</style>
</head>
Complete Code:
<!DOCTYPE html>
<html>
<head>
<title>HTML Table</title>
<style>
</style>
</head>
<body>
<thead>
<tr>
</tr>
</thead>
<tbody>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
18 | P a g e
<td>Cell 4</td>
</tr>
</tbody>
<tfoot>
<tr>
</tr>
</tfoot>
</table>
</body>
</html>
Output:
Configuring a column
● We have seen that how we can apply formatting to specific table, tbody, thead, tfoot etc.
● Now, we want to apply different different formatting to different different columns.’
● For that, we have to use colgroup tag.
colgroup tag
● The <colgroup> tag specifies a group of one or more columns in a table for formatting.
● The <colgroup> tag is useful for applying styles to entire columns, instead of repeating the
styles for each cell, for each row.
● Note: The <colgroup> tag must be a child of a <table> element, after any <caption>
elements and before any <thead>, <tbody>, <tfoot>, and <tr> elements.
● To define different properties to a column within a <colgroup>, use the <col> tag within
the <colgroup> tag.
19 | P a g e
Example: Ex. Of showing a colgroup that has three columns of different widths
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>This example shows a colgroup that has three columns of different widths:</p>
<colgroup>
</colgroup
<tr>
<td>col 1</td>
<td>col 2</td>
20 | P a g e
<td>col 3</td>
</tr>
</table>
</body>
</html>
● Specific Attributes of col group tag: The HTML <colgroup> tag also supports the following
additional attributes –
Span number Defines the number of columns the <col> should span, not
supported in Html5.
Width pixels or % Specifies a default width for each column spanned by the
current col element, not supported in Html5 .
Example: we want to apply different background color to different columns having different
classes
CSS:
<style>
21 | P a g e
.col-header {background-color:yellow;}
.col-body {background-color:green;}
.col-footer {background-color:gray;}
</style>
Class:
<colgroup>
</colgroup>
Complete Code:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.col-header {background-color:yellow;}
.col-body {background-color:green;}
.col-footer {background-color:gray;}
</style>
</head>
<body>
<colgroup>
22 | P a g e
<col class = "col-body"></col>
</colgroup>
<thead>
<tr>
<td>First-column-Heading</td>
<td>Second-column-Heading</td>
<td>Third-column-Heading</td>
</tr>
</thead>
<tbody>
<tr>
<td>First-column-body</td>
<td>Second-column-body</td>
<td>Third-column-body</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>First-column-footer</td>
<td>Second-column-footer</td>
<td>Third-column-footer</td>
</tr>
</tfppt>
</table>
</body>
23 | P a g e
</html>
Output:
● Sometimes it may be a case, where all the formatting are not supported by all the browsers
and in this case, browser will omit formatting which is not supported by it.
● But, if you are carful, you should end up with a maximum enhanced table for every
scenario.
<!DOCTYPE html>
<html>
<head>
<style>
24 | P a g e
thead { color: orange; }
</style>
</head>
<body>
<table>
<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>
25 | P a g e
<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>
26 | P a g e
</tr>
<tr>
<th scope="row">California</th>
<td>$400,000</td>
<td>$142,857</td>
</tr>
</tbody>
</table>
</body>
</html>
● The current state of our previous data table is that it’s serviceable but not very good-
looking. It’s a bit difficult to read. You can improve these aspects of the table through some
applied CSS and JavaScript. Some of your target platforms will not be able to take
advantage of some of these features, but any features you attempt to use that aren’t
supported should silently be ignored.
Here are some of the issues that it would be good to address in the current table:
● After considering above issues, I have designed a table that can achieve maximum
readability by applying some formatting to previous table.
● Final Output after achieving maximum readability:
27 | P a g e
Code for above table which achieves readibility:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
table {
border-collapse: collapse;
th, td {
/* Add some padding around the header content. Make the background
28 | P a g e
color of the headers green. */
th {
background: #C2F0C2;
padding: 5px;
row headers. */
thead th {
background: #9BC09B;
caption {
background: #4C4C4C;
padding: 5px;
color: white;
headers. . */
th[scope=col] {
29 | P a g e
th[scope=row] {
text-align: left;
td {
text-align: right;
padding: 5px;
tfoot th {
background: #BBBBD1;
tfoot td {
background: #CFCFDF;
color: #8D1919;
</style>
</head>
30 | P a g e
<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>
31 | P a g e
<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>
32 | P a g e
</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>
33 | P a g e
Limitations of CSS
● The CSS required to create alternating row highlights actually turns out to be really concise.
The downside, however, is that nth-clild is actually a CSS level 3 selector, so it is only
available in the most modern browsers. Older browsers just ignore the rule, so this rule still
safely and gracefully degrades if someone tries to view it on a noncompliant browser.
● If you drop some of the concision, you can support alternating highlights on older browsers
also. One way of approaching this is to manually add a class to all the odd rows of the table
and then select on that class directly in order to highlight the odd rows. If you were
generating the table using some code on the server, this method would be relatively
straightforward. You could just make sure to emit c lass= "odd for every other row in the
markup generated for the table.
● However, there is a distinct downside to approaching this problem in this way. If you were
to use some client-side code to change the sorting of the rows, you would also need to
reassign all the classes based on the new row order.
● Another approach is to use jQuery to manipulate the rows to implement the alternating row
highlights, one of the benefits of jQuery is that it can help you emulate some more modern
browser features on older browsers.
How to highlight alternate table row using jQuery (Increasing readability using jQuery)
34 | P a g e
Parameter Description
formula Specifies which child element(s) to be selected with a formula (an + b).
Example: p:nth-child(3n+2) selects each 3rd paragraph, starting at the 2nd child
element
Code: Add this code in the head section of previous table for applying alternative
heighlighting
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
});
</script>
● jQuery comes with a hover() mouse event to allow attach two event handlers to the matched
elements, executed when the mouse enters and leaves the matched elements.
35 | P a g e
Here, A – function to call when the mouse enters the matched element.
Example:
<html>
<body>
<table border="1">
<tr><th>No</th><th>Name</th><th>Age</th><th>Salary</th></tr>
<tr><td>1</td><td>ABC</td><td>23</td><td>$100,000</td></tr>
<tr><td>2</td><td>DEF</td><td>24</td><td>$90,000</td></tr>
<tr><td>3</td><td>GHI</td><td>22</td><td>$50,000</td></tr>
<tr><td>4</td><td>JKL</td><td>25</td><td>$40,000</td></tr>
<tr><td>5</td><td>MNO</td><td>21</td><td>$30,000</td></tr>
</table>
<script type="text/javascript">
$("tr").hover(
function () {
$(this).css("background","yellow");
},
function () {
$(this).css("background","");
);
36 | P a g e
</script>
</body>
</html>
Output:
Including Computations
● By using jQuery you can perform some calculations also like sum, multiply, divide and
many more in basic html table.
● Example: suppose it is the case where, we want to find out total salary of all the employees.
This sum can be dynamically calculated by jQuery.
<html>
<head>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script type="text/javascript">
$(function () {
var grandTotal = 0;
$("[id*=amtlbl]").each(function (){
});
37 | P a g e
});
</script>
</head>
<body>
<table border="1">
<tr><th>No</th><th>Name</th><th>Salary</th></tr>
<tr><td>1</td><td>ABC</td><td id=amtlbl_0>100000</td></tr>
<tr><td>2</td><td>DEF</td><td id=amtlbl_1>90000</td></tr>
<tr><td>3</td><td>GHI</td><td id=amtlbl_2>50000</td></tr>
<tr><td>4</td><td>JKL</td><td id=amtlbl_3>40000</td></tr>
<tr><td>5</td><td>MNO</td><td id=amtlbl_4>30000</td></tr>
<tr><td></td><td></td><td id=totalbl></td></tr>
</table>
</body>
</html>
Output:
38 | P a g e
● DataTables is a table enhancing plug-in for the jQuery Javascript library, adding sorting,
paging and filtering abilities to plain HTML tables with minimal effort.
● DataTables library is used "To enhance the accessibility of data in HTML tables".
Dependencies
● DataTables has only one library dependency – jQuery. If Jquery is successfully installed in
your system then you can use DataTables library. Else you can directly use online CDN for
JQuery
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
2. Include JS and CSS CDNs of DataTable librares in head section of your source file
CSS CDN:
JS CDN:
39 | P a g e
3. Include following function in head section
<script>
$(document).ready( function () {
$('#my_table').DataTable();
} );
</script>
& it’s done. Now you can see sorting, paging and filtering abilities are added to plain
HTML table.
Example:
<html>
<head>
<script src="jquery-3.5.1.min.js"></script>
<script>
$(document).ready( function () {
$('#my_table').DataTable();
} );
</script>
</head>
<body>
40 | P a g e
<table id="my_table" class="display" style="width:100%">
<thead>
<tr>
<th>No</th>
<th>name</th>
<th>salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>ABC</td>
<td>40000</td>
</tr>
<tr>
<td>2</td>
<td>DEF</td>
<td>50000</td>
</tr>
<tr>
<td>3</td>
<td>GHI</td>
<td>30000</td>
</tr>
</tbody>
<tfoot>
41 | P a g e
<tr>
<td></td>
<td></td>
<td>Total-120000</td>
</tr>
</tfoot>
</table>
</body>
</html>
Sorting in DataTables
● With DataTables you can alter the ordering characteristics of the table at initialization time.
Using order initialization parameter, you can set the table to display the data in exactly the
order that you want.
● The order parameter is an array of arrays where the first value of the inner array is the
column to order on, and the second is 'asc' (ascending ordering) or 'desc' (descending
ordering) as required. order is a 2D array to allow multi-column ordering to be defined.
42 | P a g e
$(document).ready(function() {
$('#example').DataTable( {
"order": [[ 3, "desc" ]]
} );
} );
● In addition to the above code, the following Javascript library files are loaded for use in
this example:
https://code.jquery.com/jquery-3.5.1.js
https://cdn.datatables.net/1.10.24/js/jquery.dataTables.min.js
Pagination in DataTables
● The style of the pagination options that DataTables presents to the end-user can greatly
effect the look and feel of your table, as well as, of course, the interaction behaviour.
DataTables has four built in paging control types (pagingType), and through the pagination
plug-ins below you can add additional options.
How to use
● To use a pagination plug-in you must include the pagination plug-in code, after you load
the DataTables library, but before you initialise the DataTable. When initialising the
DataTable, you must also tell it to make use of this plug-in, rather than using the default,
by setting the pagingType to the value required by the plug-in.
● As an example the code below makes use of the scrolling pagination plug-in saved into a
file:
<script type="text/javascript">
$(document).ready(function() {
43 | P a g e
$('#example').dataTable( {
"pagingType": "scrolling"
} );
} );
</script>
● For rendering A Bar Chart from A table, we can use highchart plugin.
● Highchart is a lightweight and easy-to-use jQuery plugin used to generate an animated bar
chart by reading tabulated data.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
Download links:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.src.js"></script>
44 | P a g e
<script src="jquery.highchartTable-min"></script>
<script type="text/javascript">
$('table.highchart').highchartTable();
</script>
Complete Code:
<!doctype html>
<html>
<head>
</head>
<body>
<thead>
<tr>
<th>month</th>
<th>sales</th>
<th>benefits</th>
</tr>
</thead>
45 | P a g e
<tr>
<td>jan</td>
<td>50000</td>
<td>30000</td>
</tr>
<tr>
<td>feb</td>
<td>20000</td>
<td>40000</td>
</tr>
<tr>
<td>march</td>
<td>10000</td>
<td>50000</td>
</tr>
<tbody>
</tbody>
</table>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.src.js"></script>
<script src="jquery.highchartTable-min"></script>
<script type="text/javascript">
$('table.highchart').highchartTable();
</script>
46 | P a g e
</body>
</html>
47 | P a g e