<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Stefan Cameron on Forms &#187; FormCalc</title>
	<atom:link href="http://forms.stefcameron.com/category/formcalc/feed/" rel="self" type="application/rss+xml" />
	<link>http://forms.stefcameron.com</link>
	<description>Building intelligent forms using Adobe LiveCycle Designer</description>
	<lastBuildDate>Fri, 10 Sep 2010 11:20:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Connecting to a Schema</title>
		<link>http://forms.stefcameron.com/2009/08/03/connecting-to-a-schema/</link>
		<comments>http://forms.stefcameron.com/2009/08/03/connecting-to-a-schema/#comments</comments>
		<pubDate>Mon, 03 Aug 2009 11:00:19 +0000</pubDate>
		<dc:creator>Stefan Cameron</dc:creator>
				<category><![CDATA[Data Binding]]></category>
		<category><![CDATA[Designer]]></category>
		<category><![CDATA[FormCalc]]></category>
		<category><![CDATA[Instance Manager]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[XFA]]></category>

		<guid isPermaLink="false">http://forms.stefcameron.com/2009/08/03/connecting-to-a-schema/</guid>
		<description><![CDATA[In previous tutorials, I have shown how to connect an XFA form to a database and a web service. Next on the list is the XML Schema (XSD). If you work in an enterprise setting, chances are you may have already had to deal with corporate data schemas that define how that data is structured. [...]]]></description>
			<content:encoded><![CDATA[<p>In previous tutorials, I have shown how to connect an XFA form to a <a href="http://forms.stefcameron.com/2006/09/18/connecting-a-form-to-a-database/">database</a> and a <a href="http://forms.stefcameron.com/2007/05/21/connecting-to-a-web-service/">web service</a>. Next on the list is the <a href="http://www.w3.org/TR/xmlschema-1/">XML Schema</a> (XSD).</p>
<p>If you work in an enterprise setting, chances are you may have already had to deal with corporate data schemas that define how that data is structured. The goal, when connecting a form to a schema, is to ensure that the data consumed and output by the form adheres to a specific structure. Any fields bound to data nodes that aren&#8217;t inside the schema space are basically ignored at time of submission.</p>
<p><span id="more-410"></span>
<p>To help put things in context, I decided to define a schema for <a href="http://forms.stefcameron.com/samples/data-binding/schema/movieRental.xsd">movie rentals</a> to stay on the movie theme for the data I used in my other data connection tutorials.</p>
<p>Before we get started, I wanted to point-out that there are so many features related to schema handling and the Data View palette that I won&#8217;t be able to cover them all in a single tutorial. It would be information overload. Instead, I&#8217;ll try to point-out the basics along with a few interesting things that I think would be useful.</p>
<h2>Creating the Connection</h2>
<p><strong><font color="#008000">Right-click in the Data View palette and choose &#8220;New Data Connection&#8221;.</font></strong> In the first step, set the name for the connection (I set it to &#8220;SchemaConnection&#8221; so it&#8217;s easy to identify amongst any other connections that might be defined) and choose &#8220;XML Schema&#8221; as the type.</p>
<p><img src="http://forms.stefcameron.com/images/SchemaConnection/SchemaConnection-1.jpg"></p>
<p>Note that while you may have multiple ODBC and web service data connections, <strong>a form may be connected to only one schema</strong>. As of <a href="http://forms.stefcameron.com/2009/03/24/xfa-30-specification-available/">XFA 3.0</a> (Designer ES2, Acrobat/Reader 9.1), however, you can inject foreign data nodes into the Data DOM even if they aren&#8217;t in the schema, which may help if you need to get extra data into/out of your form (a topic for another post; for now, have a look at the new &#8220;File &gt; Form Properties &gt; Data Binding tab&#8221; in the <a href="http://blogs.adobe.com/mtg/2009/07/adobe_livecycle_es_next_beta_a.html">Designer ES2 Beta</a>). You can also merge schemas into a single, larger schema (&lt;import/&gt; nodes are supported which can make importing multiple schemas into a single one relatively easy).</p>
<p>The next and final steps are to choose the schema file and root element. Depending on how your schema is defined, you may have a choice of elements to use as the root. In the Movie Rental schema, there is only one root: &#8220;movieRental&#8221;.</p>
<p><img src="http://forms.stefcameron.com/images/SchemaConnection/SchemaConnection-2.jpg"></p>
<p>There are other options that let you embed the schema definition into your form (good if you don&#8217;t want to have to keep the schema file along with your form; otherwise, whenever you edit your form, you will need to ensure that the schema file is available) as well as specify XSLT files for incoming and outgoing data transformations (also a topic for another post).</p>
<h2>Data View Palette</h2>
<p>The initial view of the connection in the palette shows the data structure specified by the schema as represented in XFA. Each node in the schema gets an associated XFA object type depending on its description.</p>
<p><img src="http://forms.stefcameron.com/images/SchemaConnection/SchemaConnection-3.jpg"></p>
<p>For example:</p>
<ul>
<li>Complex types (such as {customer} and {movie}) are usually mapped to subforms since they are elements that contain child elements.
<li>Simple types (such as {customer.firstName} and {customer.prov}) are typically mapped to XFA fields. As you can see, the Schema Data Connection Wizard detected various types and assigned specific XFA fields to each data node: The {customer.prov} node is set to a drop down list because that element is defined as an enumeration of specific values in the schema while {movie.cost} is set to a decimal field because it&#8217;s type is &#8220;xs:decimal&#8221;.</li>
</ul>
<h2>Root Subform Name</h2>
<p>When the connection was created, there was a change to your form&#8217;s root subform name. Note how it has changed to <strong>movieRental</strong>:</p>
<p><img src="http://forms.stefcameron.com/images/SchemaConnection/SchemaConnection-4.jpg"></p>
<p>The name of the root subform is always the name of the root data element in the Data DOM. This ensures that data submitted from the form has the correct root element to match the schema and data merged into the form will be bound first by the root subform, then by child form objects.</p>
<h2>Inserting New Fields</h2>
<p>The Data View palette can be used to insert fields into your form. This makes it really easy to create new fields with the proper settings and bindings. You can customize how fields are created by choosing &#8220;Palette menu button <img style="display: inline" src="http://forms.stefcameron.com/images/SchemaConnection/SchemaConnection-5.jpg"> &gt; Options&#8221;:</p>
<p><img src="http://forms.stefcameron.com/images/SchemaConnection/SchemaConnection-6.jpg"></p>
<ul>
<li><strong>Generate Validation Scripts: </strong>If enabled, field Validate events will be generated for elements that have certain restraints defined in the schema.
<li><strong>Generate Descriptive Information:</strong> If enabled, particular forms of <a href="http://forms.stefcameron.com/2009/08/28/schema-metadata/">metadata in the schema</a> will be inserted into the field definitions in your form (more on this in another post).
<li><strong>Allow Tables to be Generated:</strong> If enabled, tables, rather than plain subforms, will be generated where appropriate.
<li><strong>Show Binding Properties Dialog Box:</strong> If enabled and you drag &amp; drop an element onto an existing field, a customization dialog will be displayed (more on this later).</li>
</ul>
<p>To insert a <em>new</em> field configured according to an element in the schema, just drag &amp; drop the element from the connection tree (in the Data View palette) into your form. You can add an entire group in a single operation by dragging a container element (e.g. {customer}). When a field in your form is bound to an element in the schema, a binding icon is displayed next to the element in the connection tree.</p>
<p><strong><font color="#008000">Start by dragging and dropping the {customer} element into your form.</font></strong> The result should be a new subform named &#8220;customer&#8221; containing 8 fields named according to the schema&#8217;s {customer} element and the elements it contains.</p>
<p><img src="http://forms.stefcameron.com/images/SchemaConnection/SchemaConnection-7.jpg"><br />New fields on the form.</p>
<p><img src="http://forms.stefcameron.com/images/SchemaConnection/SchemaConnection-8.jpg"><br />New structure in the hierarchy.</p>
<p>Notice how each leaf element in the connection now has a binding icon next to it in the connection tree (indicating that fields on the form are bound to those schema elements). The inserted fields were also customized according to their schema element counterparts. For example:</p>
<ul>
<li>All fields have appropriate data bindings (see &#8220;Object palette &gt; Bindings tab &gt; Data Binding property&#8221;).
<li>The {customer.prov} element became a drop down list which has a list of items, as well as a default value of &#8220;ON&#8221;, as specified in the schema.
<li>All new form objects have names that match their schema element names (though these could be altered, if better suited to any scripting requirements you may have, without affecting the data bindings).
<li>All new form objects have captions that are derived from their schema element names (e.g. the field bound to {customer.firstName} has a caption of &#8220;First Name&#8221;).</li>
</ul>
<p><strong><font color="#008000">Next, drag and drop the {movie} element from the connection into your form.</font></strong> This element is special because the schema specifies it may have multiple occurrences:</p>
<pre><code>&lt;xs:element
    name="movie"
    type="movieDef"
    minOccurs="1"
    maxOccurs="unbounded"/&gt;</code></pre>
<p>The result is a <strong>repeatable subform</strong>, as you can see by the Binding properties that were automatically set on the new &#8220;movie&#8221; subform (bound to movie[*] with Min Count set to 1 and no specified Max Count (unbounded)):</p>
<p><img src="http://forms.stefcameron.com/images/SchemaConnection/SchemaConnection-9.jpg"></p>
<p>(Note that if the properties on the &#8220;Object palette &gt; Binding tab&#8221; are partially disabled, it&#8217;s most likely because the movie subform is not inside a <em>flowed</em> container. Though it isn&#8217;t an XFA requirement, Designer requires it in order to enable the repeatable properties. We&#8217;ll fix this shortly.)</p>
<h2>Binding to Existing Fields</h2>
<p>There are two ways to bind an <em>existing</em> field to an element in the schema connection:</p>
<ol>
<li>Simply choose the element to which the field should bind by using the &#8220;Object palette &gt; Binding tab &gt; Data Binding property&#8221; pop-out menu (button to the right-hand side of the property) and selecting it from within the schema connection&#8217;s sub menu.
<li>Drag an element from the connection tree, in the Data View palette, and drop it on top of a field in your form. Doing so will &#8212; if it&#8217;s enabled &#8212; cause the <strong>Binding Properties Dialog Box</strong> to be displayed, giving you some options to further customize the existing field with information from the schema.</li>
</ol>
<h3><strong>Binding Properties Dialog Box</strong></h3>
<p><strong><font color="#008000">Add a new text field to your form.</font></strong> This will be for the {returnBy} schema element. Ensure that the &#8220;Show Binding Properties Dialog Box&#8221; option is checked in the &#8220;Data View palette &gt; Palette menu button &gt; Options&#8221; dialog. Now drag the {returnBy} element from the connection and drop it on top of the new text field.</p>
<p><img src="http://forms.stefcameron.com/images/SchemaConnection/SchemaConnection-10.jpg"></p>
<p>The above dialog will be displayed, giving you the option to update the following properties of the field on which the schema element was dropped:</p>
<ul>
<li><strong>Name:</strong> Updates the name of the field to match the name of the schema element (from &#8220;TextField1&#8243; to &#8220;returnBy&#8221;).
<li><strong>Caption:</strong> Updates the caption of the field to one derived from the schema (from &#8220;Text Field&#8221; to &#8220;Return By&#8221;).
<li><strong>Type:</strong> Updates the field&#8217;s object type to match the kind of data represented by the schema element (from text field to date/time field).
<li><strong>Type &gt; Default Value:</strong> Updates the field&#8217;s default value to that specified in the schema, if any (in the case of {returnBy}, the schema does not specify a default value).
<li><strong>Type &gt; Data Pattern:</strong> Updates the field&#8217;s patterns (display, edit, validate, data) according to the schema (in the case of {returnBy}, no pattern restrictions are specified in the schema, though a &#8220;date{YYYY-MM-DD}&#8221; data pattern will be set by default).</li>
</ul>
<p>Choose the &#8220;Update all related properties&#8221; option and click on the &#8220;OK&#8221; button. The text field you previously inserted will be updated to a date/time field with various properties matching the {returnBy} schema element&#8217;s specifications (as detailed above).</p>
<h2>Finishing Touches</h2>
<p>At this point, your form should look something like this (the positioning of the objects may differ depending on where you dropped them):</p>
<p><img src="http://forms.stefcameron.com/images/SchemaConnection/SchemaConnection-11.jpg"></p>
<p>Here are a few steps you can take to make things look a little nicer (in my opinion) as well as enable instances of the movie subform to be added for new movies to rent:</p>
<ol>
<li>Place the movie subform in a flowed subform so that <a href="http://forms.stefcameron.com/2006/11/11/instance-manager-object-reference/">multiple instances</a> can be added, flowed one after the other.
<li>Place an &#8220;add&#8221; button next to the movie subform to add instances as well as a &#8220;remove&#8221; button inside the movie subform to remove specific instances.
<li>Add an unbound decimal field that shows the running total cost for the movie rental order.
<li>Add script to the returnBy field to automatically set the date 30 days in the future.
<li>Add a submit button to submit the order.</li>
</ol>
<p>Following the steps above should yield a more functional movie rental form. The only thing missing is the ability to pull-in movies to choose from. For one thing, this would avoid having to specify the movie ID, title and cost when choosing a movie to rent. To accomplish this, you could connect the form to a movie <a href="http://forms.stefcameron.com/2006/10/12/displaying-all-records-from-an-odbc-data-connection/">database</a> (note that the database in the linked tutorial doesn&#8217;t have a &#8220;cost&#8221; column associated to a movie so you&#8217;ll have to add one) or, the <strong>more secure and recommended way</strong>, to a movie <a href="http://forms.stefcameron.com/2007/05/21/connecting-to-a-web-service/">web service</a>.</p>
<p><a href="http://forms.stefcameron.com/samples/data-binding/schema/ConnectingToSchema.pdf">Download Sample Form [pdf]</a></p>
<p><a href="http://forms.stefcameron.com/samples/data-binding/schema/movieRental.xsd">Download Schema [xsd]</a></p>
<p><a href="http://forms.stefcameron.com/samples/data-binding/schema/movieRental-Sample.xml">Download Sample Data [xml]</a></p>
<p><strong>Minimum Requirements:</strong> I designed this form using Designer ES2 Beta and Acrobat 9.1 however the principals should work all the way back to Designer 7.1 and Acrobat 7.0.5. There may be a few features lacking from the Data View palette if you go back that far but the concept of connecting the form to a schema remains the same.</p>
<p class="postUpdate"><strong>Aug 28, 2009</strong> &mdash; Added link to <a href="http://forms.stefcameron.com/2009/08/28/schema-metadata/">Schema Metadata</a> tutorial.</p>
<p class="postUpdate"><strong>Nov 4, 2009</strong> &mdash; Fixed a couple of syntax errors in the sample PDF form that were preventing the addition and removal of movie subform instances.</p>
]]></content:encoded>
			<wfw:commentRss>http://forms.stefcameron.com/2009/08/03/connecting-to-a-schema/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Expandable Table with Totals</title>
		<link>http://forms.stefcameron.com/2009/02/25/expandable-table-with-totals/</link>
		<comments>http://forms.stefcameron.com/2009/02/25/expandable-table-with-totals/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 12:00:27 +0000</pubDate>
		<dc:creator>Stefan Cameron</dc:creator>
				<category><![CDATA[Data Binding]]></category>
		<category><![CDATA[Designer]]></category>
		<category><![CDATA[FormCalc]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Tables]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[XFA]]></category>

		<guid isPermaLink="false">http://forms.stefcameron.com/2009/02/25/expandable-table-with-totals/</guid>
		<description><![CDATA[It seems that calculating totals in an expandable table with a dynamic/repeatable row is a hot topic lately so I thought I would post a tutorial on how to build a simple table which represents a purchase order where you can add and remove line items and various totals are automatically calculated. Before we get [...]]]></description>
			<content:encoded><![CDATA[<p>It seems that calculating totals in an expandable table with a dynamic/repeatable row is a hot topic lately so I thought I would post a tutorial on how to build a <strong>simple</strong> table which represents a purchase order where you can add and remove line items and various totals are automatically calculated.</p>
<p>Before we get started, you&#8217;ll need Designer 7.1 and Acrobat/Reader 7.0.5 at minimum to run through this tutorial. <strong>I will be using Designer 8.2</strong> and Acrobat/Reader 9.0 to do the tutorial so it&#8217;s possible some of the UI may have changed slightly since the 7.1/7.05 versions.</p>
<p><span id="more-325"></span><br />
<h2>Creating the Table</h2>
<p>The first step is to drag a Table object from the Object Library palette&#8217;s Standard tab into your form. When you drop it, the <em>Insert Table</em> wizard appears. Set it to include 5 columns, 1 body row and header and footer rows:</p>
<p><img src="http://forms.stefcameron.com/images/ExpandableTable/ExpandableTable-1.jpg"> </p>
<h2>Headers and Footers</h2>
<p>Double-click on the first &#8220;Header&#8221; cell and set the text/caption to &#8220;#&#8221;. This will be the dynamic index column. Set the remaining headers as follows: &#8220;Item&#8221;, &#8220;Quantity&#8221;, &#8220;Cost&#8221;, &#8220;Total&#8221;.</p>
<p>In the footer, we only need two cells: One with a caption that reads, &#8220;Grant Total:&#8221;, and the other for the calculated grand total. To start, select the first 4 footer cells by Ctrl-Clicking each one:</p>
<p><img src="http://forms.stefcameron.com/images/ExpandableTable/ExpandableTable-2.jpg"> </p>
<p>Then, with the cells selected, click on the &#8220;Merge Table Cells&#8221; button in the Object palette (highlighted in yellow in the image below):</p>
<p><img src="http://forms.stefcameron.com/images/ExpandableTable/ExpandableTable-3.jpg"> </p>
<p>The 4 cells will merge into one and you can then double-click the text to change it to &#8220;Grand Total:&#8221;. While you&#8217;re editing the text, use the Paragraph palette to align it to the right.</p>
<p>We&#8217;re pretty much done with the header and footer row except for the grand total cell but we&#8217;ll come back to that after the body row has been defined.</p>
<h2>Body Row</h2>
<p>What we want for the body row is a text/read-only cell for the row number, a drop down list for selecting items, a numeric cell for the quantity, a numeric/read-only cell for the cost (which will be set depending on the selection in the item list) and a numeric/read-only cell for the calculated total (quantity * cost) for the row.</p>
<p>The first thing to do is to rename the &#8220;Row1&#8243; body row to &#8220;Item&#8221; and set it to repeat for each data item by selecting Row1 from the Hierarchy palette and setting its &#8220;Object palette &gt; Binding tab &gt; Name property&#8221; to &#8220;Item&#8221;, its &#8220;Default Binding&#8221; property to &#8220;Normal&#8221; and checking its &#8220;Repeat Row for Each Data Item&#8221; property to enable it to repeat so that the table can expand.</p>
<h3>Row Number Cell</h3>
<p>While this may sound complex, it&#8217;s actually quite easy to achieve. Since the Item body row will repeat, it will have an index and the index will be zero-based. This means our row number is simply the index of the cell&#8217;s parent (Item body row) + 1.</p>
<p>Drop a text field from the Object palette onto the first cell in the Item row. This will change the cell&#8217;s type to a text field. Set its name to &#8220;ItemIndex&#8221; (not just &#8220;index&#8221; because it&#8217;ll cause conflicts with the Item row&#8217;s index property), its &#8220;Object palette &gt; Binding tab &gt; Default Binding property&#8221; to &#8220;None&#8221; (since we don&#8217;t need the index in the submitted data &#8212; it&#8217;s just for show, after all) and its &#8220;Object palette &gt; Value tab &gt; Type property&#8221; to &#8220;Calculated &#8211; Read-Only&#8221;.</p>
<p>Finally, using the Script Editor, set its Calculate event to the following JavaScript code:</p>
<pre><code>(this.parent.index + 1) + ".";</code></pre>
<p>If we preview the form at this point, we&#8217;ll see that the first and only Item row has a row number of &#8220;1.&#8221;:</p>
<p><img src="http://forms.stefcameron.com/images/ExpandableTable/ExpandableTable-4.jpg"> </p>
<h3>Item Name and Cost Cells</h3>
<p>The next cell is the item name. This will be a drop down list with a few items to choose from which have associated values that we&#8217;ll use to set the cost field depending on the selected item.</p>
<p>Drop a down down list from the Object Library onto the second cell in the Item row. Set its name to &#8220;ItemName&#8221; (not just &#8220;name&#8221; because it&#8217;ll cause conflicts with the Item row&#8217;s name property) and add some items to it using the Object palette&#8217;s Field tab. The following image shows that I&#8217;ve populated my list with 3 items:</p>
<p><img src="http://forms.stefcameron.com/images/ExpandableTable/ExpandableTable-5.jpg"> </p>
<blockquote><p>An important thing to note here is that we did <strong>not</strong> associate values as costs to each item. It would&#8217;ve been convenient to store them like that however if we had, the item&#8217;s name would&#8217;ve shown-up as the cost in the submitted data which is not the intention. When data is submitted, we want the item&#8217;s name (e.g. &#8220;Helmet&#8221;) to show-up as the ItemName data.</p></blockquote>
<p>With the ItemName cell defined, we need to setup the Cost cell so that we can set its value to the value associated with the selected item.</p>
<p>Drop a numeric field from the Object Library onto the <strong>4th cell</strong> (the 3rd cell is for quantity) in the Item row. Set its name to &#8220;Cost&#8221;, its &#8220;Object Palette &gt; Binding tab &gt; Data Format property&#8221; to &#8220;Float&#8221;, its Value Type to &#8220;Read-Only&#8221; and its Display Pattern to &#8220;num{($zzzz9.99)}&#8221; (without the double-quotes).</p>
<p>Now set the following JavaScript code as the Change event for the <strong>ItemName</strong> drop down list:</p>
<pre><code>switch (xfa.event.newText)
{
    case "Helmet":
        Cost.rawValue = 45.00;
        break;

    case "Pedals":
        Cost.rawValue = 150.00;
        break;

    case "Wheel":
        Cost.rawValue = 225.00;
        break;

    default:
        // unknown item
        Cost.rawValue = 0;
        break;
}</code></pre>
<p>This simple script looks at the new selection in the ItemName list and sets the Cost field&#8217;s value accordingly.</p>
<p>At this point, we preview the form and choose &#8220;Pedals&#8221; as the item, the Cost cell will get a value of &#8220;150&#8243; which will be displayed as &#8220;$150.00&#8243; because of the display pattern we set on the Cost field earlier:</p>
<p><img src="http://forms.stefcameron.com/images/ExpandableTable/ExpandableTable-6.jpg"> </p>
<h3>Quantity Cell</h3>
<p>With the ItemName and Cost cells defined, we can now define the Quantity cell. This one is quite simple: Drop a numeric field onto the 3rd cell in the Item row. Set its name to &#8220;Quantity&#8221; and its default value to 1.</p>
<h3>Total Cell</h3>
<p>The last cell in the Item row is the one what will show the total for that Item which is &#8220;Quantity * Cost&#8221;. This is another easy one: Drop a numeric field onto the 5th cell in the Item row. Set its name to &#8220;Total&#8221;, its Default Binding to &#8220;None&#8221; (no need to store calculated values in the data), its Value Type to &#8220;Calculated &#8211; Read-Only&#8221;, its Display Pattern to &#8220;num{($zzzz9.99)}&#8221; (without the double-quotes) and its Calculate event to the following <strong>FormCalc</strong> code:</p>
<pre><code>Quantity * Cost</code></pre>
<p>That&#8217;s it! FormCalc is great for spreadsheet-like calculations and in FormCalc, the &#8220;rawValue&#8221; property is implied so you don&#8217;t need to specify it like in JavaScript.</p>
<p>At this point, previewing our form and choosing &#8220;Pedals&#8221; as the item shows a calculated total of &#8220;$150.00&#8243; for that item:</p>
<p><img src="http://forms.stefcameron.com/images/ExpandableTable/ExpandableTable-7.jpg"> </p>
<h2>Footer Grand Total Cell</h2>
<p>Now that the Item body row is complete, we just have a couple of things left to do. First, we need to setup the Grand Total field in the footer row which will display the grand total of the order. This field will be a numeric/read-only field which will calculate the sum of all Total fields in all Item rows that exist. This may sound complicated but once again, FormCalc makes this easy-as-pie.</p>
<p>Drop a numeric field onto the 5th (or 2nd or last) cell in the footer row. Set its name to &#8220;GrandTotal&#8221;, its Default Binding to &#8220;None&#8221; (no need to store calculated values in the data), its Value Type to &#8220;Calculated &#8211; Read-Only&#8221;, its Display Pattern to &#8220;num{($zzzz9.99)}&#8221; (without the double-quotes) and its Calculate event to the following <strong>FormCalc</strong> code:</p>
<pre><code>Sum(Item[*].Total)</code></pre>
<p>You&#8217;re probably wondering what the &#8220;[*]&#8221; notation means. Put simply, it&#8217;s like a wildcard for the index of the Item row and it tells the Sum function to get the value of the Total field in <strong>all instances </strong>of the Item row and calculate their sum.</p>
<p>At this point, if we preview our form and choose &#8220;Pedals&#8221; as the item, the total and grand total values are automatically calculated:</p>
<p><img src="http://forms.stefcameron.com/images/ExpandableTable/ExpandableTable-8.jpg"> </p>
<h2>Adding/Removing Items</h2>
<p>Last but not least, we need a mechanism to add and remove items in the table. Since we had the Item row repeatable, this will be done using the <a href="http://forms.stefcameron.com/2006/11/11/instance-manager-object-reference/">Instance Manager</a> associated with the Item row.</p>
<p>To start, place two buttons on the form and name them &#8220;Add&#8221; and &#8220;Remove&#8221;. Set their captions to &#8220;Add&#8221; and &#8220;Remove&#8221; as well.</p>
<p>Using the Script Editor palette, set the Add button&#8217;s Click event to the following JavaScript code:</p>
<pre><code>Table1._Item.addInstance(0);</code></pre>
<p>This script will add a new instance of the Item row to the table. Now set the Remove button&#8217;s Click event to the following JavaScript code:</p>
<pre><code>if (Table1._Item.count &gt; 1)
    Table1._Item.removeInstance(Table1._Item.count - 1);</code></pre>
<p>This script will first make sure that there are at least 2 rows (since the minimum row count is 1) and if so, will remove the last row that was added to the table. Note the use of the <a href="http://forms.stefcameron.com/2006/05/18/instantiating-subforms-at-runtime/">underscore prefix</a> to access the Item row&#8217;s Instance Manager.</p>
<p>Previewing the form at this point will now allow you to add/remove rows and you&#8217;ll see that the totals are all being calculated automatically as you choose items and set quantities:</p>
<p><img src="http://forms.stefcameron.com/images/ExpandableTable/ExpandableTable-9.jpg"> </p>
<h2>Sample Form</h2>
<p>Here is the sample form for the tutorial. It&#8217;s the solution so try not to look at it until you&#8217;ve run through the tutorial on your own&#8230;</p>
<p><a href="http://forms.stefcameron.com/samples/tables/ExpandableTable.pdf">Download Sample [pdf]</a></p>
<p><strong>Sample Minimum Requirements:</strong> Designer 7.1/Acrobat 7.0.5</p>
<h2><a name="pageTotals"></a>Sample Form with Page Totals</h2>
<p>Reader Sandeep Goli asked an <a href="#comment-56711">interesting question</a>: How would one show a page total?</p>
<p>This can be done by adding first allowing the table&#8217;s content to break across pages by checking the &#8220;Object palette > Table tab > &#8216;Allow Page Breaks within Content&#8217; property&#8221;. The next step is to add another footer row to the table, located prior to the existing &#8220;FooterRow&#8221; row object (select the FooterRow row, duplicate it, rename it to &#8220;PageFooter&#8221;, rename the field inside to &#8220;PageTotal&#8221; and drag it up above the FooterRow object in the Hierarchy palette), and set it up to be included on subsequent pages as well as a the final page (rather than just the final page) using the &#8220;Object palette > Pagination tab > &#8216;Include Footer Row in Subsequent Pages&#8217; and &#8216;Include Footer Row in Final Page&#8217; properties&#8221;. This will cause the footer row to appear at the bottom of all pages, including the last page, as the table breaks across pages.</p>
<p>The final step in making this work is to set script in the new footer row&#8217;s Layout:Ready event. (If you duplicated the FooterRow, make sure you remove its Calculate script.) In this event, you can use APIs from the Layout Model which will let you determine the set of table &#8220;Item&#8221; rows that are on a given page &#8212; that being the page on which the PageTotal field was rendered:</p>
<pre><code>xfa.layout.page(object)</code></pre>
<p>will return the 1-based page number on which that object (field, subform, etc.) was rendered and</p>
<pre><code>xfa.layout.pageContent(zeroBasedPageIndex, type, 0)</code></pre>
<p>will return a list of all form objects of the specified type (a string, such as &#8216;subform&#8217;, &#8216;field&#8217;, &#8216;draw&#8217;, etc.) on the specified 0-based page index.</p>
<p>The following script finds all Item rows on the same page as the rendered PageTotal field, sums their Total child field values and sets that number as the PageTotal&#8217;s value:</p>
<pre><code>// get a list of all subforms rendered on the same page as this field
var pageSFList = xfa.layout.pageContent(xfa.layout.page(this) - 1, "subform", false);
var pageTotal = 0;
// for each subform named "Item", sum its "Total" field value
for (var i = 0; i &lt; pageSFList.length; i++)
{
    var sf = pageSFList.item(i);
    if (sf.name == "Item")
        pageTotal += sf.Total.rawValue;
}
// set this field's value to the sum of all Item rows on this page
this.rawValue = pageTotal;</code></pre>
<p><a href="http://forms.stefcameron.com/samples/tables/ExpandableTable-PageSum.pdf">Download Sample [pdf]</a><br /><a href="http://forms.stefcameron.com/samples/tables/ExpandableTable-PageSum-Data.xml">Download Sample Data [xml]</a></p>
<p><strong>Sample Minimum Requirements:</strong> Designer 8.0/Acrobat 8.0. This is due to bug fixes with table footer placement which are available only as of Acrobat 8.0.</p>
<h2><a name="removeThisRow"></a>Sample Form with Row Remove Button</h2>
<p>Reader Susila asked an <a href="#comment-60114">interesting question</a>: How do you remove a specific row using a &#8220;remove&#8221; button in the row to be removed?</p>
<p>This is done simply by inserting a column to the left of the ItemIndex column in the table and setting the cell&#8217;s type to a button. The button&#8217;s Click event is then set to remove the row to which it belongs.</p>
<p>The trick lies in writing the script in such a way that it automatically relates to (i.e. removes) the row instance to which it belongs. This is achieved by retrieving the instance index of the row to which it belongs and using that as the parameter supplied to the Item row <a href="http://forms.stefcameron.com/2006/11/11/instance-manager-object-reference/">Instance Manager</a>&#8216;s removeInstance([index]) method. The method expects a zero-based index and that&#8217;s what the index property gives you:</p>
<pre><code>// JavaScript:
this.parent.parent._Item.removeInstance(this.parent.index);</code></pre>
<p>The script above first accesses the button&#8217;s grand-parent (Table1). Then it gets the Instance Manager for the Item row and executes its removeInstance() method providing it with the index property value of its (the button&#8217;s) parent (Item row instance). By accessing parent objects, you ensure that you&#8217;re doing things with relation to the button that was clicked and, therefore, the row that contains the button.</p>
<p>Note that simply removing the row won&#8217;t necessarily update the ItemIndex field in other row instances that remain. If your form&#8217;s <a href="http://forms.stefcameron.com/2009/02/11/careful-with-form-compatibility-settings/">Target Version</a> is set to Acrobat/Reader 7.0.5, you will have to explicitly tell the form to <a href="http://forms.stefcameron.com/2006/05/20/add-recalculate/">re-execute calculations</a>. This issue was addressed in Acrobat 8.0+ so there&#8217;s no need to do this if your form targets a later version of Acrobat/Reader.</p>
<p><a href="http://forms.stefcameron.com/samples/tables/ExpandableTable-RemThisRow.pdf">Download Sample [pdf]</a></p>
<p><strong>Sample Minimum Requirements:</strong> Designer 7.1/Acrobat 7.0.5</p>
<h2>Further Improvements</h2>
<p>As I mentioned earlier, this is a <strong>simple </strong>dynamic table with a repeatable row. It demonstrates only the basics. You could enhance it further by including row shading options, validations, better formatting (font and paragraph), smarter add/remove buttons (e.g. put the remove button on the Item row in order to delete that specific row instead of always deleting from the end), put an insert button on the Item row in order to insert new items instead of always appending to the end of the list, etc.</p>
<p>For an advance sample, you should check-out the &#8220;Dynamic Interactive Purchase Order&#8221; sample that ships with Designer. Depending on how Designer was installed (either with Acrobat, with LiveCycle or standalone) and whether it was on Win 2k, XP or Vista, you should be able to find it in one of the following folders:</p>
<p align="left"><strong>C:\Program Files\Adobe\LiveCycle ES\Workbench ES\Designer ES\</strong><em>{version}</em>\EN\Samples\Forms\Purchase Order\Dynamic Interactive\Forms</p>
<p align="left"><strong>C:\Program Files\Adobe\LiveCycle Designer ES\</strong><em>{version}</em>\EN\Samples\Forms\Purchase Order\Dynamic Interactive\Forms</p>
<p align="left"><strong>C:\Program Files\Adobe\Acrobat\Designer\</strong><em>{version}</em>\EN\Samples\Forms\Purchase Order\Dynamic Interactive\Forms</p>
<p align="left">(Note that in Vista, &#8220;Program Files&#8221; changes to &#8220;Program Files (x86)&#8221;.)</p>
<p class="postUpdate"><strong>Updated Oct 2, 2009</strong> &#8212; Added <a href="#pageTotals">page totals</a> sample.</p>
<p class="postUpdate"><strong>Updated Oct 2, 2009</strong> &#8212; Added <a href="#removeThisRow">row remove button</a> sample.</p>
]]></content:encoded>
			<wfw:commentRss>http://forms.stefcameron.com/2009/02/25/expandable-table-with-totals/feed/</wfw:commentRss>
		<slash:comments>101</slash:comments>
		</item>
		<item>
		<title>Adobe LiveCycle ES Update 1 Documentation</title>
		<link>http://forms.stefcameron.com/2008/07/21/adobe-livecycle-es-update-1-documentation/</link>
		<comments>http://forms.stefcameron.com/2008/07/21/adobe-livecycle-es-update-1-documentation/#comments</comments>
		<pubDate>Mon, 21 Jul 2008 14:32:22 +0000</pubDate>
		<dc:creator>Stefan Cameron</dc:creator>
				<category><![CDATA[Designer]]></category>
		<category><![CDATA[FormCalc]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://forms.stefcameron.com/2008/07/21/adobe-livecycle-es-update-1-documentation/</guid>
		<description><![CDATA[Since the release of Designer ES 8.2 and now LiveCycle ES Update 1, the documentation page has been updated with all sorts of goodies related to Update 1, including the following Designer-related documentation: Scripting Reference (technical document about scripting your forms) Target Version Reference (if you want to know what features are compatible with specific [...]]]></description>
			<content:encoded><![CDATA[<p>Since the release of Designer ES 8.2 and now <a href="http://www.adobe.com/go/learn_lc_whatsNew_82">LiveCycle ES Update 1</a>, the <a href="http://www.adobe.com/support/documentation/en/livecycle/">documentation page</a> has been updated with all sorts of goodies related to Update 1, including the following Designer-related documentation:</p>
<ul>
<li><a href="http://www.adobe.com/go/learn_lc_scriptingReference_82">Scripting Reference</a> (technical document about scripting your forms)</li>
<li><a href="http://www.adobe.com/go/learn_lc_target_82">Target Version Reference</a> (if you want to know what features are compatible with specific PDF versions or if you&#8217;re using Designer ES 8.2 to build a form for users on Acrobat/Reader 7.x and you want to <a href="http://forms.stefcameron.com/2008/04/18/target-version-reference/">make sure they can use your form</a>)</li>
<li><a href="http://www.adobe.com/go/learn_lc_designerSyntaxChecking_82">Scripting Errors Reference</a> (solutions to common scripting errors)</li>
<li><a href="http://www.adobe.com/go/learn_lc_formCalc_82">FormCalc Reference</a> (for those of you who like to use FormCalc instead of JavaScript, this reference includes previously-omitted information about various language constructs such as &#8220;while&#8221;, &#8220;for&#8221; and &#8220;for each&#8221; loops)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://forms.stefcameron.com/2008/07/21/adobe-livecycle-es-update-1-documentation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LC Designer ES Documentation Now Online</title>
		<link>http://forms.stefcameron.com/2007/06/25/lc-designer-es-documentation-now-online/</link>
		<comments>http://forms.stefcameron.com/2007/06/25/lc-designer-es-documentation-now-online/#comments</comments>
		<pubDate>Mon, 25 Jun 2007 19:13:36 +0000</pubDate>
		<dc:creator>Stefan Cameron</dc:creator>
				<category><![CDATA[Designer]]></category>
		<category><![CDATA[FormCalc]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://forms.stefcameron.com/2007/06/25/lc-designer-es-documentation-now-online/</guid>
		<description><![CDATA[Adobe recently published a whole collection of documents for Designer. In particular, Designer documentation is now available online via LiveDocs. Under the Develop Tab, you&#8217;ll find documentation on the following topics as they pertain to Designer: Designer Help (LiveDocs) Form Guides (&#8220;getting started&#8221; guide) Scripting Basics Scripting Reference Target Version Reference (to know what features [...]]]></description>
			<content:encoded><![CDATA[<p>Adobe recently published a <a href="http://www.adobe.com/support/documentation/en/livecycle/es/">whole collection of documents</a> for <a href="http://www.adobe.com/products/livecycle/designer/">Designer</a>. In particular, Designer documentation is now <a href="http://www.adobe.com/go/learn_lc_designer">available online via <strong>LiveDocs</strong></a>.</p>
<p>Under the <strong>Develop Tab</strong>, you&#8217;ll find documentation on the following topics as they pertain to Designer:</p>
<ul>
<li>Designer Help (LiveDocs)</li>
<li><strong>Form Guides</strong> (&#8220;getting started&#8221; guide)</li>
<li>Scripting Basics</li>
<li>Scripting Reference</li>
<li><strong>Target Version</strong> Reference (to know what features of XFA and Designer are supported in specific versions of Acrobat &#8212; this is what the new <strong>Target Validation</strong> feature in Designer is intended to help you manage)</li>
<li>Transformation Reference (to know what&#8217;s supported if you&#8217;re targeting HTML)</li>
<li>FormCalc Reference</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://forms.stefcameron.com/2007/06/25/lc-designer-es-documentation-now-online/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adobe Unveils LiveCycle ES</title>
		<link>http://forms.stefcameron.com/2007/06/04/adobe-unveils-livecycle-es/</link>
		<comments>http://forms.stefcameron.com/2007/06/04/adobe-unveils-livecycle-es/#comments</comments>
		<pubDate>Mon, 04 Jun 2007 17:29:51 +0000</pubDate>
		<dc:creator>Stefan Cameron</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Designer]]></category>
		<category><![CDATA[FormCalc]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://forms.stefcameron.com/2007/06/04/adobe-unveils-livecycle-es/</guid>
		<description><![CDATA[Today, Adobe unveiled its new LiveCycle Enterprise Suite. It includes a whole slew of new product versions all designed to work together in an IDE (Integrated Development Environment), bringing PDF and Flash together in the same workflow using a new technology called Form Guides (see the Generate dynamic Adobe Flash&#174; Player compatible form guides topic [...]]]></description>
			<content:encoded><![CDATA[<p>Today, <a href="http://www.adobe.com">Adobe</a> unveiled its new <a href="http://www.adobe.com/products/livecycle/">LiveCycle Enterprise Suite</a>. It includes a whole slew of new product versions all designed to work together in an IDE (Integrated Development Environment), bringing PDF and Flash together in the same workflow using a new technology called <strong>Form Guides</strong> (see the <a href="http://www.adobe.com/products/livecycle/forms/features.html">Generate dynamic Adobe Flash&reg; Player compatible form guides</a> topic in the <a href="http://www.adobe.com/products/livecycle/forms/">LC Forms ES</a> feature list). Form Guides enable you to add an RIA (Rich Internet Application) view to your  form to engage your customers in a more compelling way.</p>
<p>What&#8217;s more is that the new Enterprise Suite launch means that there&#8217;s also <strong>new version of Designer</strong>: <a href="http://www.adobe.com/products/livecycle/designer/">LiveCycle Designer ES</a> (version 8.1). I <em>believe</em> this will be an upgrade to Designer 8.0 which currently ships with <a href="http://www.adobe.com/products/acrobatpro/">Acrobat Pro</a> 8.0. Most notable in this new version are</p>
<ul>
<li>the new <strong>Guide Builder tool </strong>which will help you quickly re-purpose your PDF form as a <strong>Flash-based</strong> <strong>Form Guide</strong>;</li>
<li>a new feature called <strong>Form Fragments</strong> which will enable you to <strong>re-use sections of your forms and scripts </strong>in other forms while only needing to <strong>make changes in one place</strong>;</li>
<li>a <strong>Syntax Checker</strong> in the Script Editor to check for errors in your scripts (this will definitely help with debugging!); and</li>
<li>the <strong>Target Validation</strong> feature which will help you determine which XFA features you may use when targeting a specific version of Reader/Acrobat (e.g. if you specify Reader/Acrobat 7.0 as your <em>target version</em> and you insert a table into your form, you&#8217;ll get a warning telling you that tables aren&#8217;t supported in Reader/Acrobat 7.0 &#8212; they&#8217;re only supported as of Reader/Acrobat 7.0.5).</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://forms.stefcameron.com/2007/06/04/adobe-unveils-livecycle-es/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FormCalc Expressions (Foreach)</title>
		<link>http://forms.stefcameron.com/2006/10/07/formcalc-expressions-foreach/</link>
		<comments>http://forms.stefcameron.com/2006/10/07/formcalc-expressions-foreach/#comments</comments>
		<pubDate>Sat, 07 Oct 2006 23:25:13 +0000</pubDate>
		<dc:creator>Stefan Cameron</dc:creator>
				<category><![CDATA[FormCalc]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://forms.stefcameron.com/blog/?p=47</guid>
		<description><![CDATA[A couple of weeks ago, I started a series of posts on FormCalc Expressions. The first ones I convered were the If and For expressions. This time, I thought I would explain the Foreach expression. Foreach Expression The Foreach expression is quite similar to the For expression but is meant to be used to achieve [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of weeks ago, I started a series of posts on FormCalc Expressions. The first ones I convered were the <a href="http://forms.stefcameron.com/2006/09/14/formcalc-expressions-if-and-for/">If and For expressions</a>. This time, I thought I would explain the Foreach expression.</p>
<h2>Foreach Expression</h2>
<p>The Foreach expression is quite similar to the For expression but is meant to be used to achieve a slightly different kind of iteration that may be better suited to your needs rather than just increasing/decreasing the value of a counter (as for a regular For expression). The difference is that the Foreach expression assigns a different value taken from an argument list (list of objects/values) to a variable for each iteration of the loop (and there&#8217;s one iteration for each argument in the list).</p>
<h3>FormCalc Syntax</h3>
<pre><code>foreach Variable in (ArgumentList) do
  ExpressionList
endfor</code></pre>
<p>In this syntax, there&#8217;s an <em>ArgumentList</em>. This is simply a comma-separated list of objects/values to assign to the <em>Variable</em> on each iteration.</p>
<p>Based on the syntax, you can do things like set a <em>total</em> field&#8217;s value to the sum of <em>subTotal</em>, <em>shipping</em> and <em>taxes</em> fields:</p>
<pre><code>foreach val (subTotal, shipping, taxes) do
  total = total + val
endfor</code></pre>
<p>In this example, each time the expression loops, the variable <em>val</em> assumes the &#8220;identity&#8221; of a different object in the argument list: First, the subTotal field, then the shipping field and, finally, the taxes field.</p>
<h3>JavaScript Syntax</h3>
<p>For comparison, here is the equivalent JavaScript examples (note the semicolons and curly braces):</p>
<pre><code>var arr = {0: subTotal, 1: shipping, 2: taxes};

for (key in arr)
{
  total.rawValue += arr[key].rawValue;
}</code></pre>
<p>(In this case, the curly braces are optional but I think this is better style.) It&#8217;s certainly more cumbersome in JavaScript using the For&#8230;In expression (which is the closest equivalent to FormCalc&#8217;s Foreach expression I could find) because it&#8217;s only meant for iterating through an object&#8217;s properties or an array&#8217;s keys (which is what my example is doing).</p>
]]></content:encoded>
			<wfw:commentRss>http://forms.stefcameron.com/2006/10/07/formcalc-expressions-foreach/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FormCalc Expressions (If and For)</title>
		<link>http://forms.stefcameron.com/2006/09/14/formcalc-expressions-if-and-for/</link>
		<comments>http://forms.stefcameron.com/2006/09/14/formcalc-expressions-if-and-for/#comments</comments>
		<pubDate>Thu, 14 Sep 2006 17:39:53 +0000</pubDate>
		<dc:creator>Stefan Cameron</dc:creator>
				<category><![CDATA[FormCalc]]></category>

		<guid isPermaLink="false">http://forms.stefcameron.com/blog/?p=40</guid>
		<description><![CDATA[In Designer, you can write scripts using one of two languages: FormCalc and JavaScript. If you&#8217;re like me, you&#8217;re more familiar with JavaScript because it&#8217;s very similar to Java and JScript syntax but when it comes to FormCalc, you&#8217;re lost. Well, not completely but you find it very difficult to find documentation on the syntax [...]]]></description>
			<content:encoded><![CDATA[<p>In Designer, you can write scripts using one of two languages: FormCalc and JavaScript. If you&#8217;re like me, you&#8217;re more familiar with JavaScript because it&#8217;s very similar to Java and JScript syntax but when it comes to FormCalc, you&#8217;re lost. Well, not completely but you find it very difficult to find documentation on the syntax for loops (like While, For, etc.) which are sometimes essential. Maybe you fall-back on JavaScript in those times but, on average, <strong>FormCalc scripts execute much faster</strong> than JavaScript scripts so why not harness that performance in our forms?</p>
<p>In this post, I&#8217;ll explain the syntax of the <strong>If</strong> and <strong>For</strong> expressions in FormCalc. Those of you familiar with Visual Basic or <span class="caps">VBS</span>cript will likely find this more familiar than the equivalent JavaScript syntax (while I&#8217;ll include for comparison however this isn&#8217;t a post on JavaScript syntax). In future posts, I&#8217;ll add to this collection by discussing the syntax for <strong>While</strong> and <strong>ForEach</strong> loops.</p>
<h2>Conventions</h2>
<p>Before we get started, I should explain a few things about the various syntax blocks you&#8217;ll find further down in this post (note that this is simplified for the purposes of this article):</p>
<ul>
<li><em>SimpleExpression</em>: A simple expression like a comparison &#8220;i &gt;= 0&#8221; that evaluates to true or false or a single entity like &#8220;MyVar&#8221; (a defined variable) or &#8220;5&#8221; (a number), for example.</li>
<li><em>ExpressionList</em>: A set of 1 or more expressions, each on a separate line.</li>
<li><em>Square Brackets</em>: A series of keywords (like &#8220;if&#8221;, &#8220;then&#8221;, &#8220;for&#8221;, &#8220;while&#8221;, etc.), SimpleExpressions and ExpressionLists grouped within square brackets denote an optional part of the syntax.</li>
<li><em>Asterisk Character</em>: Sometimes a closing square bracket is followed by an asterisk (*). This indicates that the optional syntax may be used 0 or more times.</li>
<li><em>Question Mark Character</em>: Sometimes a closing square bracket is followed by a question mark (?). This indicates that the optional syntax must be used 1 or more times.</li>
</ul>
<h2>Variables</h2>
<p>I assume you&#8217;re probably familiar with declaring variables in scripts but just in case, in FormCalc, you declare variables like this:</p>
<pre><code>var MyVar</code></pre>
<p>and you can even declare it and assign it a value in the same step:</p>
<pre><code>var MyVar = 5</code></pre>
<h2>If Expression</h2>
<p>Let&#8217;s look at one of the most essential of all expressions:</p>
<h3>FormCalc Syntax</h3>
<pre><code>if ( SimpleExpression ) then
    ExpressionList
[ elseif ( SimpleExpression ) then
    ExpressionList]*
[ else
    ExpressionList]?
endif</code></pre>
<p>Based on the syntax, you can do things like (assuming you&#8217;ve declared a variable named &#8220;i&#8221;):</p>
<pre><code>if (i &gt;= 10) then
  i = i + 10
endif</code></pre>
<p>or</p>
<pre><code>if (i &gt;= 10) then
  i = i + 10
elseif (i &gt;= 5) then
  i = i + 5
  i = i * 2
else
  i = i + 1
endif</code></pre>
<p>Hopefully this illustrates the differences in the syntax indicated by the asterisk and question mark characters.</p>
<h3>JavaScript Syntax</h3>
<p>For comparison, let&#8217;s look at the equivalent JavaScript syntax for the two examples above (note the semicolons and curly braces):</p>
<pre><code>if (i &gt;= 10)
  i = i + 10;</code></pre>
<p>or</p>
<pre><code>if (i &gt;= 10)
  i = i + 10;
else if (i &gt;= 5)
{
  i = i + 5;
  i = i * 2;
}
else
  i = i + 1;</code></pre>
<h2>For Expression</h2>
<p>Often times, in your scripts, you need to execute some statements a certain number of times or iterate through a collection. In those cases (and many others), a For loop may be useful.</p>
<h3>FormCalc Syntax</h3>
<pre><code>for Variable = StartExpression
        (upto | downto) EndExpression
            [step StepExpression]?
        do
    ExpressionList
endfor</code></pre>
<p>In this syntax, there&#8217;s a <em>StartExpression</em>, and <em>EndEExpression</em> and a <em>StepExpression</em>. These are all similar to <em>SimpleExpressions</em> as described earlier. One thing to note about For loops in FormCalc is that &#8220;upto&#8221; loop will iterate as long as <em>Variable</em> is less-than-or-equal-to (&lt;=) <em>EndExpression</em> whereas a &#8220;downto&#8221; loop will keep iterating as long as <em>Variable</em> is greater-than-or-equal-to (&gt;=) <em>EndExpression</em>. Also, the variable <em>Variable</em> is implicitely declared in the For expression.</p>
<p>Based on the syntax, you can do things like add the numbers 1 to 10, inclusively, to a list:</p>
<pre><code>for i = 1 upto 10 do
  MyList.addItem(i)
endfor</code></pre>
<p>and then</p>
<pre><code>var nCount = MyList.#items[0].nodes.length

if (nCount &gt;= 2) then
  for nItem = nCount downto 1 step 2 do
    xfa.host.messageBox( MyList.#items[0].nodes.item(nItem - 1) )
  endfor
endif</code></pre>
<p>which would display the value of each even-numbered list item in reverse sequence using a message box as long as there&#8217;s at least one even-numbered list item to display.</p>
<h3>JavaScript Syntax</h3>
<p>For comparison, here are the equivalent JavaScript examples (note the semicolons and curly braces again):</p>
<pre><code>for (var i = 1; i &lt;= 10; i++)
{
  MyList.addItem(i);
}</code></pre>
<p>(in this case, the curly braces are optional but I think this is better style) and then</p>
<pre><code>var nCount = MyList.resolveNode(&quot;#items[0]&quot;).nodes.length;

if (nCount &gt;= 2)
{
  for (var nItem = nCount; nItem &gt;= 1; nItem - 2)
  {
    xfa.host.messageBox(
      MyList.resolveNode(&quot;#items[0]&quot;).nodes.item(nItem - 1) );
  }
}</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://forms.stefcameron.com/2006/09/14/formcalc-expressions-if-and-for/feed/</wfw:commentRss>
		<slash:comments>44</slash:comments>
		</item>
	</channel>
</rss>
