Update a doc property, trigger an ironPython script or even a javascript based on filtering, custom expression or marking

To update a document property, trigger an ironPython script when a calculated value executes based on marking or filtering and even pass the value to a javascript variable, the flow is:

Spotfire Expression ➭ r Input Variable ➭ r Output Variable ➭ Document Property ➭ Property Control ➭ Javascript Variable

The Data Function takes an expression through the input parameter 'a' and passes the value to 'b' which is then stored in a 'c' document property. When the document property changes, an iron python script executes which adds a space to a text area. This forces the text area to refresh, triggering its javascript that extracts the document property value from a label control. There are ways to avoid using the iron python and datafunciton all together by using a calculated value or any property control on a text area by using a javascript timer interval.

  1. Go to Edit>Data Function Properties and Register a new simple TERR Data Function that takes an input and returns the same input. For example:

    b <- a #pass the value of a to b

  2. Run the function and set the input parameter 'a' as an Expression (limited by marking or current filtering) and the output parameter 'b' as a Document Property. Make sure the input handler expression is limited by the active filtering or marked rows.  The function must be set to auto refresh and run locally.

  3. At this point, every time the number of filtered rows changes, the expression will send the result to the document property 'c', which now behaves like a Calculated Value with python capabilities. To demonstrate, add a Label Property Control on a Text Area to show the value of 'c'
  4. To pass this document property to a javascript variable, extract the value of the Label Property Control with Javascript. For example:

    <span id='d' style='border:1px solid black'><SpotfireControl id="77fa68f4471f48b2a51e0b2c9049f1d1" /></span>

    #extract doc prop 'c' from div 'd' and stores it in javascript varialbe 'e' and animates the div 'd' width to 'e' pixels
    function refreshWidth(){ e=d.innerText $("#d").animate({width:e+"px"},500)}
  5. //trigger refreshWidth refreshWidth()

  6. Finally, to trigger this javascript every time the document property changes, add a python script to force refreshing the text area.

    from Spotfire.Dxp.Application.Visuals import HtmlTextArea
    textArea.As[HtmlTextArea]().HtmlContent += " "

    As an alternative, use the refreshWidth Javascript function in a timer. Use either one, but not both!

    setInterval(refreshWidth,500) //trigger every .5 sec)

1 comment:

Stephen MacKenzie said...

Awesome article. Put this into practice today, and opens up some great possiblities. Thanks a million.