简体   繁体   中英

Expand all / collapse all lists for class not working

I have a tree list where the user can open or close the branches by clicking. The tree has many levels and sub-levels. I want to be able to collapse all branches (down to the first level) or expand all branches for the class "my-clickable" when the user clicks on the Collapse All or Expand All button.

I can get the individual branches to close or open. But I can't get them all to open or close at once.

Here is my jsfiddle: https://jsfiddle.net/dyrh325f/65/

One thing to note is that the Expand All button is being used after an ajax call that populates the entire tree. I have the 'Expand All' code in the "done" portion of the ajax call, so it applies to the generated html.

I want the user to be able to expand the entire tree out after he has closed one or more nodes. I've tried several variations of what I have in my jsFiddle. Nothing seems to work. What am I doing wrong?

I am a jQuery newbie so - thanks for any and all help!!!

Code: HTML: Expand Tree

<font face="calibri" size="3pt" >
<ul id="tree1" class="mytree">  
<li id="theJob" class="my-clickable mytree liParOpen">My Job</li>
<ul id="treeList" class="mytree" name="treeList">
  <li id='0' class='mytree child-click'>Batcher</li>
    <li id='1' class='mytree child-click'>Retriever</li>
    <li id='2' class='my-clickable mytree liParOpen'> TASK 2</li>
    <ul class='mytree'>
      <li id='2a' class='mytree child-click'>Prep1</li>
      <li id='2b' class='mytree child-click'>Prep2</li>
      <li id='2c' class='mytree my-clickable liParOpen'>PREP3</li>
      <ul class='mytree'>
          <li id='2b1' class='mytree child-click'>PREP3a</li>
          <li id='2b2' class='mytree child-click'>PREP3b</li>
      </ul>
    </ul>
    <li id='3' class='mytree child-click' > task 3</li>
    <li id='4' class='my-clickable mytree liParOpen'> TASK 4</li>
    <ul class='mytree'>
      <li id='4a' class='mytree child-click'>Edit1</li>
      <li id='4b' class='mytree child-click'>Edit2</li>
    </ul>
    <li id='5' class='my-clickable mytree liParOpen'> TASK 5</li>
    <ul class='mytree'>
      <li id='5a' class='mytree my-clickable liParOpen'>Del1</li>
      <ul class='mytree'>
        <li id='5a1' class='mytree child-click'>Del1a</li>
        <li id='5a2' class='mytree child-click'>Del1b</li>
      </ul>
      <li id='5b' class='mytree child-click'>Del2</li>
    </ul>
    <li id='6' class='mytree child-click'>DocEjector-3</li>
</ul>
</ul>
</font>

Jquery:

$(document).ready(function(){

  // expand button
  $('#expandTree').on('click', function(event) {
     var all = $('.my-clickable');
     var closed = all.filter('.liParClosed');

     closed.find("ul").each(function(){
        $(this).next('ul').attr("class", "liParOpen");
        $(this).nextAll('ul').toggle();
     });
   }); // end expand button 

   // collapse button
   $('#collapseTree').on('click', function(event) {
     var all = $('.my-clickable');
     var open = all.filter('.liParOpen');

     open.find("ul").each(function(){
        $(this).next("ul").attr("class", "liParClosed");
        $(this).nextAll('ul').slideToggle();
     });
   }); // end collapse button 

   // this is the top most level parents
   $(".my-clickable").on('click', function(event) {

       var taskId=$(this).closest("li").attr('id');
       var tsk = '#'.concat(taskId);  

       if (taskId != "theJob") {
        if ($(tsk).next('ul').length <= 0) {
           $(tsk).toggleClass("liParOpen liParClosed");
               $(tsk).next('ul').slideToggle();
        }
        else { 
           //$(event.target).find('ul').toggle();
           $(tsk).toggleClass("liParOpen liParClosed");
               $(tsk).children('li').slideToggle();
               $(tsk).next('ul').slideToggle();
        }
    }  // end if taskId != "theJob"
    else {
       $(tsk).toggleClass("liParOpen liParClosed");
       $(tsk).slideToggle();
    }

    event.cancelBubble=true;
    event.stopPropagation();
});

//2nd level parents
$(".my-clickable").on('click', ".my-clickable", function(event) {
    var taskId=$(this).closest("li").attr('id');
    var tsk = '#'.concat(taskId);

    //$(event.target).find('ul').slideToggle();
    $(tsk).toggleClass("liParOpen liParClosed");

    //event.cancelBubble=true;
    event.stopPropagation();
});


// first level child w/no children (parent=job)
$(".child-click").on('click', function(event) {
    event.stopPropagation();
 });

});

CSS:

ul.mytree  li.liParClosed { 
    background-color: green;
    fontWeight: normal;
}
ul.mytree  li.liParOpen { 
    background-color: cyan;
    fontWeight: normal;
}
.selected{
    border: 3px solid yellow; 
    background-color: yellow;
    fontWeight: bold;
}
ul.mytree  liParOpen selected{ 
    border: 3px solid red; 
    background-color: yellow;
    fontWeight: bold;
}
ul.mytree  li selected{ 
    border: 3px solid red; 
    background-color: yellow;
    fontWeight: bold;
}
ul.mytree  li { 
    background-color: white;
    fontWeight: normal;
}
ul.mytree  { 
    background-color: white;
    fontWeight: normal;
}

You almost got it but the toggling of classes "liParClosed" and "liParOpen" wasn't being done right.

Here's a fiddle fixing that issue:

JS Fiddle

Relevant code changes:

      // expand button
  $('#expandTree').on('click', function(event) {
            var all = $('.my-clickable');
            var closed = all.filter('.liParClosed');

            closed.each(function(){
        $(this).removeClass('liParClosed').addClass('liParOpen');
                $(this).next('ul').slideToggle();
            });
    }); // end expand button 

    // collapse button
  $('#collapseTree').on('click', function(event) {
            var all = $('.my-clickable');
            var open = all.filter('.liParOpen');

            open.each(function(){
          $(this).removeClass('liParOpen').addClass('liParClosed');
                $(this).next('ul').slideToggle();
            });
    }); // end collapse button 

Look at the way the classes are being added/removed. Also, you were looking for open.find("ul") BUT open itself had to be looped through as it is a list of open li's.

Hope this helps. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM