Share via


Summary View Calculations in the Calculations Developer Sample Form [InfoPath 2003 SDK Documentation]

Applies to:

Microsoft Office InfoPath 2003

For information on Service Pack 1 features for working with calculations, see the "Calculating Data" section in the InfoPath 2003 Help.

The Summary view in the Calculations developer sample form is the simpler of the two views that the sample implements. It is used to display a summary of all line item sales for a specified product. The Summary view contains only two fields:

  • Product  This field is bound to the selectedProduct node in the form's underlying XML document. It is implemented as a drop-down list box that uses the Products secondary data source as its source of data.
  • Sales Summary  This field is bound to the productSalesSummary node in the form's underlying XML document. It is implemented as a text box.

When you select a product in the Product drop-down list box, the Product field's OnAfterChange event occurs, which is used to validate the selection and then to call the CalcSalesSummary custom function to add the matching order item totals and display the summarized sales total in the Sales Summary field. The NewValue property of the DataDOMEvent object is used to pass the ID of the selected product as an argument to the CalcSalesSummary custom function. The following is the JScript code for the OnAfterChange event handler, which is used to respond to the OnAfterChange event:

function msoxd_my_selectedProduct::OnAfterChange(eventObj)
{
   if (eventObj.IsUndoRedo)
      return;

   CalcSalesSummary();
}

The CalcSalesSummary custom function is used to calculate the total sales of a specified product by looping through all of the line items for that product and adding the sales figures from each line item. It first sets a reference to the productSalesSummary node of the form's underlying XML document using the selectSingleNode method of the XML Document Object Model (DOM), which is accessed through the DOM property of the XDocument object.

The CalcSalesSummary function then builds an XPath expression, which is used in the selectNodes method of the XML DOM to set a reference to all of the item nodes that match the specified productID. Once the collection of item nodes has been established, the function then loops through that collection, checking whether the item nodes contain a total value, and if they do, adding that value to a variable that contains the sum of all the line item values for the specified product. The following is the JScript code for the CalcSalesSummary custom function:

function CalcSalesSummary()
{
   var salesSummaryNode =
    XDocument.DOM.selectSingleNode("/my:Calculations/my:productSalesSummary");
   var itemTotalExpr =
    "/my:Calculations/my:itemType/my:item[my:itemID = /my:Calculations/my:selectedProduct]/my:itemTotal";
   var itemTotalNodes = XDocument.DOM.selectNodes(itemTotalExpr);
   var itemTotalNode = null;
   var nTotal = 0;

   itemTotalNodes.reset();
   while ((itemTotalNode = itemTotalNodes.nextNode()) != null)
   {
      var nItemTotal = itemTotalNode.nodeTypedValue;

      if (!isNaN(nItemTotal))
      {
         nTotal += Number(nItemTotal);
      }
   }

   salesSummaryNode.nodeTypedValue = nTotal;
}