1 Reply Latest reply on Aug 24, 2009 6:36 AM by Kevin Cavallo

    Is there a better way to find the parent?

    JHodnefield

      For speed - I coded the block below - in order to find the index of a parent.

      I need to find a specific subform parent - "part".

       

      This could be in any number of levels - so I need to spread out my search.

      When the parent is found - tIndex stores the index of that parent.

       

      There must be a better way to code for this....

       

      This code looks really horrible:

       

       

       

       

       

       

       

       

       

       

       

       

      if

       

       

       

      (obj.parent.name==

      "part")

      {

       

       

       

      var tIndex =

      obj.parent.index;

      }

       

       

       

      if(obj.parent.parent.name==

      "part")

      {

       

       

       

      var tIndex =

      obj.parent.parent.index;

      }

       

       

       

      if(obj.parent.parent.parent.name==

      "part")

      {

       

       

       

      var tIndex =

      obj.parent.parent.parent.index;

      }

       

       

       

      if(obj.parent.parent.parent.parent.name==

      "part")

      {

       

       

       

      var tIndex =

      obj.parent.parent.parent.parent.index;

      }

       

       

       

      if(obj.parent.parent.parent.parent.parent.name==

      "part")

      {

       

       

       

      var tIndex =

      obj.parent.parent.parent.parent.parent.index;

      }

       

       

       

      if(obj.parent.parent.parent.parent.parent.parent.name==

      "part")

      {

       

       

       

      var tIndex =

      obj.parent.parent.parent.parent.parent.parent.index;

      }

       

       

       

      if(tIndex==null

      )

        • 1. Re: Is there a better way to find the parent?
          Kevin Cavallo Level 4

          How about something like this (untested!).  It should return the index of the most immediate parent that is named part or -1 if none found.

           

          function findIndex(myObj)
          {
          var ind = -1;
          var obj = myObj.parent;

          while (obj != null)
            {
            if (obj.name == "part")
             {
             ind = obj.index;
             break;
             }
            else
             obj = obj.parent;
            }

          ret ind;
          }

          1 person found this helpful