Using Data Items with Widgets

Version:
Current
Last modified: June 15, 2020

Widgets are available to allow you to create a tool that is consistent with Designer's look and functionality. These widgets are compatible with all lifecycle methods and do not require as much manual configuration. For example, when using widgets, you do not need to manually create data items, attach UI controls to change events to update data, or set a UI control's initial state with AfterLoad.

Widgets and the DOM

If using widgets, do not interact with them through the DOM, as they modify their DOM nodes dynamically on their own.

To begin, pull in the JavaScript UI library by adding the following script to your page header. This script uses document.write to allow the window.Alteryx.LibDir to specify where to find the UI library, regardless of where the user installed Designer.

<script type="text/javascript"> document.write('<link rel="import" href="' + window.Alteryx.LibDir + '2/lib/includes.html">') </script>

Place a Widget

Widgets are placed before SetConfiguration is called, as the JavaScript SDK UI library only places widgets at initialization. Do not try to add or modify these tags after the HTML page has finished loading.

Use the tag <ayx></ayx> to place the widget in the body of the page. This tag accepts these attributes:

  • data-ui-props: Holds a case-sensitive JavaScript object string that describes all the properties to set in selecting and configuring the widget. Properties include:
    • type: Required. Identifies which widget to render.
    • widgetId: Used to access the widget. Must be unique across all widgets.
  • data-item-props: Set to automatically create and attach a data item to the widget. Holds a case-sensitive JavaScript object string that describes all the properties to set in selecting and configuring the data item. Properties include:
  • Standard markup attributes, like class or style. The attributes persist after the widget has rendered.

<ayx data-ui-props='{type: "TextBox", widgetId: "bear", placeholder:"Pick a bear"}' data-item-props='{dataName: "chosen-bear", dataType: "SimpleString"}'> </ayx>

data-ui-props and data-item-props Processing

The data-ui-props and data-item-props attributes are processed with a direct JavaScript eval() call. You may directly reference values and objects that live on the window within it.

Manually Bind a Widget

To manually bind an existing data item to a widget, use the bindDataItemToWidget method on the Manager.

window.Alteryx.Gui.Manager.bindDataItemToWidget(window.bearDataItem, ‘bear’)

This ensures the data item's value is persistent and loads correctly from the tool's XML configuration, assuming a data item has been added to the manager or DataItemContainer.

To change the value of the described widget, call the setValue function on the associated data item; the widget also updates. Several things besides data are synchronized from the data item automatically, depending on the chosen data item.

Several common synchronized properties:

  • disabled
  • hidden
  • In the selector data items, optionList.

To alter a widget’s data item props after it has been created automatically by data-item-props, you must get the data item by data name from the Manager:

var dataItemToModify = window.Alteryx.Gui.Manager.getDataItem(dataName) dataItemToModify.setDisabled(true)

Alter a Widget

To alter a widget's UI properties after it has been created, use the setProp method. Do not attempt to alter the widget's data-ui-props on the DOM node after BeforeLoad has started; eval() on these properties only happens at initial page scrape.

window.Alteryx.Gui.Manager.setProp(widgetId, propertyName, newPropertyValue)

To alter a widget's data item properties, retrieve them from manager.getDataItem(dataName).

Use Collections

CheckBox and RadioButton widgets support toggling visibility of nested HTML elements and widgets, so the UI only presents the active selection and its nested contents. To create a collection, place HTML elements instead of an ayx tag.

<ayx data-ui-props='{type: "CheckBox", widgetId: "CheckBox1"}'> <label>Contents of a collection</label> <ayx data-ui-props='{type: "Button", widgetId: "Button1"}'> </ayx>

HTML
  1. <!DOCTYPE html> 
  2. <html> 
  3. <head> 
  4.     <meta charset="UTF-8"> 
  5.     <title>Location tool</title> 
  6.     <script type="text/javascript"> 
  7.       	// This line below is the necessary piece for loading our UI library 
  8.       	document.write('<link rel="import" href="' + window.Alteryx.LibDir + '2/lib/includes.html">') 
  9.     </script> 
  10.     <script> 
  11.       	// Any widget data-ui-props you wish to create in JavaScript instead of in-line 
  12.       	// should be placed in a script tag here in the head, so that the object is alive 
  13.       	// on the window, when the widget itself get's processed during SetConfiguration 
  14.       	// which happens immediately after the page has completed loading. 
  15.       	// If you are referencing a config object then we recommend namespacing your UI to reduce the likelihood of 
  16.       	// conflicting with other properties on the window. 
  17.       	window.MyGUINamespace = {} 
  18.       	window.MyGUINamespace.XWidgetInitialProps = { 
  19.         	type: 'TextBox', 
  20.         	widgetId: 'X', 
  21.         	placeholder: 'X coord value' 
  22.       	} 
  23.       	window.MyGUINamespace.YWidgetInitialProps = { 
  24.         	type: 'TextBox', 
  25.         	widgetId: 'Y', 
  26.         	placeholder: 'Y coord value' 
  27.       	}
  28.     </script> 
  29. </head> 
  30. <body> 
  31.  
  32.     <h1>Location:</h1> 
  33.     <br /> 
  34.     <label>X:</label> 
  35.     <!-- Note, you may use the data-ui-props to simply reference variables, as it will be eval()ed as JavaScript. --> 
  36.     <ayx data-ui-props='window.MyGUINamespace.XWidgetInitialProps'></ayx> 
  37.     <br /> 
  38.     <label>Y:</label> 
  39. 	<ayx data-ui-props='window.MyGUINamespace.YWidgetInitialProps'></ayx> 
  40. 	</ayx> 
  41.  
  42.     <script type="text/javascript"> 
  43.  		window.Alteryx.Gui.BeforeLoad = function (manager, AlteryxDataItems, json) {
  44.     		// Using SimpleInt data items to work with integral values. 
  45.     		var xDataItem = new AlteryxDataItems.SimpleInt('X') 
  46.  
  47.     		manager.bindDataItemToWidget(xDataItem, 'X') 
  48.  
  49.     		// Adding the data items to the manager is necessary if you want it 
  50.     		// to automatically persist them during GetConfiguration for you 
  51.     		// The manager is also window scoped at window.Alteryx.Gui.manager 
  52.     		manager.addDataItem(xDataItem) 
  53.   		} 
  54.  
  55.   		// This function will be called when it tries to persist the data 
  56.   		// to give you a way of returning a string to display on the canvas 
  57.   		window.Alteryx.Gui.Annotation = function (manager) { 
  58.     		var xDataItem = manager.getDataItem('X') 
  59.     		var yDataItem = manager.getDataItem('Y') 
  60.     		return 'X:' + xDataItem.getValue() + '\nY:' + yDataItem.getValue() 
  61.   		} 
  62.  
  63.     </script> 
  64. </body> 
  65. </html>
HTML
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Location tool</title>
  6.     <script type="text/javascript">
  7.       // This line below is the necessary piece for loading our UI library
  8.       document.write('<link rel="import" href="' + window.Alteryx.LibDir + '2/lib/includes.html">')
  9.     </script>
  10.     <script>
  11.       // Any widget data-ui-props you wish to create in JavaScript instead of in-line
  12.       // should be placed in a script tag here in the head, so that the object is alive
  13.       // on the window, when the widget itself get's processed during SetConfiguration
  14.       // which happens immediately after the page has completed loading.
  15.       // If you are referencing a config object then we recommend namespacing your UI to reduce the likelihood of
  16.       // conflicting with other properties on the window.
  17.       window.MyGUINamespace = {}
  18.       window.MyGUINamespace.XWidgetInitialProps = {
  19.         type: 'TextBox',
  20.         widgetId: 'X',
  21.         placeholder: 'X coord value'
  22.       }
  23.       window.MyGUINamespace.YWidgetInitialProps = {
  24.         type: 'TextBox',
  25.         widgetId: 'Y',
  26.         placeholder: 'Y coord value'
  27.       }
  28.     </script>
  29. </head>
  30. <body>
  31.  
  32.   <h1>Location:</h1>
  33.   <br />
  34.   <label>X:</label>
  35.   <!-- Note, you may use the data-ui-props to simply reference variables, as it will be eval()ed as JavaScript. -->
  36.   <ayx data-ui-props='window.MyGUINamespace.XWidgetInitialProps'
  37.        data-item-props='{ dataName: "X", dataType: "SimpleInt" }'>></ayx>
  38.   <br />
  39.   <label>Y:</label>
  40.   <ayx data-ui-props='window.MyGUINamespace.YWidgetInitialProps'
  41.        data-item-props='{ dataName: "Y", dataType: "SimpleInt" }'></ayx>
  42.   </ayx>
  43.  
  44.   <script type="text/javascript">
  45.  
  46.   // This function will be called when it tries to persist the data
  47.   // to give you a way of returning a string to display on the canvas
  48.   window.Alteryx.Gui.Annotation = function (manager) {
  49.     var xDataItem = manager.getDataItem('X')
  50.     var yDataItem = manager.getDataItem('Y')
  51.     return 'X:' + xDataItem.getValue() + '\nY:' + yDataItem.getValue()
  52.   }
  53.  
  54.   </script>
  55. </body>
  56. </html>
Was This Helpful?

Running into problems or issues with your Alteryx product? Visit the Alteryx Community or contact support.