Combine Hightlight with Add and Delete Selected Row |
Kylena 02-04-2005, 08:50 AM I'm modifying the scripts below but when I try to add in a few more parameters, it doesn't work anymore.
Functions that add and delete rows to a table.
function addRowToTable(tblID)
{
var tbl = document.getElementById(tblID);
var lastRow = tbl.rows.length;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(lastRow);
switch (tblID)
{
case 'family':
{
// 1st cell
var cell1 = row.insertCell(0);
var textNode = document.createTextNode(iteration);
cell1.appendChild('<a href=http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/javascript:SelectRow(' + iteration + ')>' + textNode + '</a>');
// 2nd cell
var cell2 = row.insertCell(1);
var famName = document.createElement('input');
famName.setAttribute('type', 'text');
famName.setAttribute('name', 'FamFullName' + iteration);
famName.setAttribute('size', '20');
cell2.appendChild(famName);
//3rd cell
var cell3 = row.insertCell(2);
var dob = document.createElement('input');
dob.setAttribute('type', 'text');
dob.setAttribute('name', 'FamDOB' + iteration);
dob.setAttribute('size', '20');
cell3.appendChild(dob);
//4th cell
var cell4 = row.insertCell(3);
var famRel = document.createElement('input');
famRel.setAttribute('type', 'select');
famRel.setAttribute('name', 'FamRelationship' + iteration);
famRel.setAttribute('size', '20');
cell4.appendChild(famRel);
//5th cell
var cell5 = row.insertCell(4);
var famCom = document.createElement('input');
famCom.setAttribute('type', 'text');
famCom.setAttribute('name', 'FamComment' + iteration);
famCom.setAttribute('size', '20');
cell5.appendChild(famCom);
}
break;
case 'asso':
{
// 1st cell
var cell1 = row.insertCell(0);
var textNode = document.createTextNode(iteration);
cell1.appendChild(textNode);
// 2nd cell
var cell2 = row.insertCell(1);
var AssoName = document.createElement('input');
AssoName.setAttribute('type', 'text');
AssoName.setAttribute('name', 'AssoFullName' + iteration);
AssoName.setAttribute('size', '20');
cell2.appendChild(AssoName);
//3rd cell
var cell3 = row.insertCell(2);
var aRel = document.createElement('input');
aRel.setAttribute('type', 'text');
aRel.setAttribute('name', 'AssoRelationship' + iteration);
aRel.setAttribute('size', '20');
cell3.appendChild(aRel);
//4th cell
var cell4 = row.insertCell(3);
var assoCom = document.createElement('input');
assoCom.setAttribute('type', 'text');
assoCom.setAttribute('name', 'AssoComments' + iteration);
assoCom.setAttribute('size', '20');
cell4.appendChild(assoCom);
}
break;
case 'default':
{}
}
}
function removeRowFromTable(tblID)
{
var tbl = document.getElementById(tblID);
var lastRow = tbl.rows.length;
if (lastRow > 2) tbl.deleteRow(lastRow - 1);
}
I've yet to modify removeRowFromTable() as I've encountered problem when trying to add a new row.
It seems that there is an error in the code:
cell1.appendChild('<a href=http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/javascript:SelectRow(' + iteration + ')>' + textNode + '</a>');
Original was: cell1.appendChild(textNode);
Am I using it wrongly?
Function to highlight the selected row:
// keep track of line # for currently selected row
var selectedRow = "undefined";
function SelectRow( rowID )
{
// unhighlight the previously selected row, if any
if( selectedRow != "undefined" )
UnHighlightRow( selectedRow );
// if necessary, convert row ID to row line number here
rowNumber = rowID;
// highlight and save selected row
HighlightRow( rowNumber );
selectedRow = rowNumber;
}
function MSIEUnHighlightRow( rowNumber )
{
var targetRow = eval( "document.all.row" + rowNumber );
targetRow.bgColor = "white";
}
function MSIEHighlightRow( rowNumber )
{
var targetRow = eval( "document.all.row" + rowNumber );
targetRow.bgColor = "lightsteelblue";
}
The table below:
<table width=400 cellspacing=0 cellpadding=0 border=0>
<tr id="row0">
<td width=50> <a href=http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/"JavaScript:SelectRow( 0 )">0</a></td>
<td width=250><a href=http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/"JavaScript:SelectRow( 0 )">Carol Cunnings</a></td>
<td width=100><a href=http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/"JavaScript:SelectRow( 0 )">555-7890</a></td></tr>
<tr id="row1">
<td width=50> <a href=http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/"JavaScript:SelectRow( 1 )">1</a></td>
<td width=250><a href=http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/"JavaScript:SelectRow( 1 )">Jane Doe</a></td>
<td width=100><a href=http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/"JavaScript:SelectRow( 1 )">555-1234</a></td></tr>.....
</table>
I only got the 1st row to be highlighted when selected. The rest just 'disappears' when trying to add.
hemebond 02-04-2005, 09:03 PM It seems that there is an error in the code:
cell1.appendChild('<a href=http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/http://www.codingforums.com/archive/index.php/javascript:SelectRow(' + iteration + ')>' + textNode + '</a>');
Original was: cell1.appendChild(textNode);
Am I using it wrongly?Yes you are. appendChild does not take a string as an argument, it takes an object. To do what you want to do, you'd need to create an a element, append a text node to it, and append the a element to cell1.
Kylena 02-05-2005, 04:17 AM Thanks for the explanation.
Kylena 02-08-2005, 01:01 AM I got this part working now:
function addRowToTable(tblID)
{
var tbl = document.getElementById(tblID);
var lastRow = tbl.rows.length;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(lastRow);
switch (tblID)
{
case 'family':
{
// 1st cell
var cell1 = row.insertCell(0);
var textNode = document.createTextNode(iteration);
var rowLink = document.createElement('a');
rowLink.setAttibute('href', 'javascript('+iteration+')');
rowLink.appendChild(rowLink);
cell1.appendChild(textNode);
However, the select row works only if I have <tr = rowX> where X is a number.
How do I append when creating a new row to it?
hemebond 02-08-2005, 01:44 AM Tell me the exactly what you're trying to achieve.
Kylena 02-11-2005, 02:12 AM What I'm trying to achieve is already stated in my 1st post.
I'm trying to insert a new row and I want to allow the users to select and delete the selected row.
hemebond 02-11-2005, 03:25 AM What I'm trying to achieve is already stated in my 1st post.
I'm trying to insert a new row and I want to allow the users to select and delete the selected row.Actually, you first post doesn't say that. To me at least. I'm still not sure how you want it all to work, but here's a row selection script anyway.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>51634</title>
<style type="text/css">
tr.selected
{
background-color:#ff5;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>id</th>
<th>Name</th>
<th>Number</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>Carol Cunnings</td>
<td>555-7890</td>
</tr>
<tr>
<td>1</td>
<td>Jane Doe</td>
<td>555-1234</td>
</tr>
</tbody>
</table>
<script type="text/javascript">
var rows = document.getElementsByTagName("tr");
for(var i = 0, len = rows.length; i < len; i++)
{
rows[i].addEventListener("click", selectRow, true);
}
var selected = null;
function selectRow(e)
{
target = e.currentTarget;
if(selected)
{
selected.className = (selected.className == "selected") ? "" : "selected";
}
selected = target;
selected.className = (selected.className == "selected") ? "" : "selected";
}
</script>
</body>
</html>I tried to get it working in IE, but I haven't a clue what that crap is doing.
glenngv 02-11-2005, 05:10 AM I tried to get it working in IE, but I haven't a clue what that crap is doing.
For IE, use rows[i].attachEvent("onclick", selectRow, true) and target = e.srcElement and remove the tr selector in the CSS.
Kylena 02-11-2005, 06:51 AM Actually, you first post doesn't say that. To me at least. I'm still not sure how you want it all to work, but here's a row selection script anyway.
I'm sorry I wasn't clear before.
Thanks for the code, hemebond and Glenn.
I'll try it out and see what I'm missing.
hemebond 02-11-2005, 08:31 PM target = e.srcElement and remove the tr selector in the CSS.Now see I tried that, and the stupid thing kept returning the a TD object. Then things were getting selected all over the show. But I guess I needed to use attachEvent. Thanks for the tip.
Kylena 02-15-2005, 12:54 AM Now I have the following to create a button that will delete the current row.
function addRowToTable(tblID)
{
var tbl = document.getElementById(tblID);
var lastRow = tbl.rows.length;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(lastRow);
switch (tblID)
{
case 'family':
{
// 1st cell
var cell1 = row.insertCell(0);
var textNode = document.createTextNode(iteration);
var rowLink = document.createElement('a');
rowLink.setAttibute('href', 'javascript('+iteration+')');
rowLink.appendChild(rowLink);
cell1.appendChild(textNode);
.......
//6th cell
var cell6 = row.insertCell(5);
var delBtn = document.createElement('input');
delBtn.setAttribute('type', 'button');
delBtn.setAttribute('name', 'delBtn' + iteration);
delBtn.setAttribute('value', 'Delete');
delBtn.onclick = deleteRow
}
Function for deleteRow
function deleteRow(i, tblID)
{
document.getElementById(tblID).deleteRow(i);
}
How do I pass in the paramenters when I create the button? Can that be done?
glenngv 02-15-2005, 04:08 AM delBtn.onclick = function() {deleteRow(iteration, tblID)};
Kylena 02-15-2005, 06:08 AM Thanks Glenn.
I'll try it out.
Thanks again for being so patient and the modifications to the codes. :p
Kylena 02-15-2005, 07:53 AM I got the code working, however, when I've deleted a row, the row index is wrong.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script language="JavaScript" type="text/javascript">
<!--
function addRowToTable(tblID)
{
var tbl = document.getElementById(tblID);
var lastRow = tbl.rows.length;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(lastRow);
switch (tblID)
{
case 'family':
{
// 1st cell
var cell1 = row.insertCell(0);
var famName = document.createElement('input');
famName.setAttribute('type', 'text');
famName.setAttribute('name', 'FamFullName' + iteration);
famName.setAttribute('size', '20');
cell1.appendChild(famName);
// 2nd cell
var cell2 = row.insertCell(1);
var dob = document.createElement('input');
dob.setAttribute('type', 'text');
dob.setAttribute('name', 'FamDOB' + iteration);
dob.setAttribute('size', '20');
cell2.appendChild(dob);
//3rd cell
var cell3 = row.insertCell(2);
var famRel = document.createElement('input');
famRel.setAttribute('type', 'select');
famRel.option = document.forms[0].FamRelationship.option;
famRel.setAttribute('name', 'FamRelationship' + iteration);
cell3.appendChild(famRel);
//4th cell
var cell4 = row.insertCell(3);
var famCom = document.createElement('input');
famCom.setAttribute('type', 'text');
famCom.setAttribute('name', 'FamComment' + iteration);
famCom.setAttribute('size', '20');
cell4.appendChild(famCom);
//5th cell
var cell5 = row.insertCell(4);
var fdelBtn = document.createElement('input');
fdelBtn.setAttribute('type', 'button');
fdelBtn.setAttribute('name', 'del' + iteration);
fdelBtn.setAttribute('value', 'Delete');
fdelBtn.onclick = function() {deleteRow(iteration, 'family')};
cell5.appendChild(fdelBtn);
}
break;
case 'asso':
{
// 1st cell
var cell1 = row.insertCell(0);
var AssoName = document.createElement('input');
AssoName.setAttribute('type', 'text');
AssoName.setAttribute('name', 'AssoFullName' + iteration);
AssoName.setAttribute('size', '20');
cell1.appendChild(AssoName);
// 2nd cell
var cell2 = row.insertCell(1);
var aRel = document.createElement('input');
aRel.setAttribute('type', 'text');
aRel.setAttribute('name', 'AssoRelationship' + iteration);
aRel.setAttribute('size', '20');
cell2.appendChild(aRel);
//3rd cell
var cell3 = row.insertCell(2);
var assoCom = document.createElement('input');
assoCom.setAttribute('type', 'text');
assoCom.setAttribute('name', 'AssoComments' + iteration);
assoCom.setAttribute('size', '20');
cell3.appendChild(assoCom);
//4th cell
var cell4 = row.insertCell(3);
var adelBtn = document.createElement('input');
adelBtn.setAttribute('type', 'button');
adelBtn.setAttribute('name', 'adelBtn' + iteration);
adelBtn.setAttribute('value', 'Delete');
adelBtn.onclick = function() {deleteRow(iteration, 'asso')};
cell4.appendChild(adelBtn);
}
break;
case 'default':
{}
}
}
function deleteRow(i, tblID)
{
var tbl = document.getElementById(tblID);
var lastRow = tbl.rows.length;
alert('b4 del: ' + lastRow + '\nrow to del: ' + i)
if (confirm('Are you sure you want to delete this row?'))
{
if (i <= lastRow)
tbl.deleteRow(i);
else
tbl.deleteRow(this.parentNode.parentNode.rowIndex);
}
lastRow = tbl.rows.length;
alert('after del: ' + lastRow)
}
// -->
</script>
</head>
<body text="#000000" bgcolor="#FFFFFF">
<table id=family>
<tr>
<th id="normHeader">Full Name</th>
<th id="normHeader">Date of Birth</th>
<th id="normHeader">Relationship</th>
<th id="normHeader">Comments</th>
<td>
<input type="button" onclick="addRowToTable('family');" value="Add"> </td>
</tr>
<tr>
<td>
<input name="FamFullName" value=""></td>
<td>
<input name="FamDOB" value=""></td>
<td>
<select name="FamRelationship">
<option>- Please Select -
<option>Father
<option>Mother
<option>Sister
<option>Brother
<option>Son
<option>Daughter</select>
</td>
<td>
<input name="FamComments" value=""></a></td>
</tr>
</table>
<hr>
<h4>Associated Personalities</h4>
<table id=asso>
<tr id=arow0>
<th id="normHeader">Full Name</th>
<th id="normHeader">Relationship</th>
<th id="normHeader">Comments</th>
<td>
<input type="button" onclick="addRowToTable('asso');" value="Add"> </td>
</tr>
<tr id=arow1>
<td>
<input name="AssoFullName" value=""></td>
<td>
<input name="AssoRelationship" value=""></td>
<td>
<input name="AssoComments" value=""></td>
</td>
</table>
</body>
</html>
After I delete the row, how do I re-iterate the row indexes again?
Or do I append and 'id = row' + iteration?
Kylena 02-16-2005, 06:31 AM Found a work around.
Replace: adelBtn.onclick = function() {deleteRow(iteration, 'asso')};
With: adelBtn.onclick = function() {deleteRow(this.parentNode.parentNode.rowIndex, 'asso')};
|
|
|
|
|