I am trying to create an expandable list. It is showing correctly for 1 node in the following manner:
- University
- Department0
- Course0 Student0
- Department0
However, when I do the same for more than 1 items ie. when number of departments are more than 1. It distributes the list anywhere around. Here is my code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Family Tree</title>
<link rel="stylesheet" href="css/style.css" type="text/css" media="screen, projection">
<script type="text/javascript" src="js/jquery-1.4.2.min.js">
</script>
<script type="text/javascript" src="js/scripts.js">
</script>
</script>
</head>
<body>
<h1><b>Demo ExpandableList</b></h1>
<div id="listContainer">
<div class="listControl">
<a id="expandList">Expand All</a>
<a id="collapseList">Collapse All</a>
</div>
<ul id="expList">
<?php
echo '<li>';
echo 'University';
for($department = 0; $department < 3; $department++){
if($department == 0){
echo '<ul>';
echo '<li>';
echo "d".$department;
}
else if($department == $countOfDepartments - 1){
echo '</li>';
echo '</ul>';
} else{
echo '<li>';
echo "d".$department;
}
for($course = 0; $course < 3; $course++){
if($course == 0){
echo '<ul>';
echo '<li>';
echo "c".$course;
}
else if($course == $countOfCourses - 1){
echo '</li>';
echo '</ul>';
} else{
echo '<li>';
echo "c".$course;
}
for($student = 0; $student < 3; $student++){
if($student == 0){
echo '<ul>';
echo '<li>';
echo "s".$student;
}
else if($student == $countOfStudents - 1){
echo '</li>';
echo '</ul>';
} else{
echo '<li>';
echo "s".$student;
}
} // for - 3
} // for - 2
}// for - 1
echo '</li>';
?>
</ul>
</div>
</body>
</html>
styles.js:
function prepareList() {
$('#expList').find('li:has(ul)')
.click( function(event) {
if (this == event.target) {
$(this).toggleClass('expanded');
$(this).children('ul').toggle('medium');
}
return false;
})
.addClass('collapsed')
.children('ul').hide();
//Create the button funtionality
$('#expandList')
.unbind('click')
.click( function() {
$('.collapsed').addClass('expanded');
$('.collapsed').children().show('medium');
})
$('#collapseList')
.unbind('click')
.click( function() {
$('.collapsed').removeClass('expanded');
$('.collapsed').children().hide('medium');
})
};
$(document).ready( function() {
prepareList()
});
style.css:
body {
font-size: 16px;
}
#menu {
list-style: none;
padding: 0;
margin: 0;
}
.clear {
clear: both;
}
/********************/
/* EXPANDABLE LIST */
/********************/
#listContainer{
margin-top:15px;
}
#expList ul, li {
list-style: none;
margin:0;
padding:0;
cursor: pointer;
}
#expList p {
margin:0;
display:block;
}
#expList p:hover {
background-color:#121212;
}
#expList li {
line-height:140%;
text-indent:0px;
background-position: 1px 8px;
padding-left: 20px;
background-repeat: no-repeat;
}
/* Collapsed state for list element */
#expList .collapsed {
background-image: url(../img/collapsed.png);
}
/* Expanded state for list element
/* NOTE: This class must be located UNDER the collapsed one */
#expList .expanded {
background-image: url(../img/expanded.png);
}
#expList {
clear: both;
}
.listControl{
margin-bottom: 15px;
}
.listControl a {
border: 1px solid #555555;
color: #555555;
cursor: pointer;
height: 1.5em;
line-height: 1.5em;
margin-right: 5px;
padding: 4px 10px;
}
.listControl a:hover {
color:#222222;
font-weight:normal;
}
The list that I am trying to get is in the following format;
- Unversity
- Department0 - Course0 - Student0 - Student1 - Student2
- Department1 - Course1 - Student0 - Student1 - Student2
- Department2 - Course2 - Student0 - Student1 - Student2
What am I possibly doing wrong? My logic seems to be correct.