Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Inserting Table Rows when row contains string

    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

    Code:
    <TR><TD>Sleepless</TD></TR>
    possible?

    Thanks for any help!


    Code:
    <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>
    Last edited by cyberpine; 12-04-2009 at 07:43 PM. Reason: wrap the code [code][/code]

  • #2
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    [code]
    <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>
    Last edited by cyberpine; 12-04-2009 at 09:02 PM.

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    568
    Thanks
    6
    Thanked 84 Times in 84 Posts
    I think this does roughly what you want

    Code:
    <!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>

  • #4
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    Code:
    <!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>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •