jQuery provides a number of useful client
side functionalities on Table like Style, Row count, Column count, Search,
Filter etc
In the
following example I have tried to cover all the possible scenarios which we
come across while using a Table.
Here is the example
<html xmlns="http://www.w3.org/1999/xhtml" >
<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
jQuery Table
</title>
<script type="text/javascript"
src="JavaScript/jquery-1.7.2.js"></script>
src="JavaScript/jquery-1.7.2.js"></script>
<link type="text/css"
href="Stylesheet.css"
rel="Stylesheet" />
href="Stylesheet.css"
rel="Stylesheet" />
<script type="text/javascript">
$(document).ready(function() {
//
// Add
Table Style
$("#tblEmployee").addClass("Table");
//
// Add
Table Style
$("#tblEmployee
td").addClass("TableCell");
/*
// Add Cell Style
$("#tblEmployee
td").filter(":even").addClass("TableCellEven");
$("#tblEmployee
td").filter(":odd").addClass("TableCellOdd");
*/
//
// Add
Header Style
$("#tblEmployee
th").addClass("TableHeader");
//
// Add
Row Style
$("#tblEmployee
tr").addClass("TableRow");
//
// Add
Alternating Row Style
$("#tblEmployee
tr").filter(":even").addClass("TableRowEven");
$("#tblEmployee
tr").filter(":odd").addClass("TableRowOdd");
// Add
Row Hover Style
//
MouseOver
$("#tblEmployee
tr").mouseover(function() {
$(this).addClass("TableRowHover");
})
//
MouseLeave
$("#tblEmployee
tr").mouseleave(function() {
$(this).removeClass("TableRowHover");
})
//
// Get
Row Count
var
rowCount = "Total Rows: " + ($("#tblEmployee tr").length -
1).toString();
document.getElementById("lblRowCount").innerText = rowCount;
//
// Get
Column Count
var
columnCount = "Total Columns: " +
($("#tblEmployee").find('tr')[0].cells.length).toString();
document.getElementById("lblColumnCount").innerText =
columnCount;
//
// Client
Side Search
// Get
the search Key from the TextBox
//
Iterate through the 2nd Column.
//
td:nth-child(2) - Filters only the 2nd Column
// If
there is a match show the row [$(this).parent() gives the Row]
// Else
hide the row [$(this).parent() gives the Row]
$('#cmdSearch').click(function(event) {
event.preventDefault();
var
searchKey = $('#txtName').val().toLowerCase();
$("#tblEmployee
tr td:nth-child(2)").each(function()
{
var
cellText = $(this).text().toLowerCase();
if
(cellText.indexOf(searchKey) >= 0) {
$(this).parent().show();
}
else
{
$(this).parent().hide();
}
});
});
//
// Client Side
Search (Autocomplete)
// Get the search
Key from the TextBox
// Iterate
through the 1st Column.
//
td:nth-child(1) - Filters only the 1st Column
// If there is a
match show the row [$(this).parent() gives the Row]
// Else hide the
row [$(this).parent() gives the Row]
$('#txtID').keyup(function(event) {
var
searchKey = $(this).val().toLowerCase();
$("#tblEmployee
tr td:nth-child(1)").each(function()
{
var
cellText = $(this).text().toLowerCase();
if
(cellText.indexOf(searchKey) >= 0) {
$(this).parent().show();
}
else
{
$(this).parent().hide();
}
});
});
//
// Clear
Search
// Clear
the Search textbox
//
Iterate through all the rows
// Set
the visibility of all the rows to Show()
$('#cmdClear').click(function(event) {
event.preventDefault();
$('#txtName').val('');
$("#tblEmployee
tr").each(function() {
$(this).show();
});
});
//
// Get
Row Count
$('#cmdGetRowCount').click(function(event) {
event.preventDefault();
var
rowCount = "Total Rows: " + ($("#tblEmployee tr").length -
1).toString();
alert(rowCount);
});
//
// Get
Column Count
$('#cmdGetColumnCount').click(function(event) {
event.preventDefault();
var
columnCount = "Total Columns: " +
($("#tblEmployee").find('tr')[0].cells.length).toString();
alert(columnCount);
});
//
// Loop
Through a Specific Column (Names)
// Column
index starts with 1
$('#cmdGetNames').click(function(event) {
event.preventDefault();
$("#tblEmployee
tr td:nth-child(2)").each(function()
{
alert($(this).text());
});
});
//
// Loop
Through a Specific Row (2nd Row)
// Column
index starts with 0
$('#cmdRow2').click(function(event) {
event.preventDefault();
$("#tblEmployee
tr:nth-child(3) td").each(function()
{
alert($(this).text());
});
});
//
$("#tblEmployee
tr").click(function(event) {
//
Check if Row is already Selected
if
($(this).hasClass('TableRowSelected'))
{
//
If selected, un-Select row
$(this).removeClass("TableRowSelected");
}
else
{
//
If not selected, Select row
$(this).addClass("TableRowSelected");
}
});
//
// Get
Selected Row Cell Values
$("#tblEmployee
tr").click(function(event) {
alert("Row
Clicked");
alert($(this).index());
var
ID = $(this).find("td:nth-child(1)").html();
var
Name = $(this).find("td:nth-child(2)").html();
alert('ID:
' + ID + '\nName: ' + Name);
});
});
</script>
</head>
<body>
<form name="frmTable" method="post" action="Table.aspx" id="frmTable">
<table>
<tr>
<td>
Search Name:
<input name="txtName"
type="text"
id="txtName"
style="width:100px;" />
type="text"
id="txtName"
style="width:100px;" />
<input type="submit"
name="cmdSearch"
value="Search"
id="cmdSearch" />
name="cmdSearch"
value="Search"
id="cmdSearch" />
<input type="submit"
name="cmdClear"
value="Clear"
id="cmdClear" />
name="cmdClear"
value="Clear"
id="cmdClear" />
</td>
</tr>
<tr>
<td>
Search ID (Autocomplete):
<input name="txtID"
type="text"
id="txtID"
style="width:100px;" />
type="text"
id="txtID"
style="width:100px;" />
</td>
</tr>
<tr valign="top">
<td>
<div id="divEmployee">
<div>
<table
cellspacing="0"
rules="all"
border="1"
id="tblEmployee"
style="border-collapse:collapse;">
cellspacing="0"
rules="all"
border="1"
id="tblEmployee"
style="border-collapse:collapse;">
<tr>
<th scope="col">ID</th>
<th scope="col">Name</th>
<th scope="col">DOB</th>
<th scope="col">Email</th>
</tr>
<tr>
<td>237</td>
<td>John</td>
<td>05/04/1981</td>
<td>john@abcsoftware.com</td>
<td>237</td>
<td>John</td>
<td>05/04/1981</td>
<td>john@abcsoftware.com</td>
</tr>
<tr>
<td>238</td>
<td>Tom</td>
<td>11/11/1967</td>
<td>tom@abcsoftware.com</td>
<tr>
<td>238</td>
<td>Tom</td>
<td>11/11/1967</td>
<td>tom@abcsoftware.com</td>
</tr>
<tr>
<td>239</td>
<td>239</td>
<td>Harry</td>
<td>10/07/1973</td>
<td>harry@abcsoftware.com</td>
<td>10/07/1973</td>
<td>harry@abcsoftware.com</td>
</tr>
<tr>
<td>240</td>
<td>Peter</td>
<td>01/01/1981</td>
<td>peter@abcsoftware.com</td>
<td>240</td>
<td>Peter</td>
<td>01/01/1981</td>
<td>peter@abcsoftware.com</td>
</tr>
<tr>
<tr>
<td>241</td>
<td>John</td>
<td>05/04/1981</td>
<td>john@abcsoftware.com</td>
<td>John</td>
<td>05/04/1981</td>
<td>john@abcsoftware.com</td>
</tr>
</table>
</div>
</div>
</td>
</tr>
<tr>
<td align="left">
<span id="lblRowCount" class="Label"></span>
</td>
</tr>
<tr>
<td align="left">
<span id="lblColumnCount" class="Label"></span>
</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td class="Label">
Get Row Count:
<input type="submit"
name="cmdGetRowCount"
value="Get Row Count"
id="cmdGetRowCount" />
name="cmdGetRowCount"
value="Get Row Count"
id="cmdGetRowCount" />
</td>
</tr>
<tr>
<td class="Label">
Get Column Count:
<input type="submit"
name="cmdGetColumnCount"
value="Get Column Count" id="cmdGetColumnCount" />
name="cmdGetColumnCount"
value="Get Column Count" id="cmdGetColumnCount" />
</td>
</tr>
<tr>
<td class="Label">
Loop Through a Specific
Column (Names):
<input type="submit"
name="cmdGetNames"
value="Get Names"
id="cmdGetNames" />
name="cmdGetNames"
value="Get Names"
id="cmdGetNames" />
</td>
</tr>
<tr>
<td class="Label">
Loop Through a Specific Row
(2nd Row):
<input type="submit"
name="cmdRow2"
value="Get 2nd Row"
id="cmdRow2" />
name="cmdRow2"
value="Get 2nd Row"
id="cmdRow2" />
</td>
</tr>
</table>
</form>
</body>
</html>
Add your Table (or) add server side code to
populate the Table with data.
Stylesheet.css
.Table
Stylesheet.css
.Table
{
border: 1px solid #000000;
}
.TableCell
{
padding: 3px 3px 3px 3px; /* Cellpadding */
border: 1px solid #000000;
font-family:Verdana;
font-size:10pt;
}
.TableCellEven
{
padding: 3px 3px 3px 3px;
border: 1px solid #000000;
background-color:#ffffcc;
font-family:Verdana;
font-size:10pt;
}
.TableCellOdd
{
padding: 3px 3px 3px 3px;
border: 1px solid #000000;
background-color:#fedcba;
font-family:Verdana;
font-size:10pt;
}
.TableHeader
{
background-color:#999966;
}
.TableRow
{
background-color:#ffffcc;
}
.TableRowEven
{
background-color:#ffffcc;
}
.TableRowOdd
{
background-color:#FEDCBA;
}
.TableRowHover
{
background-color:#CCCCCC;
}
.TableRowSelected
{
background-color:#CC99FF;
}JQuery Table Style
JQuery Table Header Style
JQuery Table Selected Row Values
JQuery Table Selected Row Index
JQuery Table Row Click Event
JQuery Table Row Selected
JQuery Table Column Values
JQuery Table Row Values
JQuery Table Column Count
JQuery Table Row Count
JQuery Table Hover Effect
JQuery Table Alternating Column Style
JQuery Table Alternating Row Style
JQuery Table Row Style
jQuery Table Tutorial
jQuery Table Filter Autocomplete Style
jQuery Table Search Client Side
No comments:
Post a Comment