javascript - jQuery prop parent checkbox from directory listing pulled from PHP -


my goal "prop" parent checkbox in nested list. right pull in directory listing in php this:

function listdirectories($dir, $i, $howdeep) {             $lastfolder = end(explode("/", $dir));             $liststring .='<li class="closed"><span class="folder"><input type="checkbox" class="userpermissioncheckbox" parent="'.$i.'" howdeep="'.$howdeep.'" value="'.$dir.'" />'.str_replace('_', ' ', $lastfolder).'</span>';             foreach (glob($dir."/*", glob_onlydir) $d) {                 $howdeep++;                 $liststring .='<ul>';                 $liststring .=' '.listdirectories($d, $i, $howdeep).' ';                 $liststring .='</ul>';                 $i++;                 $howdeep = 1;             }             $liststring .='</li>';             return $liststring;         } 

that works awesome!, bind jquery/js function check boxes "prop" of child boxes when parent selected this:

 var isalreadychecked = $(this).prop('checked');             $(this).parents(':eq(1)').find('.userpermissioncheckbox').each(function () {                 $(this).prop('checked', isalreadychecked);             }); 

this works fantastic.

where stuck if check child box without checking parent box, auto check parent... tried this:

var isalreadychecked = $(this).prop('checked');              var parentnumber = number($(this).attr('parent'));              var howdeepisnest = number($(this).attr('howdeep'));             $(this).parents(':eq(1)').find('.userpermissioncheckbox').each(function () {                 $(this).prop('checked', isalreadychecked);             });             if(howdeepisnest > 1){                 var immediateparent = howdeepisnest - 1;                 $('.userpermissioncheckbox[howdeep='+immediateparent+']').prop('checked', true);             } 

this kind of works, auto prop child boxes works great, can't auto check parent boxes work. i'm open suggestions. thank taking time read this.

this will:

  • un/check children
  • check parents
  • uncheck direct parent if siblings unchecked

fiddle: http://jsfiddle.net/a7wdk/1/

$('input:checkbox').on('change', function() {     var $this = $(this),         $lis = $this.parents('li'),         $parents = $lis.find('> span > input:checkbox').not(this),         $children = $lis.eq(0).find('input:checkbox').not(this),         $related = $lis.eq(0).siblings().find('input:checkbox');      if($this.is(':checked')) {         $parents.add($children).attr('checked', 'checked');     } else {         if($children.length) {             $children.removeattr('checked');         }          if($related.length && !$related.is(':checked')) {             $parents.eq(0).removeattr('checked').trigger('change');         }     } }); 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -