473,804 Members | 3,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

tree menu, checking / uncheching all child items, client side, possible?

67 New Member
Hi friends,

I'm having a menu like this -



that I've fetched from the database, using recursive function, where data is stored as - id | title | parent

I need a javascript function, that will help me to check / uncheck child elements (if present) when I click on a parent at different level.

Like,
  • if I check / uncheck Test March, all the check boxes should be checked / unchecked.
  • If I check / uncheck A scent during sleep, only this title should be checked / unchecked.
  • If I check / uncheck Sub March 1, this title and all titles following this should be checked / unchecked.
  • If I check / uncheck test sub sub march, the last two should be checked / unchecked.

Wondering, if anyone is having any idea here on how to do this.

Thanks.
Dec 16 '07 #1
23 2797
kigoobe
67 New Member
I thought to give an update here -

At this moment, I'm having a javascript array, having a form id~title~parent -

Expand|Select|Wrap|Line Numbers
  1. menuArray["59"] = "174~Test March~1";
  2. menuArray["60"] = "178~A Scent During Sleep~174";
  3. menuArray["61"] = "175~Sub March 1~174";
  4. menuArray["62"] = "208~Sub sub March 2~175";
  5. menuArray["63"] = "209~Sub level 5~208";
  6. menuArray["64"] = "210~Sub level 6~209";
  7. menuArray["65"] = "211~Sub level 7~210";
  8. menuArray["66"] = "207~test Sub Sub March~175";
  9. menuArray["67"] = "229~test Sub March 03-23~207";
Hmmm ... now what ?

Do I need a js recursive function here ?

Conceptually, I need a function here, like -

Expand|Select|Wrap|Line Numbers
  1. function check(id, array) {
  2.  for(var i=0;i<array.length;i++) {
  3.   var vArr = array[i].split("~");
  4.   if (vArr[0] == id) {
  5.   newMenu[] = vArr[0];
  6.   check(vArr[2],array); // this doing the recursion, but again, I need 
  7. // something similar in javascript, and what about multiple submenus at 
  8. // the same level, this function will probably take just one line.
  9.   }
  10.  }
  11. }
Dec 17 '07 #2
kigoobe
67 New Member
Hi guys

Well, I am storing a multilevel menu in mysql, in the following way.

id | menu_name | parent_id

where, ids are linked to each other with parent_id. ie, if menu_y is a child of menu_x, for menu_y the parent_id would be menu_x's id.

In the webpage, the menu is listed with a check box associated with each menu.

I need to make a javascript array, where all the menu items will be listed along with their id and parent_id. Then, I need a way to figure out a given menu's children and subsequent descendents.

The idea is, once we check / uncheck a check box, all it's descendants get checked / unchecked.

Any tips / hints here friends? Is the recursive thing a possible solution here, that could help me to get all descendents of a menu from it's id?

I had a post earlier on this subject

Jan 20 '08 #3
kigoobe
67 New Member
Speaking about the recursive function, here is what I'm using server side, for php ...

[php]
function getNodes($menu_ id, $level = 0, $version='') {
global $myversion;
global $arrCheckMenu;
$sql = "SELECT id, menu_name, parent_menu FROM menu_management WHERE parent_menu='". $menu_id."' AND version = '".$version. "' ORDER BY position";
$result = mysql_query($sq l);
while ($row = mysql_fetch_arr ay($result)) {
if ($row['id']!=1) {
echo '<p><input type="checkbox" name="check_men uid[]" value="'.$row['id'].'">&nbsp;'.str _repeat('&nbsp; »&nbsp;', $level-1).$row['menu_name'].'</p>';
}
getNodes($row['id'], $level+1, $myversion);
}
}
getNodes(0);
[/php]

Probably I need a way to convert this php function to a javascript function, with a modification that instead of printing the checkbox I'll be checking / unchecking it. The function could be called onclick with checkbox ... hmmm ... thinking aloud ...
Jan 20 '08 #4
acoder
16,027 Recognized Expert Moderator MVP
I had a post earlier on this subject

Merged with the previous thread.

To get all the checkboxes, you can use elem.getElement sByTagName("inp ut") where elem is the parent container containing the checkboxes that you need to access. You would also need to check that the elements are checkboxes by checking type.

One other possibility is to use document.getEle mentsByName() instead and use the name of the set of checkboxes.
Jan 21 '08 #5
kigoobe
67 New Member
Thanks acoder ... but the problem is, how to get all child elements recursively !!!

Jan 21 '08 #6
acoder
16,027 Recognized Expert Moderator MVP
For recursion to work, it needs to have an end case where it can stop.

Am I correct in assuming that no parent id is greater than the menu id, e.g. a menu has an id of, say, 175. Its parent id cannot be greater than 175?
Jan 21 '08 #7
kigoobe
67 New Member
thanks again, acoder. Actually, no. A menu can have a parent_id which is greater than it's id. Because, id is getting attributed when a menu is getting created, and at that given point it's parent is always smaller than it's id.

However, when we are moving individual menu items to reorganize the menu, we can easily place a menu created earlier as a child of a menu created later.

Does this makes the task more difficult? To get the end, we can either start from zero (telling that's the end), or can loop through a javascript array (where we can store all data collected using php on page load) to find the maximum number.

What is your suggestion here ?
Jan 21 '08 #8
acoder
16,027 Recognized Expert Moderator MVP
No, I was thinking in terms of efficiency, but here is a better idea. Earlier, you posted this:
At this moment, I'm having a javascript array, having a form id~title~parent -

Expand|Select|Wrap|Line Numbers
  1. menuArray["59"] = "174~Test March~1";
  2. menuArray["60"] = "178~A Scent During Sleep~174";
  3. menuArray["61"] = "175~Sub March 1~174";
  4. menuArray["62"] = "208~Sub sub March 2~175";
  5. menuArray["63"] = "209~Sub level 5~208";
  6. menuArray["64"] = "210~Sub level 6~209";
  7. menuArray["65"] = "211~Sub level 7~210";
  8. menuArray["66"] = "207~test Sub Sub March~175";
  9. menuArray["67"] = "229~test Sub March 03-23~207";
How about changing it to:
Expand|Select|Wrap|Line Numbers
  1. menuArray[174][0] = "Test March";
  2. menuArray[174][1] = 1;
  3. menuArray[178][0] = "A Scent During Sleep";
  4. menuArray[178][1] = 174;
and so on. This avoids having to split the string to get the values.

A tree-based store would be better, but if we're using an array, we need to get all the children and their children. A function could get the children by checking the parent id of each array item. If it matches, then get its children too. The recursion would stop when there are no children.
Jan 21 '08 #9
kigoobe
67 New Member
Thanks ... so first I work on to make the first function to make the changed array, and then a second function for the rest ...

thanks ... let's see how far I can go with this wisdom ... :)
Jan 21 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

22
11102
by: Marek Mand | last post by:
How to create a functional *flexible* UL-menu list <div> <ul> <li><a href=""></li> <li><a href=""></li> <li><a href=""></li> </ul> </div> (working in IE, Mozilla1.6, Opera7 (or maybe even in Opera6))
4
1987
by: Robin Tucker | last post by:
Hi, I'm currently implementing a database with a tree structure in a table. The nodes in the tree are stored as records with a column called "Parent". The root of the tree has a "NULL" parent. The path to each node is stored in the column "Path" and is of the form "\000001\000002\000003\" etc. The latter enabling me to fetch subtrees using the "LIKE" predicate. I also have created the relation "ID" <-> "ID_Parent, effectively the...
2
3905
by: Jackson Yap | last post by:
can someone kind enough to help me look at the attached html and js file? Why is it that the javascript menu could not work at www.apchosting.net but could work at http://home.pacific.net.sg/~jacksony ? (the drop down bar could not work at www.apchosting.net but can drop at home.pacific.net.sg. I suspect it is a server problem but was told it is not possible, therefore assuming it is a client script problem? the script works last time...
19
6792
by: Christian Fowler | last post by:
I have a VERY LARGE pile of geographic data that I am importing into a database (db of choice is postgres, though may hop to oracle if necessary). The data is strictly hierarchical - each node has one, and only one parent. The depth should not exceed 6 or 7 levels. The initial import will have about 6 million leaves, and 3 million branches. I would expect the leaves to grow significantly, in number easily tripling. However, the branches will...
4
9023
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working, even the removal, I just don't know how to keep track of the parent, so that I can set its child to the child of the node to be removed. IE - if I had C / \ B D
3
1374
by: MrNobody | last post by:
I have a situation where I add over a hundred child MenuItems to a parent MenuItem. When I click the parent which shows a listing of all the child menu items, it is too big to fit on the page, so it has these up/down arrows so you can scroll through it. This is great, except this window it drew takes the entire height of my screen so it looks a little silly. How can I restrict the size of this menu, so it shows only like 20 menu items and...
7
2718
by: Chuck Hartman | last post by:
I have a Windows service that requests web pages from a site using an HttpWebRequest object. When I try to request a page from an ASP.NET 2 site, I get a WebException with message "The remote server returned an error: (500) Internal Server Error." I found a post that suggested to catch the WebException to retrieve the actual HttpWebResponse object for more information. The response returned is shown below. At first I thought this was a...
1
2168
by: Anthony | last post by:
Below is a script I found at http://javascript.internet.com/ for a cascading menu. The script works great but there is one thing that I would like modified. BecauseI am just learning javascript, I did not want to try to modify the code without a little help. When you place the mouse over the menu bar, this script calls the function to show the menu. I would like it modified to hide the menu when the mouse is removed. Please help. The...
0
1124
by: rmkumar | last post by:
hi everyone, I have created a tree control (CTreeCtrl) in a dialog and i have created context menu which will be shown when Right click on Child Items. I'm unable to get corrct coordinate for child items. i used HitTest() to get clicked item but it returns NULL and Mouse point Coordinates shows different values while the GetItemRect() gives different..... i'm so confused to match those things... can any one help me to enable context...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10337
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9160
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6854
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5654
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2995
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.