Section 10.8.  Calculations

Prev don't be afraid of buying books Next

10.8. Calculations

Another common use for scripts in InfoPath is to perform calculations, such as totals, averages, tax amounts, or currency conversions. Calculation scripts are often associated with the OnAfterChange event, which occurs after validation. This is useful for updating fields in response to changes in other fields, for example to update a total when a line item has changed.

Suppose we want each line item in our order form to reflect the total cost of that item, namely the quantity times the price. In addition, we want a summary section that shows the total number of items ordered, and the total cost of the order. We could associate script functions with the quantity and price fields, so that any time either of them change, the totals are updated. The script functions might look like Example 10-5.

Example 10-5. A calculation script
 function msoxd_ns1_quant::OnAfterChange(eventObj) {   if (eventObj.IsUndoRedo)       return;   updateCost(eventObj.Site.parentNode)   updateTotals() } function msoxd_ns1_price::OnAfterChange(eventObj) {   if (eventObj.IsUndoRedo)       return;   updateCost(eventObj.Site.parentNode)   updateTotals() } 

This function just calls two other functions, updateCost and updateTotals, that are defined later in the script file. The updateCost function, shown in Example 10-6, calculates the cost of each line item.

Example 10-6. Calculating the cost
 function updateCost(thisItemNode) {   var thisQuant = thisItemNode.selectSingleNode("ns1:quant")   var thisPrice = thisItemNode.selectSingleNode("ns1:price")   var thisCost = thisItemNode.selectSingleNode("ns1:cost")   var nQuant = parseFloat(thisQuant.nodeTypedValue)   var nPrice = parseFloat(thisPrice.nodeTypedValue)   var nCost = nPrice * nQuant;   thisCost.nodeTypedValue = nCost } 

On lines 3 through 5, it assigns the three relevant nodes to variables. Then, on lines 6 and 7, it extracts the typed value of the quantity and price nodes and converts them to JScript floating point numbers using the paraseFloat function. This is necessary for them to be used in mathematical calculations. Line 8 calculates the cost and assigns it to the nCost variable, and finally the cost node is updated by setting the thisCost.nodeTypedValue equal to nCost.

Our second calculation function, updateTotals, appears in Example 10-7. It calculates the two values in the summary section, namely the number of items and the order total. The logic is similar to the updateCost function, except that it calls yet another function, sum, to calculate the sum.

Example 10-7. Calculating the totals
 function updateTotals() {   var numItemsNode =     XDocument.DOM.selectSingleNode("//ns1:numItems")   var nNumItems = sum("//ns1:quant")   numItemsNode.nodeTypedValue = nNumItems   var orderTotalNode =     XDocument.DOM.selectSingleNode("//ns1:orderTotal")   var nOrderTotal = sum("//ns1:cost")   orderTotalNode.nodeTypedValue = nOrderTotal } 

It passes the sum function XPath expressions that are used to retrieve the nodes whose values need to be included in the sum. The XPath expressions used in this function use a double slash notation, as in //ns1:quant. This essentially means "all the ns1:quant elements in the document, no matter where they appear."

The sum function is shown in Example 10-8. On line 3, it selects the nodes of interest using the selectNodes method of XDocument.DOM. In our previous examples, we used selectSingleNode because we only wanted to retrieve one node. The selectNodes method can be used instead to retrieve a list of nodes.

Example 10-8. The sum function
 function sum(xpath) {   var nodeList = XDocument.DOM.selectNodes(xpath)   var sum = 0   var xmlNode   while (xmlNode = nodeList.nextNode())      if (xmlNode.nodeTypedValue > 0)     sum += parseFloat(xmlNode.nodeTypedValue);   return sum } 

The function then iterates through the list of nodes using a while loop that assigns one node after another to the xmlNode variable. The value of each node in the list is added to the sum.

In this example, we created a library of three functions that can be called from one or more other functions. You can add an unlimited number of such library functions to the script file. Unlike the functions that are specifically associated with fields and events, you can choose the names of your library functions.

Amazon


XML in Office 2003. Information Sharing with Desktop XML
XML in Office 2003: Information Sharing with Desktop XML
ISBN: 013142193X
EAN: 2147483647
Year: 2003
Pages: 176

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net