This article explains how to create a binding that selects a specific item from an array using a Data Connector.
Prerequisites
|
Important
|
Logic
Syntax | {{Smart field[index]}} |
---|---|
Input | number |
Output | Item x from the array |
Example
In this example a document with a data connector is created which allows a user to select a category of products in the dropdown. Each category contains one or more products, but only one specific item of the selected category will be shown in the document. The external data sources below are used in the example.
Note
|
NoteNote that both external data sources only show a sample of the content, so the data sources might actually contain thousands of items. |
Steps
To create a data connector that selects only one specific item from an array follow the steps below:
- In Word open a new document then in Template Designer click + Create template.
- On the Form tab click + Add question, then in the Type field select Data connector.
- In the Question title field type 'Product category'.
-
In External data source select 'Categories', then in Label column select 'CategoryName' (this is the second column shown in the Categories screenshot above). The result looks like this:
- Click Add to form.
- In the template type 'Selected Product Category: [Category]'.
- Below the text create a table with 2 rows and 4 columns. In the first row type the heading texts 'Product Name', 'Unit Price', 'Quantity Per Unit' and 'Product ID' (each text in a separate column). In the second row type the placeholder texts '[ProductName]', '[UnitPrice]', '[QuantityPerUnit]' and '[ProductId]' (each text in a separate column). The result looks like this:
-
In the template select the placeholder text '[Category]'. On the Template tab expand Form responses, then expand 'Productcategory', select the 'CategoryName' form field and click Add to template:
Note
- If the Template tab already contains smart fields, then you first need to click
+ Add smart field to display Form responses. - In the Advanced menu you can specify e.g. the visibility of the field (optional).
- If the Template tab already contains smart fields, then you first need to click
- In the table select the placeholder text '[ProductName]'. On the Template tab click + Add smart field then click Custom text binding.
-
In the Binding field type the binding
{{Form.ProductCategory.Products[0].ProductName}}
(the[0]
means the first item will be selected when a document is created). The result looks like this:
-
Click Add to template.
-
Repeat steps 9-11 for the other placeholder texts with the values below:
[UnitPrice] {{Form.Productcategory.Products[0].UnitPrice}}
.[QuantityPerUnit] {{Form.Productcategory.Products[0].QuantityPerUnit}}
.[ProductId] {{Form.Productcategory.Products[0].ProductID}}
.
- Save the template and upload it to the Admin Center.
- When a document is created and the category 'Beverages' (this is Category Id '1') is selected, then the result shows 'Chai | 18 | 10 boxes x 20 bags | 1' (this is the first item that has Category Id '1' in the 'Products' data source as shown in the 3rd screenshot on top of this article):
When 'Condiments' (this is Category Id '2') is selected, the result shows 'Aniseed Syrup | 10 | 12 - 550 ml bottles | 3' (this is the first item that has Category Id '2' in the 'Products' data source as shown in the 3rd screenshot on top of this article): -
Now verify what happens when you change the
[0]
in the bindings to e.g.[2]
(this will select the third item). So the bindings created in step 10 and 12 become:
[ProductName] {{Form.Productcategory.Products[2].ProductName}}
.[UnitPrice] {{Form.Productcategory.Products[2].UnitPrice}}
.[QuantityPerUnit] {{Form.Productcategory.Products[2].QuantityPerUnit}}
.[ProductId] {{Form.Productcategory.Products[2].ProductID}}
.
When a document is created and the category 'Condiments' is selected, then the result now shows 'Chef Anton's Gumbo Mix | 21.35 | 36 boxes | 5'.
Important
|
Comments
Article is closed for comments.