Sunday, June 12, 2011

Customizing Simple jQuery Tree View (Multi-Level) to add accordion effect to first level

Firstly you should show the original Simple jQuery Tree site jQuery Simple TreeView Plugin

after you briefly review the options for the plugin as

  1. open:  "▼", // HTML string to display for the opened handle
  2. close: "►", // HTML string to display for the closed handle
  3. slide: false, // Boolean flag to indicate if node should slide open/close
  4. speed: 'normal', // Speed of the slide. Can be a string: 'slow', 'fast', or a number of milliseconds: 1000
  5. collapsed: false, // Boolean to indicate if the tree should be collapsed on build
  6. collapse: null, // A node to collapse on build. Can be a string with indexes: '0.1.2' or a jQuery ul: $("#tree ul:eq(1)")
  7. expand: null // A node to expand on build. Can be a string with indexes: '0.1.2' or a jQuery ul: $("#tree ul:eq(1)")
and basic installation as


    $(document).ready(function() {
         $("ul#sitemap").simpletreeview();
    });


now we will describe the customized method that will add accordion effect to the first level of the tree


  1. function toggle($ul, method, callback)
  2.     // this method customized by Ahmed Gouda to support accordion effect
  3.     // only for first level and also highlighting anchor elements
  4.     // Set callback to empty function if undefined
  5.     if (callback === undefined) callback = function() { };
  6.     var $handle = $ul.parent("li").children("span.handle");
  7.     var $anchor = $ul.parent("li").children("a");
  8.     if ($ul.is(':hidden') && $ul.hasClass('SecondLevelUL')) {
  9.         $('ul.SecondLevelUL').each(function(){
  10.         $(this).parent("li").children("span.handle").html(settings.close);
  11.           $(this).parent("li").children("a").css('font-weight', 'normal');
  12.             $(this).slideUp(settings.speed, callback);
  13.         });
  14.        }
  15.     if (method == "open") {
  16.         $handle.html(settings.open);
  17.         $anchor.css('font-weight', 'bold');
  18.         if (settings.slide) { $ul.slideDown(settings.speed, callback); }
  19.         else { $ul.show(); callback(); }
  20.     }
  21.     else if (method == "close") {
  22.         $handle.html(settings.close);
  23.         $anchor.css('font-weight', 'normal');
  24.         if (settings.slide) { $ul.slideUp(settings.speed, callback); }
  25.         else { $ul.hide(); callback(); }
  26.     }
  27.     else {
  28.         $handle.html($ul.is(':hidden') ? settings.open : settings.close);
  29.         if ($ul.is(':hidden'))
  30.             $anchor.css('font-weight', 'bold');
  31.         else
  32.             $anchor.css('font-weight', 'normal');
  33.         if (settings.slide) { $ul.slideToggle(settings.speed, callback); }
  34.         else { $ul.toggle(); callback(); }
  35.     }
  36. }
and here the sample implementation

  1. <script type="text/javascript">
  2.     $(document).ready(function() {
  3.         var sitemap = $("#sitemap").simpletreeview({
  4.             open: '<span style="color: #900;">&darr;</span>',
  5.             close: '<span style="color: #090;">&rarr;</span>',
  6.             slide: true,
  7.             speed: 'fast',
  8.             collapsed: true,
  9.             expand: '0'
  10.         });
  11.         $('a.expand').click(function() {
  12.             sitemap.expand($(this).attr('rel'));
  13.         });
  14.         $('a.collapse').click(function() {
  15.             sitemap.collapse($(this).attr('rel'));
  16.         });
  17.     });                      
  18. </script>
and you can find complete sample from here



keep commenting.

1 comment: