Adding & Deleting Table Rows
Adds a table row to the bottom on click with "Add Button".
Deletes only checked Items with the click of "Delete Button"
HTML
<script src="http://tablesorter.com/__jquery.tablesorter.min.js"></script>
<table id="box-table-a">
<!-- Table header -->
<thead>
<tr>
<th scope="col" ><a href='#' id='selectAll'>Select</a></th>
<th scope="col" >Name</th>
<th scope="col" >Units</th>
<th scope="col" >Calories</th>
<th scope="col" >Sugar</th>
</tr>
</thead>
<!-- Table body -->
<tbody style="overflow: hidden; height: 25px; width: 320px;">
<tr class='normal' id='hidden'>
<td><input type="checkbox" /></td>
<td><select>
<option>Select an item:</option>
<option>Food</option>
<option>Water</option>
</select></td>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr class="normal" id="0">
<td><input type="checkbox" /></td>
<td><select>
<option>Select an item:</option>
<option>Food</option>
<option>Water</option>
</select></td>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr class="normal" id="1">
<td><input type="checkbox" /></td>
<td><select>
<option>Select an item:</option>
<option>Food</option>
<option>Water</option>
</select></td>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
</tbody>
<tfoot>
<tr>
...
CSS
#box-table-a
{
font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size: 12px;
margin: 45px;
width: 480px;
text-align: left;
border-collapse: collapse;
}
#box-table-a th
{
font-size: 13px;
font-weight: normal;
padding: 8px;
background: #b9c9fe;
border-top: 4px solid #aabcfe;
border-bottom: 1px solid #fff;
color: #039;
cursor: pointer;
}
#box-table-a td
{
padding: 8px;
border-bottom: 1px solid #fff;
border-top: 1px solid transparent;
}
#box-table-a tr:hover td
{
background: #d0dafd;
color: #339;
cursor: pointer;
}
.normal
{
background: #e8edff;
color: #339;
color: #669;
}
.highlight
{
background: #d0dafd;
color: #339;
}
.hidden
{
visibility:hidden;
display: none;
}
#box-table-a th.headerSortUp {
background-image: url(../img/small_asc.gif);
background-color: #3399FF;
}
#hidden
{
display: none;
}
JavaScript
$(document).ready(function() {
var rowCount = $('#box-table-a tbody>tr').length;
var flag = 0;
$("#box-table-a").tablesorter();
//Click on tr highlights and checks checkbox
$("#box-table-a tbody tr").click(function() {
//Highlights the clicked row
if($(this).hasClass("normal")){
$(this).removeClass("normal").addClass("highlight");
}
else {
$(this).removeClass("highlight").addClass("normal");
}
//Checks the checkbox
var $target = $(event.target);
if(!$target.is('input:checkbox')) {
$(this).find('input:checkbox').each(function() {
if(this.checked) { this.checked = false; }
else { this.checked = true;}
});
}
});//End tr click
//Add Rows to table
$('#addRows').click(function() {
$('#box-table-a tbody>tr:first').clone(true).insertAfter('#box-table-a tbody>tr:last');
$('#box-table-a tbody>tr:last').attr("id", "txtHint");
$('#box-table-a tbody>tr:nth-child(rowCount -1').attr("id", "row"+rowCount);
rowCount += 1;
alert($('#box-table-a tbody>tr:last').attr('id'));
});//End Add Rows
//Delete Checked Rows
$('#delRows').click(function() {
$('#box-table-a tbody>tr').each(function() {
if($(this).attr("id") !== "0") {
if($(this).find('input').attr('checked')) {
rowCount = rowCount - 1;
if(rowCount !== 0) {
$(this).remove();
}
}
}
else {
idNum = $(this).attr("id");
alert("First Row Cannot Be Deleted!\n ID: "+idNum);
...