'for' loop being killed before it's finished.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bugboy
    New Member
    • Sep 2007
    • 160

    'for' loop being killed before it's finished.

    Hi I'm trying to build tree type lists where each row in the list has several cells (columns). My code will render the first list ok then stops. The first 'for' statement should be moving to build the second list, but the loop is killed. The 'list', 'row' and 'cell' constructors are not shown.

    Any idea what's killing the initial iteration.. is it the recursion in getChildren()?

    Thanks for you help!

    Code:
    function  getlist()
    	{ 
    	this.list = arguments[0].split(",");
    	this.render = function()
    		{
    		document.getElementById('workspace').innerHTML="";
    		for(x in this.list)
    			{ 
    			var listid = this.list[x];
    			var rootid = list[listid].root;
    			document.getElementById('workspace').innerHTML += list[listid].html;
    			list[listid].resize();
    			resize('workspace', 'list.'+listid);
    			document.getElementById('list.'+listid).innerHTML += row[rootid].html;//render the root's html
    			getcells(rootid, 'row.'+rootid);
    			getChildren(rootid);
    			}
    		}
    	}
    	
    function getChildren(rowid)
    	{
    	for(n in row[rowid].children)
    		{ 
    		var child = row[rowid].children[n];
    		document.getElementById('children.'+rowid).innerHTML += row[child].html;//add the child row html the the child container
    		getcells(child, 'row.'+child);
    		getChildren(child);//then get the child row's children
    		}
    	} 
    	
    function getcells(rowid, target)
     	{
    	for(y in row[rowid].cell)
    		{ 
    		var cellid = row[rowid].cell[y];
    		document.getElementById(target).innerHTML += cell[cellid].html;//then add the cells to that child row html
    		}
    	}
  • RamananKalirajan
    Contributor
    • Mar 2008
    • 608

    #2
    Hi bugboy,
    Use try and catch block for the functions. Then you will know the source of the bug. Or you have to use alert and chek. Moreover in getChildren() you havent used return, since the function is a recursive() it will be expecting a return.

    sample Code:

    Code:
    try
    {
       //your code goes here.
    }
    catch(e)
    {
     alert(e.message);
    }
    Thanks and Regards
    Ramanan Kalirajan

    Comment

    • phvfl
      Recognized Expert New Member
      • Aug 2007
      • 173

      #3
      Is the list variable on line 10 a global? I can't see it declared anywhere.

      Comment

      • bugboy
        New Member
        • Sep 2007
        • 160

        #4
        'list' 'row' and 'cell' are all objects, i haven't shown their constructors.

        'getChildren' and 'getCells' both write to the target element in the document so a return isn't needed as far as i know.

        Comment

        • bugboy
          New Member
          • Sep 2007
          • 160

          #5
          If i remove the call to it's self inside getChildren it works fine rendering both lists - except it won't get the children of the children so the tree/list only renders two levels deep. If i leave it in place the first tree/list renders fine with multiple levels but then the second list doesn't render at all, it's as if the initial 'for' loop in getList is just forgotten once recursion starts.

          Comment

          • RamananKalirajan
            Contributor
            • Mar 2008
            • 608

            #6
            When are you stopping the recursive function?

            Thanks and Regards
            Ramanan Kalirajan

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              note: be cautious when using a for … in loop, it does not only fetch the numeric members.

              what’s this? (line #26): row[child].html

              Comment

              • bugboy
                New Member
                • Sep 2007
                • 160

                #8
                The recursive function is called inside the for loop so it is only called once for each subchild, when there aren't any subchildren the for loop stops.

                Sorry i'm not sure what you mean by not only numeric members.?

                (line #26): row[child].html is just an html string being retrieved from the row object.

                Comment

                • bugboy
                  New Member
                  • Sep 2007
                  • 160

                  #9
                  I've replaced the getChild for ...in loop with a while and it appears to work!

                  What was the for ..in loop doing wrong?

                  Code:
                  function getChildren(rowid)
                  	{
                  	var x = 0;
                  	while(row[rowid].children[x])
                  		{ 
                  		var child = row[rowid].children[x];
                  		document.getElementById('children.'+rowid).innerHTML += row[child].html;//in the rows children container add the child row html
                  		getcells(child, 'row.'+child);
                  		getChildren(child);//then get the child row's children
                  		x=x+1;
                  		}
                  }

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    Originally posted by bugboy
                    Sorry i'm not sure what you mean by not only numeric members.?
                    numeric members: list[0], list[1], etc. for … in also loops over custom members (anything that is not a numbered member)

                    Comment

                    • bugboy
                      New Member
                      • Sep 2007
                      • 160

                      #11
                      Ahh right, Thanks for your help guys!

                      I'm not sure what the problem was, my arrays only have successive numeric members.. hard to pick the best answer..

                      Comment

                      Working...