Sunday, April 15, 2012

Javascript: expand/collapse functionality in a web page

Here is the script to implement expand/collapse functionality in a web page.

Javascript:

function toggle(showHideDiv, switchImgTag) 
{
        var elem = document.getElementById(showHideDiv);
        var imageElem = document.getElementById(switchImgTag);
        if(elem.style.display == "block") {
                elem.style.display = "none";
    imageElem.innerHTML = '';
        }
        else {
                elem.style.display = "block";
                imageElem.innerHTML = '';
        }
}

function Collapse(showHideDiv, switchImgTag) 
{
        var elem = document.getElementById(showHideDiv);
        var imageElem = document.getElementById(switchImgTag);
        
  elem.style.display = "none";
  imageElem.innerHTML = '';
}

function Expand(showHideDiv, switchImgTag) 
{
        var elem = document.getElementById(showHideDiv);
        var imageElem = document.getElementById(switchImgTag);
        
  elem.style.display = "block";
        imageElem.innerHTML = '';
}

function ExpandAll()
{
  var elements = document.getElementsByTagName("a");
  for (var i = 0; i < elements.length; i++) 
  {
      var myname = elements[i].getAttribute("id");
    if(myname != null)
    {
       if(myname.substr(0, 4) == "img-")
     {
      var linkid = myname.substr(4);
      Expand("div-"+ linkid, "img-"+linkid);
     }
    }
  }
  document.getElementById('expand-all').style.display='none';
  document.getElementById('collapse-all').style.display='block';
}

function CollapseAll()
{
  var elements = document.getElementsByTagName("a");
  for (var i = 0; i < elements.length; i++) 
  {
      var myname = elements[i].getAttribute("id");
    if(myname != null)
    {
       if(myname.substr(0, 4) == "img-")
     {
      var linkid = myname.substr(4);
      Collapse("div-"+ linkid, "img-"+linkid);
     }
    }
  }
  document.getElementById('expand-all').style.display='block';
  document.getElementById('collapse-all').style.display='none';
}

Web Page:





Expand Collapse Test





Collapse All
Test1
TestIDDescriptionActualExpectedTime TakenResultScreen-Shot
Test-01Test 01PASSPASS 1 secPassNone
Test-01Test 02PASSFAIL 5 secFailClick me
Test2
TestIDDescriptionActualExpectedTime TakenResultScreen-Shot
Test-01Test 03PASSPASS 1 secPassNone
Test-01Test 04PASSPASS 2 secPassNone
Test-01Test 05PASSPASS 3 secPassNone

Stylesheet:

body, table {
    font-family: Verdana, Arial, sans-serif;
    font-size: 12;
}

table {
    border-collapse: collapse;
    border: 1px solid #ccc;
}

th, td {
    padding-left: 0.3em;
    padding-right: 0.3em;
}

a {
    text-decoration: none;
}

.title {
    font-style: italic;
}

.selected {
    background-color: #ffffcc;
}

.status_done {
    background-color: #eeffee;
}

.status_passed {
    background-color: #00FF00;
}

.status_failed {
    background-color: #FF0000;
}

.status_notrun {
    background-color: #FFFF00;
}

.status_header {
    background-color: #FFA500;
}

.status_duration {
    background-color: #00FFFF;
}

.breakpoint {
    background-color: #cccccc;
    border: 1px solid black;
}

No comments: