Help with jscript code for automatic populating repeating table

Joined
Oct 22, 2014
Messages
26
Reaction score
0
I am having real troubles with my jscript code and/or xpath syntax for
InfoPath form code. I'm writing code for users who input two dates and
hit a button that creates rows in a blank repeating table and then it
fills the date column with dates of consecutive months.

e.g., Start Date:1 Feb 2006 Finish Date: 1 May 2006
Table:
Date Cost
==== ====
Feb 2006 0
Mar 2006 0
April 2006 0
May 2006 0

User then inputs Costs. I am working within a default schema, and the
xml schema look like:
Projects/Project/Tasks/Task/Period/Cashflow/Baseline/ group contains
Date and Cost elements,
Projects/Project/Tasks/Task/ contains Start and Finish date elements

I have the psudo-code and the appropriate jscript methods (I think) but
am flaundering with the syntax. I am a rookie jscript'er and having
tons of problems. This is what I have so far:

function CTRL423_5::OnClick(eventObj)
{

//declaration for grabbing data from form's fields
XDocument.DOM.setProperty("SelectionNamespaces",
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-03-23T23:00:00"
xmlns:xd="http://schemas.microsoft.com/office/infopath/2003"');

//Delete blank row at top so just monthly populated rows show
var node =
XDocument.DOM.selectSingleNode(Projects/Project/Tasks/Task/Cashflow/Period[1]"
);
node.parentNode.removeChild( node );

//Calculation for number of months, grab start and end dates
compareStart = new
Date(XDocument.DOM.selectSingleNode("Projects/Project/Tasks/Task/Start");
compareEnd = new
Date(XDocument.DOM.selectSingleNode("Projects/Project/Tasks/Task/Finish");

//calculate number of months
months = (compareStart.getFullYear() - compareEnd.getFullYear()) * 12;
addmonths = compareStart.getMonth() - compareEnd.getMonth();
// The previous calculation could result in a negative number.
months = months + addmonths;
// Adjust month total if the compareStart hasn't
occurred yet this month.
if(compareStart.getDate() < compareEnd.getDate())
{
months--;
}

//iterate the creation of the table rows and insertion
of months
for (var i=1;i<months;i++)
{

// get a reference to the node the repeating table is bound to
IXMLDOMNode row =
thisXDocument.DOM.selectSingleNode("/Projects/Project/Tasks/Task/Cashflow/Period/");

// make a copy of the node by cloning it
row = row.cloneNode(true);

//Get the date to be inputted into the cloned row
currentDate = new Date();
currentDate.setMonth(compareStart.getMonth() + i );

// set the new date of the row's field
row.selectSingleNode("/Projects/Project/Tasks/Task/Cashflow/Period/Baseline/Cost").text
= currentDate;

// append the cloned row to the parent node
IXMLDOMNode parent =
thisXDocument.DOM.selectSingleNode("/Projects/Project/Tasks/Task/Cashflow/Period/");
parent.appendChild(row);

}

Any help would be appreciated. Thanks a bunch!

Anthony
Vancouver, BC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top