LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   javascript - having a problem recusively visiting elements... (https://www.linuxquestions.org/questions/programming-9/javascript-having-a-problem-recusively-visiting-elements-593159/)

BrianK 10-19-2007 09:08 PM

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?

paulsm4 10-20-2007 06:12 PM

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

BrianK 10-22-2007 01:16 PM

Quote:

Originally Posted by paulsm4 (Post 2931069)
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.

BrianK 10-22-2007 02:22 PM

sigh... typo

Quote:

Originally Posted by BrianK (Post 2930260)
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";
                          }
                      }
                    }




All times are GMT -5. The time now is 09:04 PM.