Copy data from fields to cloned node - InfoPath Dev
in

InfoPath Dev

Copy data from fields to cloned node

Last post 07-25-2007 10:14 PM by Patrick Halstead. 9 replies.
Page 1 of 1 (10 items)
Sort Posts: Previous Next
  • 04-25-2006 12:21 AM

    • spudy
    • Top 150 Contributor
    • Joined on 03-23-2006
    • United Kingdom
    • Posts 67

    Copy data from fields to cloned node

    I have a data collection view with dropdowns and text boxes. The save button is supposed to clone a row from a repeating group to store data, then copy the data collection fields into cloned node. This is then appended to parent.

    This works ok:

    // get a reference to the node where the data is to be stored
    var row = XDocument.DOM.selectSingleNode("//my:RoomData/my:RoomDetail") ;
    // make a copy of the node by cloning it
    row = row.cloneNode(true) ;
    //copy data to cloned node
    row.selectSingleNode("my:field1").text = XDocument.DOM.selectSingleNode("//my:field1a").nodeTypedValue;
    // append the cloned row to the parent node
    var parent = XDocument.DOM.selectSingleNode("//my:RoomData") ;
    parent.appendChild(row) ;
    // Clear the RoomDetail
    XDocument.DOM.selectSingleNode("//my:field1a").text = "";
    //change view
    XDocument.View.SwitchView("Internal Works");


    The problem is that there are 61 fields and obviously I want to avoid repetitive lines of code. So then I tried to perform the copy with looping code. This however does not work but alas an error message is not given. I suspect the problem lies in the count but without error I am lost. Here is the revised code:


    // get a reference to the node where the data is to be stored
    var row = XDocument.DOM.selectSingleNode("//my:RoomData/my:RoomDetail") ;
    // make a copy of the node by cloning it
    row = row.cloneNode(true) ;
    // set the new values of the row’s fields. Copying data from my:field1 to myfielda1, my:field2 to my:fielda2 etc.. up to 61 fields
    var i = 1;
    //count the number of fields in the row
    var fieldnumber = row.count;
    while ( i <= fieldnumber )
    {
    var fieldTitle = "my:field" & i;
    var fieldaTitle = "//my:fielda" & i;
    row.selectSingleNode(fieldTitle).text = XDocument.DOM.selectSingleNode(fieldaTitle).nodeTypedValue;
    i++;
    }
    return(i);
    // append the cloned row to the parent node
    var parent = XDocument.DOM.selectSingleNode("//my:RoomData") ;
    parent.appendChild(row) ;
    // Clear the fields from data collection view
    var i = 1;
    while ( i <= fieldnumber)
    {
    var fieldaTitle = "//my:fielda" & i;
    XDocument.DOM.selectSingleNode(fieldaTitle).text = "";
    i++;
    }
    return(i);
    //now switch from the data collection view
    XDocument.View.SwitchView("Internal Works");


    Any help would be greatly appreciated.

    Regards

    Spudy
  • 04-25-2006 01:51 AM In reply to

    Re: Copy data from fields to cloned node

    Hi,

    U have given a return(i) statement before appending the cloned node.
    Infact u have given this return(i) statement twice.
    Remove the return(i) statemtnt that u have placed before appending the cloned node.


    // get a reference to the node where the data is to be stored
    var row = XDocument.DOM.selectSingleNode("//my:RoomData/my:RoomDetail") ;
    // make a copy of the node by cloning it
    row = row.cloneNode(true) ;
    // set the new values of the row’s fields. Copying data from my:field1 to myfielda1, my:field2 to my:fielda2 etc.. up to 61 fields
    var i = 1;
    //count the number of fields in the row
    var fieldnumber = row.count;
    while ( i <= fieldnumber )
    {
    var fieldTitle = "my:field" & i;
    var fieldaTitle = "//my:fielda" & i;
    row.selectSingleNode(fieldTitle).text = XDocument.DOM.selectSingleNode(fieldaTitle).nodeTypedValue;
    i++;
    }
    return(i);//This is where the problem lies.Remove this
    // append the cloned row to the parent node
    var parent = XDocument.DOM.selectSingleNode("//my:RoomData") ;
    parent.appendChild(row) ;
    // Clear the fields from data collection view
    var i = 1;
    while ( i <= fieldnumber)
    {
    var fieldaTitle = "//my:fielda" & i;
    XDocument.DOM.selectSingleNode(fieldaTitle).text = "";
    i++;
    }
    return(i);//I think even this statement should be placed after changing the view.
    //now switch from the data collection view
    XDocument.View.SwitchView("Internal Works");

    Regards,
    Abhishek
  • 04-25-2006 03:26 AM In reply to

    • spudy
    • Top 150 Contributor
    • Joined on 03-23-2006
    • United Kingdom
    • Posts 67

    Re: Copy data from fields to cloned node

    Abhishek, thanks for your response

    This first loop copies each of 61 fields of one row to the cloned node. Only after the copy has run through this first loop 61 times using the row.count it then appends node to parent.
    The return statement is a required part of the first loop I thought. There is then another loop further down, hence two returns.


    while ( i <= fieldnumber )
    {
    var fieldTitle = "my:field" & i;
    var fieldaTitle = "//my:fielda" & i;
    row.selectSingleNode(fieldTitle).text = XDocument.DOM.selectSingleNode(fieldaTitle).nodeTypedValue;
    i++;
    }
    return(i);
  • 04-25-2006 06:23 AM In reply to

    • spudy
    • Top 150 Contributor
    • Joined on 03-23-2006
    • United Kingdom
    • Posts 67

    Re: Copy data from fields to cloned node

    Further to my last comment I must add that the original code did not even change the view.
    Once I removed the return(i) the view changed but text was not copied to the cloned row.
  • 04-26-2006 01:43 AM In reply to

    Re: Copy data from fields to cloned node

    Maybe I am missing something basic, but these are the errors that I see in the code
    1. row is an IXMDOMNode not an IXMLDOMNodeList. So row.count would not be correct right? Are you sure that loop is running 61 times? I think you have SelectNodes and do a count on that
    2. Also the CloneNode statement needs to go into the loop as well as the append child.
    Shanthi Reddy
  • 04-27-2006 03:00 AM In reply to

    • spudy
    • Top 150 Contributor
    • Joined on 03-23-2006
    • United Kingdom
    • Posts 67

    Re: Copy data from fields to cloned node

    Perhaps if I describe what it is I am trying to do again.

    I have 61 fields (named fielda1, fielda2, etc to fielda61) that I have used to collect data in a view. I have a cloned node of 61 fields (named field1, field2 etc.. to field61) and I wish to copy the collected data into this cloned node.

    So I need to copy fielda1 to field1, then increment by 1, then copy fielda2 to field2 through until fielda61 to field61 is copied.

    Only once this loop until has copied all 61 fields, only then does the cloned node have all of it's information, and only then does it need to be appended to the parent.

    So my first objective and question here is how to count how many fields(cells?) exist in the cloned node(row?).

    The number is currently 61 but may change in the future. But for the purposes of testing the loop I have hard-coded 61. The example below still does not work:


    {
    // get a reference to the node to clone
    var row = XDocument.DOM.selectSingleNode("//my:RoomData/my:RoomDetail") ;
    row = row.cloneNode(true) ;
    //start counter at 1
    var i = 1;
    while ( i <= 61 )
    {
    var fieldTitle = "my:field" + i;
    var fieldaTitle = "//my:fielda" + i;
    row.selectSingleNode(fieldTitle).text = XDocument.DOM.selectSingleNode(fieldaTitle).nodeTypedValue;
    i++;
    }
    return(i);
    var parent = XDocument.DOM.selectSingleNode("//my:RoomData") ;
    parent.appendChild(row) ;
    XDocument.View.SwitchView("Internal Works");
    }


    This does not work but if I replace loop with the following code it works. So my problem then relates to the loop.


    var i = 1;
    var test = "my:field" + i;
    row.selectSingleNode(test).text = XDocument.DOM.selectSingleNode("//my:fielda1").nodeTypedValue;


    Many thanks for continued help, Paul
  • 04-30-2006 11:27 PM In reply to

    Re: Copy data from fields to cloned node

    Hello Paul,
    With regards to your first objective, you can get the answer by doing .childNodes on the IXMLDomNode. This will give you an IXMLDomNodeList. You can do a .length on this to find out the number of children of the row node

    About why the loop is not working correctly, did you debug to see where its actually failing when you have a loop. You could put a try/catch block and do an alert on the message.

    But I think you definitely should not have return(i) after the loop. What happens if you remove that? Are you still getting an added empty row?
    Shanthi Reddy
  • 05-03-2006 12:49 PM In reply to

    • spudy
    • Top 150 Contributor
    • Joined on 03-23-2006
    • United Kingdom
    • Posts 67

    Re: Copy data from fields to cloned node

    Thanks for your response Shanthi.

    I discovered the problem related to the incorrect function being called. You weere right concerning the return - without it it works!

    Thanks again, Spudy
  • 07-25-2007 03:06 PM In reply to

    Re: Copy data from fields to cloned node

    Hi

     Doe this just cop one field 61 times so that you end up with Field 1, Field 2 etc

     

    If so, this is what I need - I am not sure how to start the code.

  • 07-25-2007 10:14 PM In reply to

    Re: Copy data from fields to cloned node

    Hi Ihmunro and welcome to our forum!

    Are you asking how to add code to an InfoPath form?

    Patrick Halstead
    Project Manager at Qdabra
Page 1 of 1 (10 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.