Hi there,

I have these hirachical tree,I'm strugling to put checkbox on nodes only,for users to select a question they want to submit.

here is my code:

<style type="text/css">
.normal { background-color: white }
.normalActive { background-color: #DEE7EF;}

li{
	list-style:none;
}

</style>

<script language="javascript">
function show(subCategory, path){
var thesub;
var theimage;
var imageid;
var testingdiv;

thesub = document.getElementById(subCategory);

imageid = "img_"+subCategory;
theimage = document.getElementById(imageid);

testingdiv = document.getElementById("testing");

if(thesub.style.display == 'block'){
	//collapse...
	thesub.style.display = 'none';
	theimage.src = path+'images/c.gif';

	/*
	//testing...
	testingdiv.style.display = 'block';
	testingdiv.innerHTML = path+'common/parts_manuals/open.gif';
	*/

}else{
	//expand/display...
	thesub.style.display = 'block';
	theimage.src = path+'images/e.gif';

	/*
	//testing...
	testingdiv.style.display = 'block';
	testingdiv.innerHTML = path+'common/parts_manuals/up.gif';
	*/
}

}
</script>
</head>
<body>
<div id="Layer3" style="position:absolute; left:5px; top:50px; width:750px; height:20px;">
<table width="100%">
<tr bgcolor="#AA0000"><td align="right"><font size="4" face="Arial, Helvetica, sans-serif" color="White"><strong>Nonconforming Event Report</strong></font></td></tr>
</table>
</div>
<div id="Layer5" style="position:absolute; left:5px; top:75px; width:750px; height:20px;">
<font size="2" face="Arial, Helvetica, sans-serif" color="Navy">
<?php
require_once("../shared/UserMenu.php"); //include User menus

	$iIssueId	=	$_GET["iEventId"];

?>
</font>

</div>
<div id="Layer6" style="position:absolute; left:5px; top:100px; width:750px; height:40px;">
<font size="2" face="Arial, Helvetica, sans-serif" color="Navy">
<TABLE WIDTH="100%" CELLSPACING="0" BGCOLOR="#99CCFF">
<TR>
<TD VALIGN="BOTTOM">
</TD>
<TD ALIGN="RIGHT" VALIGN="BOTTOM">
<font size="-1" color="#000063">
<A HREF="NewEvent.php">New</A> |
<A HREF="EventSubmitted.php">All</A> |
<A HREF="EventAssigned.php"><strong>Mine</strong></A> |
<A HREF="EventResolved.php">Resolved</A> |
<A HREF="EventReport.php">Report</A>|
<A HREF="EventSearch.php">Search</A>
</font>
</TD>
</TR>
</TABLE>
</font>
</div>
<div id="Layer4" style="position:absolute; left:5px; top:105px; width:750px; height:520px; border: 1px none #000000"><br clear="all">
<font size="2" face="Arial, Helvetica, sans-serif" color="#000063">
<?php
$connection = mssql_connect($dbHost,$dbUser,$dbPass) or die("<p style='color:#FF0000;text-align:left;'>Cannot connect to database</p>");
$database = mssql_select_db($dbDbase) or die("<p style='color:#FF0000;text-align:left;'>Cannot select the database</p>");

//#########################################################################
$children = 0;
$no_children = array();

// $parent is the parent of the children we want to see
// $level is increased when we go deeper into the tree, used to display a nice indented tree
function display_children($parent, $level) {
global $children;
global $no_children;

// retrieve all children of $parent
/ $sql = sprintf("SELECT category_name, category_id, category_link
FROM categories
WHERE parent_id = %d",
$parent);
/
$sql = sprintf("SELECT category_no, category_desc, iParentId FROM n_CATEGORIES WHERE igroupid = 69 AND iuserid = 54 AND tiactive = 1 AND iParentId = %d ORDER BY iParentId",$parent);

$result = mssql_query($sql);

//if this is a sub category nest the list...
   if($level > 0)
        echo "<ul id='$parent' style='display:none;'>\n";

$list_id='';
// display each child
while ($row = mssql_fetch_array($result)) {

	$children++;
	$list_id = 'list_'.$children;

   //display each child
   echo '<li id="'.$list_id.'">';
   echo '<a href="#" onClick="show('.$row['category_no'].', \''.$HTTP_PATH.'\')">';
   echo '<img src="'.$HTTP_PATH.'images/c.gif" title="expand" border="0" id="img_'.$row['category_no'].'">';
   echo '</a>&nbsp;&nbsp;';
   echo $row['category_desc'];  " <span style='color:red;'>".$list_id."</span>";
   echo '</li>';

   // call this function again to display this child's children
   display_children($row['category_no'], $level+1);

}

// $child_product = display_products($parent);

// if this is a sub category nest the list...
if($level > 0)
echo "</ul>\n";

	$no_children[] =  'list_'.$children;

	//if the category has at least one product in it allow us to expand and see that product...
	if($child_product)
		array_pop($no_children);

}
//#########################################################################

echo "<ul style='list-style:none;'>";
display_children('',0);
echo "</ul>";

/
echo "<pre style='color: red'> ";
print_r($no_children);
echo "</pre>";
/

?>
<!--<div style="display:block; border: 1px solid red;" id="testing"></div>-->

<script language="javascript" type="text/javascript">
function nokids(){
var kidnot;
kidnot = new Array();

	<?php
		for($i=0; $i<count($no_children); $i++){
			print("kidnot.push(\"".$no_children[$i]."\");\n");
		}
	?>

	for(i in kidnot){

		var theid;
		theid = kidnot[i];

		//document.getElementById("testing").innerHTML += theid+", ";
		document.getElementById(kidnot[i]).style.color = "green";
		/*
		var nodelings = document.getElementById(kidnot[i]).childNodes;
		for(var i=0; i<nodelings.length; i++){
			alert(nodelings[i]);
		}
		*/
	}
}

</script>

    What happends when you add a INPUT element just after this code?

    //display each child
    echo '<li id="'.$list_id.'">';

    You have the $row record for each children, so you should be able to add:

    echo "<input type=checkbox name=checkboxs[" . $row["id"] . "]>";

    If you only want it on certain nodes, (ie: leafs), then just check the record structure. Normally you would have a parent id.

    Then iterate $_POST["checkboxs"]; to get the check state of each child.

    But, that said, 3 years ago I tried a similar solution on a table that had a javascript column sort feature.

    There was a bug in either the sort function or IE (at the time), which caused the checkbox's to be lost when the parent TR's were moved. If you are using JavaScript for a dynamic tree view, you might have a similar problem.

    But, that said, until you put in your checkboxes, who can tell?

      After I put echo "<input type=checkbox name=checkboxs[" . $row["id"] . "]>";

      It shows check box on all tree menu,

        7 days later

        Isn't that a good thing?

        If you only want some nodes to have checkboxes, then you need to define the "selection criteria".

        ie: If you only want nodes that have parents, then you check the parent id for > 0

        The other issue with checkboxs is, no value is posted if the checkbox is not checked.

        Which can make it difficult to code under some situations.

        ie: if you have a selection criteria, then just because a value is not present in checkboxs[] can also mean the node didn't match the criteria.

        That means you need to test the selection criteria again when moving the check results into the database.

        Anyway, hope you the best

          Write a Reply...