Go Back   SitePoint Forums > Forum Index > Program Your Site > JavaScript
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Nov 7, 2009, 00:45   #1
digitalbart
SitePoint Enthusiast
 
digitalbart's Avatar
 
Join Date: Apr 2002
Location: chicago
Posts: 72
jquery ajax menu collapse expand

Hi I am trying to build something like a tree menu but not exactly. You click a menu, which then does a ajax request for submenus and then displays them. You can then click on the submenus again and get another level of submenus. My problem is sometimes when click on the submenus it does close the menu or sometimes will keep adding to the existing submenu, menaing it will add an identical copy of the submenu, so you see two of the everything. I am using .live to and then is.hidden to see if the menu is visible. Any thoughts as to how I can get this work, toggle can be used with .live as far as I know because you have to click twice?

JavaScript Code:
$(document).ready(function(){
 
 
  $('.cat',this).toggle(
    function () {
      var id = $(this).attr('href');
      if($('.box'+id).text() == '') { 
    var rand = '?random=' + Math.random();           
        $.getJSON('mclicks.php'+rand,{id:id},function(data){ 
          $.each(data, function(i,item){                               
            $.each(item, function(intIndex, objValue) {
             // console.log(this);
                 $("div.box"+id).append("<a class='ahref' href='" + intIndex + "' id='"+id+"' onclick='return false;'>" + objValue + "</a> <div class='sub" + intIndex + " data'></div><br /> ");
             });
            $("div.box"+id).addClass("data").slideDown(200);         
          })
        })
        // $(this).html("Hide Clicks");
      } else {
        // $(this).html('Hide Clicks'); 
        $('.box'+id).slideDown(200)
      } 
    }
    function () { 
      var id = $(this).attr('href');
      //$(this).html('View Clicks'); 
      $('.box'+id).slideUp(200)
    } 
  );
 
 
 
   $('.ahref',this).live("click", function(){
   
 
   // e.preventDefault(e);
     
   var id = $(this).attr('id');
   
   var subid = $(this).attr('href');
   
 
 
if($('div.sub'+subid).is(':hidden')) {
 
   
    var text1 = $('div.sub'+subid).length;
     
          var rand = '?random=' + Math.random();
               
        $.getJSON('mclicks.php'+rand,{subid:subid,id:id},function(data){ 
          $.each(data, function(i,item){                               
            $.each(item, function(intIndex, objValue) {
             // console.log(this);  + intIndex +   + intIndex +
                 $("div.sub"+subid).append("<a class='ahref' href='' onclick='return false;'>" + objValue + "</a> <div class='sub data'></div><br /> ");
             });           
          });
           
        })
       
      $(".sub"+subid).addClass("data").slideDown(200);
 
 
 } else {
      $('.sub'+subid).slideUp(200);
      $("div.sub"+subid).remove();
          var text1 = $('div.sub'+subid).length;
        console.log(text1);
 }
 
      }, function () { 
        // $(this).html('Hide Clicks'); 
        $('.sub'+subid).slideUp(100);
         $("div.sub"+subid).remove();
          var text1 = $('div.sub'+subid).length;
        console.log(text1);           
  });

HTML4Strict Code:
<a onclick="return false;" id="3" href="27" class="ahref">Car Washes</a>
 
<div class="sub27 data" style="display: block;"><a onclick="return false;" href="" class="ahref">Chevrolet</a> <div class="sub data"/><br/> <a onclick="return false;" href="" class="ahref">Soft Wash</a> <div class="sub data"/><br/> <a onclick="return false;" href="" class="ahref">Detail Hand Car Wash</a> <div class="sub data"/><br/> </div>
digitalbart is offline   Reply With Quote
Old Nov 7, 2009, 06:24   #2
markbrown4
padawan
 
markbrown4's Avatar
 
Join Date: Jul 2006
Location: Victoria, Australia
Posts: 2,733
I know it doesn't answer your question, but imo this is a really bad use of Ajax, of all elements on a website the main menu should be fast and accessible.

I would suggest loading all the menu content into the page and then just toggling the submenus.
markbrown4 is offline   Reply With Quote
Old Nov 7, 2009, 07:50   #3
digitalbart
SitePoint Enthusiast
 
digitalbart's Avatar
 
Join Date: Apr 2002
Location: chicago
Posts: 72
Thank you for the response and sorry for the confusion. I will try to explain.

The menu is a directory built from a database and there is over 800 items in this menu. It is not the main menu per say but is the main part of the page. To load this all at once into the browser would not be good. I am using ajax to get only parts and then display them as the user browses a category, subcategory and then listing.

My question is toggle was not working when I would get json data back from the server. So I then added .live() to attached the new Ajax part of the menu to the Dom. However now I can't reliable toggle the sub menu element using and if else. The sub menu is not part of the initial page load is appended to the page based on what the user clicks on.


JavaScript Code:
$('.ahref',this).live("click", function(){
   
 
   // e.preventDefault(e);
     
   var id = $(this).attr('id');
   
   var subid = $(this).attr('href');
   
 
 
if($('div.sub'+subid).is(':hidden')) {
 
   
  gets JSON DATA HERE THEN SHOWS IT
       
      $(".sub"+subid).addClass("data").slideDown(200);
 
 
 } else {
      $('.sub'+subid).slideUp(200);
      $("div.sub"+subid).remove();
 
 }
 
      }, function () {
        // $(this).html('Hide Clicks');
        $('.sub'+subid).slideUp(100);
         $("div.sub"+subid).remove();
       
  });
digitalbart is offline   Reply With Quote
Old Nov 8, 2009, 06:15   #4
markbrown4
padawan
 
markbrown4's Avatar
 
Join Date: Jul 2006
Location: Victoria, Australia
Posts: 2,733
Hi,

I'm not all too familiar with the syntax in your code but here's how I would go about it. The main thing you are missing is to assign the same events that you have setup on page load when you load the new content.
I would nest the elements so that the sub content is inside the parent
html Code:
<ul id="tree">
  <li id="sub1" class="sub">linky linky</li>
  <li id="sub2" class="sub">linky linky</li>
</ul>
and break the code up into functions like:
javascript Code:
var Tree = {
  init: function() {
    var tree=$('#tree');
    setupEvents(tree);
  },
  setupEvents: function(elm) {
    $(elm).find('.sub').click(function() {
      ..loadContent(this)
      ..toggle
    });
  }
  loadContent: function(elm) {
    ..getJSON()
    ..append()
    [b]setupEvents(elm);[/b]
  },
};
$(document).ready(function() { Tree.init() });
Hope it helps
markbrown4 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 19:05.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved