Four Js Development Tools Forum

Discussions by product => Genero BDL => Topic started by: Francois L. on December 13, 2013, 04:39:41 pm



Title: how to get a node id with a text attribute or a name attribute
Post by: Francois L. on December 13, 2013, 04:39:41 pm
OK,

Newbie question on DOM tree.

I need to find a a node id previously created with either its name or its text attribute.

How is it done??

Do I really to scan the whole DOM tree to find it??

Thanks

Francois


Title: Re: how to get a node id with a text attribute or a name attribute
Post by: Nuno G. on December 13, 2013, 05:04:19 pm
As far as I know you don't have what I would call a nodeId within the AUI tree.
However I hope this example helps you.
Best  regards
Nuno


Title: Re: how to get a node id with a text attribute or a name attribute
Post by: Jeff M. on December 13, 2013, 05:14:40 pm
Here is a function that will return the node as an om.DomNode variable. Check it's not null to get the node id use the following code:

DEFINE ld_found    om.DomNode


LET ld_found = get_dom_node("Button", "button_name")  -- Object type (eg Button is case sensitive - check the debug tree)

IF ( ld_found IS NOT NULL ) -- We found it
THEN
    DISPLAY "Id = ", ld_found.getId()
END IF

-- THE FUNCTION

FUNCTION get_dom_node(lv_object, lv_objname)

    DEFINE lv_object                    STRING
    DEFINE lv_objname                   STRING

    DEFINE lv_value                     STRING

    DEFINE lv_path                      STRING
    DEFINE ld_rootn                     om.DomNode
    DEFINE ln_list                      om.NodeList
    DEFINE ld_child                     om.DomNode
    DEFINE lv_loop                      SMALLINT

    LET ld_rootn = ui.Interface.getRootNode()
    LET lv_path = "//", lv_object CLIPPED, "[@name='", lv_objname CLIPPED, "']"
    LET ln_list = ld_rootn.selectByPath(lv_path)

    LET lv_value = NULL
    LET ld_child = NULL

    FOR lv_loop = 1 TO ln_list.getLength()
        LET ld_child = ln_list.item(lv_loop)

        -- If uncommented this would get the name attribute
        -- LET lv_value = ld_child.getAttribute("name")

        EXIT FOR    -- We are getting just the first one
    END FOR

    RETURN ld_child        -- Must return into an om.DomNode variable

END FUNCTION


Title: Re: how to get a node id with a text attribute or a name attribute
Post by: Francois L. on December 13, 2013, 05:24:17 pm
thanks guys

I thought that it would be the only way because a text or name attribute can be the same for multiple node.

I will certainly try that.

Francois


Title: Re: how to get a node id with a text attribute or a name attribute
Post by: Reuben B. on December 15, 2013, 08:48:57 pm
I'd go with om.DomNode.selectByPath() as per Jeff's answer.  I'd just add that the first part of the XPath syntax can include * to indicate look at all nodes so you may want something like ...

Code
  1. FUNCTION find_by_name(name)
  2. DEFINE name STRING
  3. DEFINE r,n om.DomNode
  4. DEFINE nl om.NodeList
  5. DEFINE i INTEGER
  6.    LET r= ui.Interface.getRootNode()
  7.    LET nl = r.selectByPath(SFMT("//*[@name=\"%1\"]", name))
  8.    FOR i = 1 TO nl.getLength()
  9.        LET n = nl.item(i)
  10.        DISPLAY SFMT("Found node with name=%1 of type %2 and id %3", name, n.getTagName(), n.getId())
  11.    END FOR
  12. END FUNCTION


... the other thing to be aware of.  The om class is a cut-down set of routines designed to help with manipulating the user interface DOM tree.  If you are dealing with an XML file and not the user interface DOM tree, you may find the XML package has more functionality https://4js.com/online_documentation/fjs-fgl-manual-html/#c_gws_XML_Library_001.html