este es el codigo que tengo hasta el momento
Código PHP:
<script type="text/javascript">
function cambiaGrupo(chk) {
var padreDIV=chk;
while( padreDIV.nodeType==1 && padreDIV.tagName.toUpperCase()!="DIV" )
padreDIV=padreDIV.parentNode;
//ahora que padreDIV es el DIV, cogeremos todos sus checkboxes
var padreDIVinputs=padreDIV.getElementsByTagName("input");
for(var i=0; i<padreDIVinputs.length; i++) {
if( padreDIVinputs[i].getAttribute("type")=="checkbox" )
padreDIVinputs[i].checked = chk.checked;
}
}
function marcaPadre(chk){
var padreFIELDSET=chk;
while( padreFIELDSET.nodeType==1 && padreFIELDSET.tagName.toUpperCase()!="DIV" )
padreFIELDSET=padreFIELDSET.parentNode;
var padreFIELDSEThijos=padreFIELDSET.childNodes;
for(var i=0, chk, todosChecked=true; i<padreFIELDSEThijos.length && todosChecked==false; i++) {
if( padreFIELDSEThijos[i].nodeType == 1 // Tiene que ser un nodo tipo HTML, no texto
&& // No puede estar dentro de un LEGEND
// Tiene que tener hijos (el checkbox)
(chk=padreFIELDSEThijos[i].getElementsByTagName("input")[0]) && // Seleccionamos al hijo input como chk
chk.getAttribute("type")=="checkbox" && // chk tiene que ser efectivamente un checkbox
chk.checked==true // y tiene que estar no seleccionado
) {
todosChecked = false;
}
}
// Si están todos checked tenemos que activar el primer checkbox hijo
// Si no están todos checked tenemos que desactivar el primer checkbox hijo
padreFIELDSET.getElementsByTagName("input")[0].checked = todosChecked
}
</script>
<?php
function drawTree($root,&$level, & $sm, & $checked, $parentId=null) {
$children = $sm->getSubOpcion($root['id_opcion']);
$chk = '';
//echo '<li> buscando ', $root['id_opcion'], ' en ', print_r($checked,1), ' econtrado : ' ,var_dump(array_search($root['id_opcion'],$checked,true)),'</li>';
if(array_search($root['id_opcion'],$checked,true) !== false) {
$chk = 'checked="checked"' ;
}
echo '<div> <li>';
?>
<img src="imagenes/mas.gif" onclick="$(this).toggle(); $(this).next('img').toggle()<?php if (count($children) > 0){ ?>;showSubMenu('menu_<?php echo $root['id_opcion'] ?>')<?php } ?>" style="cursor:pointer<?php if (count($children) < 1){ ?>; visibility:hidden <?php } ?>" />
<img src="imagenes/menos.gif" onclick="$(this).toggle(); $(this).prev('img').toggle()<?php if (count($children) > 0){ ?>;showSubMenu('menu_<?php echo $root['id_opcion'] ?>')<?php } ?>" style="display:none; cursor:pointer" />
<?php ?>
<input name="opciones[' <?php echo $root['id_opcion']; ?>']" id="opciones_<?php echo $root['id_opcion'];?>" type="checkbox" value="<?php echo $root['id_opcion']; ?>" <?php echo $chk; ?> onchange="cambiaGrupo(this);marcaPadre(this);" /> <span title="'<?php echo $root['descripcion']; ?>'";
<?php /*if (count($children) > 0) {
echo 'onclick="showSubMenu(\'menu_' . $root['id_opcion'] . '\')" style="cursor:pointer"';
}*/?>
> <?php echo $level . ' ' . htmlentities($root['titulo'],null,'utf-8') , '</span>';
if (count($children) > 0) {
echo '<ul style="display:none;" id="menu_' . $root['id_opcion'] . '">';
foreach ($children as $child) {
drawTree($child,++$level,$sm,$checked, $root['id_opcion']);
}
echo '</ul>';
}
echo '</li></div>';
--$level;
}
?>