Listing the value of all fields in the datasource - InfoPath Dev
in

InfoPath Dev

Listing the value of all fields in the datasource

Last post 03-04-2006 02:52 AM by rjohnson. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 02-22-2006 04:15 PM

    • rjohnson
    • Top 500 Contributor
    • Joined on 09-01-2005
    • Australia
    • Posts 28

    Listing the value of all fields in the datasource

    Hi everyone,

    Is it possible to use code to run through all elements in the DataSource and display the output of each field. (ie: via a while loop)

    Also, is this possible to do this for a given folder within the DataSource?
  • 02-23-2006 07:18 AM In reply to

    Re: Listing the value of all fields in the datasource

    Hi,
    Try this XPath you should be able to get all the child nodes with it.
    node.selectNodes("descendant::*");
    then loop through these nodes and output those values where ever you want.

    Agni
    INFOPATHdev India
  • 02-23-2006 06:05 PM In reply to

    • rjohnson
    • Top 500 Contributor
    • Joined on 09-01-2005
    • Australia
    • Posts 28

    Re: Listing the value of all fields in the datasource

    Hi Agni,

    Thanks for your reply, but I am confused with your example code.

    I'll try to clarify my question:
    Pretend I have 5 field(elements), under the myFields group. I want to run some code which will find all fields under the myFields group and then display the fields value one at a time.

    From your code, I'm not sure what node is. I would assume that node is a variable, that is the group that I want to start the search from. However, I am not sure how to create such a variable.
    I would guess:
    var node = XDocument.DOM.selectSingleNode("/my:myFields");
    but that doesn't feel right.

    I am also confused when you said loop through the nodes, as I am confused how to do this as well.

    Any help would be greatly appreciated.

    Thanks

    - Reg.
  • 02-24-2006 10:12 AM In reply to

    Re: Listing the value of all fields in the datasource

    Hello,
    When you do a selectNodes you will get all of the nodes as a nodelist. Then you can have a for each loop that will walk through all of these nodes and get the information that you need.
    Shanthi Reddy
  • 03-03-2006 06:24 AM In reply to

    • rjohnson
    • Top 500 Contributor
    • Joined on 09-01-2005
    • Australia
    • Posts 28

    Re: Listing the value of all fields in the datasource

    Hi Shanthi,

    Thanks for your reply.

    I tried the selectNodes idea that was given before by Agni, but as noted I couldn't get it to work.

    When I tried again, I noticed the length of the nodelist is always 1.
    I however noticed that it shows the number of a repeating section, but this is not what I wanted.

    After searching the internet, I found an example of how you can get the childNodes of a given element (I can't remember which site it was from now, but believe it was another InfoPath Dev Site). I then added to the code to make it work for me and managed to get all the descendants by running through the nodes in a recursive manner.
    (I've attached some code at the bottom of this comment)

    If I am wrong about the selectNodes not being able to select all Nodes in a dataSource, could you please verify that.

    Thanks again for your input.

    Regards,

    - Reg.


    Code example:
    -----------------------

    function CTRL2_5::OnClick(eventObj)
    {
    var theNode = XDocument.DOM.selectSingleNode("/my:myFields");
    getDescendants( theNode );
    }

    function getDescendants( Node )
    {
    var nodeList = Node.childNodes;
    var item;

    for (var i = 0; i < nodeList.length; i++)
    {
    item = nodeList.item(i);

    // if the item is an element in the dataSource,
    // and not the elements "text".
    if ( item.nodeType == 1 )
    {
    // Outputs the item's value
    XDocument.UI.Alert( item.text )

    // find the descendants of the current node
    getDescendants( item );
    }
    }
    }
  • 03-03-2006 07:25 PM In reply to

    Re: Listing the value of all fields in the datasource

    Hi rjohnson,

    I think you're wasting your time with this getDescendents() function! It's already implemented as an XPath axis. Check out this tutorial:

    http://www.w3schools.com/xpath/xpath_axes.asp

    Also, run this code snippet to get a feel of what's going on:

    	var nl = XDocument.DOM.selectNodes( "/my:myFields/descendant::*" );
    var str = "";

    while( (node = nl.nextNode()) != null )
    {
    //str += node.xml;
    //str += node.text;
    str += node.nodeName;
    str += "\n------------\n";
    }

    //XDocument.DOM.selectSingleNode( "//my:field7" ).text = str;
    XDocument.UI.Alert( str );


    Matt Faus / Microsoft InfoPath MVP
    Qdabra® Software / Streamline data gathering to turn process into knowledge
  • 03-04-2006 02:52 AM In reply to

    • rjohnson
    • Top 500 Contributor
    • Joined on 09-01-2005
    • Australia
    • Posts 28

    Re: Listing the value of all fields in the datasource

    Hi Matt,

    Thanks for your solution,

    I didn't know how to use selectNodes to get all the descendants, but I now do, thanks to your example.

    Cheers,

    - Reg.
Page 1 of 1 (7 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.