ABOUT

Simply downloadWP ajax Collapsing Categoriesplugin from wordpress plugin panel and enable it is the easy way to add WP ajax Collapsing Categories widget.but not the best way.we can just add some code to theme's function.php to make it work.i will explain the details here.

WALKTHROUGH

1.download WP ajax Collapsing Categories,unzip it,put "wp-ajax-collapsing-categories.js" into your theme folder 2.add these lines of code into your theme's function.php (before the last "?>")
/*a function to check if post_is_in_descendant_category ,which will work with */
/**
* a function to check if post_is_in_descendant_category
* @param $cats the category object to check
* @return true if post_is_in_descendant_category or false if not in
*/
  function zxd_post_is_in_descendant_category( $cats, $_post = null ){
  foreach ( (array) $cats as $cat ) {
   $descendants = get_term_children( (int) $cat, 'category');
   if ( $descendants && in_category( $descendants, $_post ) ) return true;
  }
  return false;
}
 /**
 * get all top level categories
 * @return top level categories objects array
 */
 function getTopLevelCats() {
  $allCats = get_all_category_ids();
  $cats = array();
  foreach ($allCats as $id){
   $obj = get_category($id);
   $name = get_cat_name($id);
   if (!$obj -> parent) array_push($cats,$obj);
  }
  return $cats;
 }
 /**
 * wp ajax collapsing categories class
 */
 class ajax_cc_Widget extends WP_Widget{
  function ajax_cc_Widget() {
   $widget_ops = array('classname' => 'widget_ajax_cc', 'description' => 'WP ajax Collapsing Categories' );
   $this->WP_Widget('ajax_cc_widget','ajax_cc_widget',$widget_ops);
  }
  /**
  * implent widget fucntion
  */
  function widget($args, $instance) {
   extract($args, EXTR_SKIP);
   echo $before_widget;
   $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
   if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
   echo '          ' ;
   echo $after_widget;
  }
  /**
  * implent widget fucntion
  */
  function update($new_instance, $old_instance) {
   $instance = $old_instance;
   $title = strip_tags($new_instance['title']);
   $zxdIncludeCats=$new_instance['zxdIncludeCats'];
   $instance = compact('title','zxdIncludeCats');
   return $instance;
  }
  /**
  * implent widget fucntion
  */
  function form($instance) {
   $defaults=array(
   'title' => __('ajax Categories', 'WP ajax collapsing categories'),
   'zxdIncludeCats' => '',
   );
   $options = wp_parse_args( $instance,$defaults );
   extract($options);  
   ?>
3.done,it should work now.without enable a plugin. 4.if your theme already has a javascript loaded,you can just skip the step1,do step2,then find these lines:
wp_register_script( 'wp-ajax-collapsing-categories', get_template_directory_uri().'/wp-ajax-collapsing-categories.js',array('jquery'),'1.0',true);
wp_enqueue_script( 'wp-ajax-collapsing-categories');
wp_localize_script( 'wp-ajax-collapsing-categories', 'ajaxCC', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )) );          
,replace these lines with this one line(replace 'yourloadedjsfilename' with your js file's realname)
wp_localize_script( 'yourloadedjsfilename', 'ajaxCC', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )) );          
then,put these lines in your loaded js file:
/*
+----------------------------------------------------------------+
|                                              |
|  WordPress  Plugin: WP ajax collapsing categories                    |
|  Copyright (c) 2011 ZHAO Xudong                  |
|                                              |
|  File Written By:                                  |
|  - ZHAO Xudong                            |
|  -                             |
|                                              |
|  File Information:                                  |
|  - WP ajax collapsing categories Javascript File                          |
|  - wp-content/plugins/wp-ajax-collapsing-categories/wp-ajax-collapsing-categories.js     |
|                                              |
+----------------------------------------------------------------+
*/
jQuery( function($) {
 var zxdExpander = function() {
  var zxdExpand =$(this);
  var exp ='[—]';
  var exp2 = '[+]';
  if(zxdExpand.html() ==exp){
   zxdExpand.parent('.zxd_ajax_cc_li').children('ul').hide("normal");
   zxdExpand.html(exp2);
  }
  else{
   zxdExpand.html(exp);
   var zxdExpandChild =  zxdExpand.parent('.zxd_ajax_cc_li').children('.child_cat');
   if(zxdExpandChild.length>0) zxdExpandChild.show("normal");
   else{
    var zxdIdDiv = $('.zxd_ajax_cc').eq(0).attr("zid");
    var zxdID =zxdIdDiv?zxdIdDiv:0;
    var zxdCatLink = zxdExpand.parent('.zxd_ajax_cc_li').children('a').attr("href");
    $.ajax({
     type: 'POST',
     url:ajaxCC.ajaxurl,
     data: {
      action : 'zxd_cc_submit',
      catZxd: zxdCatLink,
      currentID:zxdID
     },
     success:function(data) {
      zxdExpand.parent('.zxd_ajax_cc_li').append(data).children('.child_cat').show("normal");
     } ,
     dataType:"html"
    });
   }
  }
 };
 $(".zxd_expand").live ('click',zxdExpander);
})