Collecting field objects and setting data

Actions you can use in your projects.

Collecting field objects and setting data

Postby Clifton » Sat Jul 11, 2015 5:28 am

Here is a bit of openScript that many developers may have used in their projects:

    step i from 1 to n
    text of field ("a" & i) = myVar [ i ]
    end step
How do you convert this for web deployment in the Actions Editor using the PowerPac?
  1. Create an example page similar to the one shown below. It has seven field with each named "a1", "a2", "a3", ... "a7". The fields can be grouped or left as individual objects on the page (which is what we did in this example). To guarantee that they will be ordered in a predictable way, make sure to layer them in the order in which you wish to collect them for maniplulation. We only added the intial text as a reference for the sake of this example. In production use, the fields would probably contain boiler plate text or none at all.

      page.png
      Example page with multiple fields
      page.png (41 KiB) Viewed 4477 times
  2. For this example, we also put our source string in a field called "Source_Values" at the bottom of the page and we created a button to hold the actions to be executed in this example.
     
  3. We created 3 variables for this example. They are objects, tmp, and n.
     
  4. Now build the actions for the on click event of the "Execute Actions" button as shown below.

      Image 1.png
      Collect field objects by using a filter where object name must begin with "^a" and be followed by one or more digits "\d+". We wrap this regular expression in slashes to tell the browser this is a special filter.
      Image 1.png (28.82 KiB) Viewed 4477 times

      The result of pgTBgetObjects() puts the unique ToolBook ids or their names into the array objects.

      Next we prepare the corresonding array tmp to hold the values that will be put into each field collected by pgTBgetObjects().

      Image 2.png
      Create an array from our numeric string by splitting the values by the comma that separates them. We store the result of this function in the variable tmp. This variable is automatically converted to an array by the function.
      Image 2.png (28.97 KiB) Viewed 4477 times

      Our next actions use a for each loop to set the data for each object in the objects [ ] array. We use the variable n as the placeholder to get the current object id being maniplulated by the current loop.

      Image 3.png
      On each iteration of our loop we will format the number using the Euro sign and add a decimal of padded zeros to represent a fraction of a Euro. Notice the reformatted value is saved in the same place as the original value which effectively replaces it with our newly formatted value.
      Image 3.png (28.67 KiB) Viewed 4477 times

      Finally, we use pgTBObjSet() to actually insert the data into the current object being manipulated in the loop.

      Image 4.png
      Set the data as "text" using pgTBObjSet().
      Image 4.png (28.66 KiB) Viewed 4477 times
  5. Export your sample and observe the result. You can also download the ToolBook source file that we used to build this example.
NOTE: If you use actions like this frequently, we would recommend building a sharedAction that is called with parameters that define the filter and string values to insert as well as any other custom manipulation that may be needed. This would greatly simplify using this code over and over again and make modifications easy as the sharedAction only needs to be modified in one place rather than on every object where the code appears.
Attachments
manipulatingObjects.zip
Source files for ToolBook v11.5
(32.32 KiB) Downloaded 409 times
Clifton
Site Admin
 
Posts: 732
Joined: Tue Jan 14, 2014 1:04 am

Return to Action Editor Examples

Who is online

Users browsing this forum: No registered users and 1 guest

cron