Checkbox parents childs arborescence
Bonjour voila j'ai une fonction qui m'affiche mon arborescence :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
function getCategories( $childs, $newsSubject) {
foreach( $childs as $key => $child) {
echo '<li><input id="child['.$child['subjectId'].']" name="subject[]" onclick="uncheck('.$child['subjectId'].', '.$child['parentId'].')" value="'.$child['subjectId'].'" type="
CHECKBOX" '.(isset($newsSubject[($child['subjectId'])])?'checked="checked"':'').'><label for="child['.$child['subjectId'].']" id="lab['.$child['subjectId'].']">'.$child['name'].'</lab
el>'."\n";
if( isset( $child['childs'])) {
echo '<ul>';
getCategories( $child['childs'], $newsSubject);
echo '</ul>';
}
echo '</li>';
}
} |
et dans le template j'ai ca :
Code:
1 2 3 4 5 6 7 8 9 10
|
<script>
list_childs = Array(<?=array2js($category['childs'])?>);
</script>
<form action="?" method="post">
<ul class="checklist">
<? getCategories( $category['childs'], $newsSubject) ?>
</ul>
</form> |
je souhaite 2 choses :
Lorsque je coche un child je veux que ca décoche le parent et le parent du parent .
et également lorsque je coche un parent que ca décoche tout ses childs
et lorsque je coche quelques choses ca se mette en rouge
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
function uncheck(subject_id, parent_id)
{
if(parent_id != 0) uncheckParent(parent_id);
else uncheckChilds(subject_id);
}
function uncheckParent(parent_id)
{
if(parent_id!=0 && document.getElementById('child['+parent_id+']') && document.getElementById('child['+parent_id+']').checked == true)
document.getElementById('child['+parent_id+']').checked = false;
}
function uncheckChilds(subject_id)
{
all = list_childs[0];
for(id in all)
{
if(all[id]['subjectId'] == subject_id && typeof all[id]['childs'] != "undefined")
{
for(child_id in all[id]['childs'])
{
temp_id = all[id]['childs'][child_id]['subjectId']
document.getElementById('child['+temp_id+']').checked = false;
}
}
}
}
function dump(obj)
{
text = '';
for(id in obj)
if(typeof obj[id] == "object") text += "\n"+id+" : "+dump(obj[id]);
else text += id+" : "+obj[id]+"\n";
return text;
}
function makeViewCheck(obj)
{
if(typeof obj['childs'] != "undefined")
{
for(id in obj['childs']) makeViewCheck(obj['childs'][id]);
}
else if(document.getElementById('child['+obj['subjectId']+']').checked)
{
document.getElementById('lab['+obj['subjectId']+']').style.color = "red";
}
} |