LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-19-2007, 09:08 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
javascript - having a problem recusively visiting elements...


I'm making my own little ajax-ish directory browser. Not sure if I'm doing it the best way, but, in short, the way it works is that every table row has an id for the directory it holds and an id for that directory's parent. From that, i can use styles to collapse or "uncollapse" a row, so you can click on a directory in the browser to see what's in that directory without reloading the page.

This works fine for expanding the directories, but when i go to collapse them, I'm not collapsing all subdirs - just one level down (all subdirs more than one level down remain - assuming they've been expanded).

Here's the javascript:
Code:
                    function coll(thing) {
                      var b = document.getElementsByTagName("tr");
	
                      for(var i = 0; i < b.length; i++){
                          if(b[i].getAttribute('parent') == thing) {
                            me = b[i].getAttribute('inode')
                            if (b[i].style.visibility == "visibile")
                              coll(me);
                          }
                      }
                      for(var i = 0; i < b.length; i++){
                          if(b[i].getAttribute('parent') == thing) {
                            b[i].style.visibility = (b[i].style.visibility == "visible") ? "collapse" : "visible";
                          }
                      }
                    }
it works on a html that looks like so:
Code:
<tr  id="0" inode="13238975" parent="13189260">
 <td><a href='javascript:coll("13238975")'>FX</a></td>
 <td align='right'>3.15 MB</td>
</tr>
the psuedocode is:

if I've been clicked and my children are collapsed
- make them visible
if I've been clicked and my children are visible
- visit each of my children recursively & collapse them

You can see this all in action here.
Expand all the links then try to collapse at the top level to see what I'm talking about.

... any idea why the recursive collapse isn't working?
 
Old 10-20-2007, 06:12 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
The issue is probably "pass by value" vs "pass by reference".

To recurse, you generally need to "pass by value" (make a copy of the value in question to pass down to the subroutine). Passing an object reference (like "me") is, of course, passing by reference: the subroutine is using *the same* object as the original caller.

Recursion works fine with values like "int". It also works fine where you can pass a pointer like "mytree.next".

Here are a couple of examples:

http://lists.evolt.org/pipermail/jav...ay/000601.html

http://www.devx.com/xml/Article/11866/1954?pf=true
 
Old 10-22-2007, 01:16 PM   #3
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by paulsm4 View Post
The issue is probably "pass by value" vs "pass by reference".

To recurse, you generally need to "pass by value" (make a copy of the value in question to pass down to the subroutine). Passing an object reference (like "me") is, of course, passing by reference: the subroutine is using *the same* object as the original caller.
but because I'm using a simple data type (a string, i believe), aren't I already passing by value? I thought only objects were pass by reference by default?

regardless, if I say something like me2 = me; & then pass me2, that still doesn't work, unless that assignment is also just some sort of pointer.
 
Old 10-22-2007, 02:22 PM   #4
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
sigh... typo

Quote:
Originally Posted by BrianK View Post
Here's the javascript:
Code:
                    function coll(thing) {
                      var b = document.getElementsByTagName("tr");
	
                      for(var i = 0; i < b.length; i++){
                          if(b[i].getAttribute('parent') == thing) {
                            me = b[i].getAttribute('inode')
                            if (b[i].style.visibility == "visibile")
                              coll(me);
                          }
                      }
                      for(var i = 0; i < b.length; i++){
                          if(b[i].getAttribute('parent') == thing) {
                            b[i].style.visibility = (b[i].style.visibility == "visible") ? "collapse" : "visible";
                          }
                      }
                    }
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
functional elements not opening after reboot in Alsa v1.0.13 problem thirulinux SUSE / openSUSE 0 02-01-2007 02:24 AM
Visiting Malaysia kylee LinuxQuestions.org Member Intro 1 01-15-2006 09:31 AM
accessing forms elements with javascript djgerbavore Programming 2 06-03-2005 03:32 PM
Recusively deleting files? The_Nerd Linux - Software 7 05-20-2004 01:32 PM
name/visiting card designing software satimis Linux - Software 4 09-18-2003 08:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration