...

View Full Version : Inserting Table Rows when row contains string



cyberpine
12-02-2009, 11:22 PM
The below javascript code only displays table rows in class ms-formlabel that contain (AP) in the text. It works great.

However, I also need it to also insert a new row right above any rows that also contains (H: and in the detail of that new row, I need the string following the H:

So a table row with (AP)(H:Sleepless), would insert a row right above that row as follows



<TR><TD>Sleepless</TD></TR>


possible?

Thanks for any help!




<script type="text/javascript">
var theRows = document.getElementsByTagName("TR");
var r = 0;
var strTitle = "";
while (r < theRows.length)
{ try
{ strTitle = theRows[r].innerText || theRows[r].textContent;
strTitle = strTitle.replace(/\n|\r|\t|\^ /g,"");
var row = theRows[r],
cells = row.getElementsByTagName('td');
if (cells[0].className.indexOf('ms-formlabel') > -1)
{
if (strTitle.indexOf("(AP)") == -1)
{
theRows[r].style.display = "none";
}
else
{
theRows[r].cells[0].innerHTML = theRows[r].cells[0].innerHTML.replace("(AP)","");
}
}

}catch(err){}r+=1;
}
</script>

cyberpine
12-04-2009, 07:38 PM
still stuck on this.

here's some testing code. Just looking to insert a TR (rigth above the matching row) with the H: string is found when a match is found. In this case 3 rows qualify, but only one qualifies for a header row. That row inserted would look like


<TR><TD>testing</TD></TR>

[code]
<html>
<head>
</head>
<body>
<table>
<TR><td>test</td></TR>
<TR><td>test2</td></TR>
<TR><td>test3</td></TR>
<TR><td>test4</td></TR>
<TR><td>Qualifies1(HP)</td></TR>
<TR><td>test</td></TR>
<TR><td>test2</td></TR>
<TR><td>test3</td></TR>
<TR><td>test4</td></TR>
<TR><td>Qualifies2(HP)</td></TR>
<TR><td>test</td></TR>
<TR><td>test2</td></TR>
<TR><td>test3</td></TR>
<TR><td>QualifiesH(HP)(H:testing)</td></TR>
<TR><td>test</td></TR>
<TR><td>test2</td></TR>
<TR><td>test3</td></TR>
</table>
</body>
</html>
<script type="text/javascript">
var theRows = document.getElementsByTagName('TR');
var r = theRows.length;
var strTitle,
row,
cell;


while (r--) {
row = theRows[r];
cell = row.cells[0];


if (cell.innerHTML.indexOf("(HP)") == -1) {
row.style.display = "none";
} else {
cell.innerHTML = cell.innerHTML.replace("(HP)","");
}
}
</script>

gusblake
12-04-2009, 09:26 PM
I think this does roughly what you want



<!DOCTYPE HTML PUBLIC "-//W3C// DTD HTML 4.//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="index.css">

</head>
<body>
<table>
<tr><td>(HP)</td></tr>
<tr><td>nodisplay</td></tr>
<tr><td>(HP)(H:test)</td></tr>
</table>

<script type="text/javascript">
var rows=Array.prototype.slice.call(document.getElementsByTagName("tr")); //slice.call converts it to a proper array

for(i in rows) {
var row=rows[i];
var cell=row.getElementsByTagName("td")[0];
var text=cell.innerHTML;
if(text.indexOf("(HP)")==-1) row.style.display="none";
var temp=text.match(new RegExp("H:.*","gi"));
if(temp) {
newtext=temp[0].substring(2,(temp[0].length-1));
var newrow=document.createElement("tr");
var newcell=document.createElement("td");
newrow.appendChild(newcell);
newcell.appendChild(document.createTextNode(newtext));
row.parentNode.insertBefore(newrow,row);
}
}
</script>
</body>
</html>

cyberpine
12-07-2009, 06:11 PM
Thank you very much!

Looks really promising, but I can't get it to work on my IE 7 client.

The javascript is running, but neither of these alerts trip:



<!DOCTYPE HTML PUBLIC "-//W3C// DTD HTML 4.//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
</head>
<body>
<table>
<tr><td>(HP)</td></tr>
<tr><td>nodisplay</td></tr>
<tr><td>(HP)(H:test)</td></tr>
</table>

<script type="text/javascript">
var rows=Array.prototype.slice.call(document.getElementsByTagName("tr"));
for(i in rows) {
alert("found");
var row=rows[i];
var cell=row.getElementsByTagName("td")[0];
var text=cell.innerHTML;
if(text.indexOf("(HP)")==-1) row.style.display="none";
var temp=text.match(new RegExp("H:.*","gi"));
if(temp) {
alert("matched");
newtext=temp[0].substring(2,(temp[0].length-1));
var newrow=document.createElement("tr");
var newcell=document.createElement("td");
newrow.appendChild(newcell);
newcell.appendChild(document.createTextNode(newtext));
row.parentNode.insertBefore(newrow,row);
}
}
</script>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum