June Writing Scripts for ZumeForms This document will assist you write scripts in forms for Zumeforms ZumeSoft Solutions Pty Ltd 16 Index 1 1.1 1.2 1.3 2 2.1 3 3.1 3.2 3.3 4 4.1 4.2 4.3 5 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 Introduction............................................................................................................... 5 What are scripts? ................................................................................................................................................ 5 Why may you need to use scripts? .............................................................................................................. 5 ZumeForm extensions ...................................................................................................................................... 5 The Script Editors ....................................................................................................... 5 Where are the Script Editors? ....................................................................................................................... 5 Languages .................................................................................................................. 7 Supported languages and frameworks ...................................................................................................... 7 Comments in scripts .......................................................................................................................................... 7 Ending lines of scripts with a semicolon................................................................................................... 7 Script Variables .......................................................................................................... 7 Declaring a variable ........................................................................................................................................... 7 Initialisation of a variable ............................................................................................................................... 8 Assignment of a variable ................................................................................................................................. 8 Using ZumeForm form fields in variables and scripts .................................................. 8 What is a form field?.......................................................................................................................................... 8 Referencing a specific field on a form ........................................................................................................ 8 The ZumeForms simple field name object ............................................................................................... 9 Setting the value of a variable in a script to the value of a field on a form ................................. 9 Setting the value of a field on a form to the value of a variable in a script ................................. 9 Setting the value of a variable to the value of a field in the Field Script Editor ........................ 9 Setting the value of a field to the value of a variable using $(this) .............................................. 10 Setting the value of one field with the value of another ................................................................... 10 Setting the value of one field with a modified version of the value of another ...................... 10 6 Setting the Record Name value ................................................................................ 11 7 Referencing a value in a repeating section ................................................................ 11 7.1 Accessing a form field value from within the repeat ......................................................................... 11 8 Doing basic maths functions ..................................................................................... 12 9 Simple Boolean logic ................................................................................................ 12 10 Simple conditional logic ........................................................................................ 12 10.1 10.2 10.3 If logic .................................................................................................................................................................... 12 If-else logic .......................................................................................................................................................... 12 Short if statement ............................................................................................................................................. 13 11 Looping ................................................................................................................ 13 11.1 11.2 While ...................................................................................................................................................................... 13 For ........................................................................................................................................................................... 13 12 Alerts ................................................................................................................... 14 Page 2 13 Functions.............................................................................................................. 14 13.1 13.2 13.3 What is a function?........................................................................................................................................... 14 Creating a function ........................................................................................................................................... 14 Running a function on a form ...................................................................................................................... 15 14 Built-in system functions ...................................................................................... 15 14.1 14.2 14.3 14.4 14.5 14.6 14.7 14.8 14.9 14.10 14.11 14.12 What is a system function? ........................................................................................................................... 15 getGender............................................................................................................................................................. 15 getAge .................................................................................................................................................................... 15 calcMinor(age) ................................................................................................................................................... 16 isTrue, for Yes/No Group, Radio Button CheckBox Group .............................................................. 16 isFalse, for Yes/No Group, Radio Button CheckBox Group ............................................................. 16 isChecked, for Yes/No Group, Radio Button CheckBox Group ...................................................... 16 CheckedValue, for Yes/No Group, Radio Button CheckBox Group .............................................. 16 SelectedValue, for Dropdown List and List Box ................................................................................... 16 changeVal ........................................................................................................................................................ 17 sumValues, for repeats .............................................................................................................................. 17 Any ..................................................................................................................................................................... 17 15 Adding functions .................................................................................................. 17 15.1 15.2 Function to add 2 numbers together ........................................................................................................ 17 Function to add 2 strings (words) together .......................................................................................... 18 16 Setting a field in a repeat ...................................................................................... 18 16.1 Counting how many repeats ........................................................................................................................ 19 17 Setting and hiding system buttons ........................................................................ 19 18 Testing for conditions ........................................................................................... 20 18.1 18.2 18.3 Testing for a condition based on a number ........................................................................................... 20 Testing for a condition based on a string (word)................................................................................ 21 Testing for a condition based on a checkbox (Boolean)................................................................... 21 19 Setting headings and labels................................................................................... 22 19.1 19.2 19.3 19.4 19.5 Setting a heading to the value of a field ................................................................................................... 22 Setting the label of a checkbox .................................................................................................................... 23 Set node label in a yn control....................................................................................................................... 23 Setting a yn labels with script on load ..................................................................................................... 23 Setting the value of a heading...................................................................................................................... 24 20 Triggering events .................................................................................................. 25 20.1 20.2 20.3 20.4 20.5 20.6 AfterFormLoad .................................................................................................................................................. 25 BeforeDataLoad................................................................................................................................................. 25 afterDataLoad .................................................................................................................................................... 25 BeforeAssembly ................................................................................................................................................ 26 Runs a function every time there is input on any page..................................................................... 26 Runs function every time input within repeat section ..................................................................... 26 21 Check status of a number of fields ........................................................................ 26 22 Resources ............................................................................................................. 29 22.1 Shortcuts for script editor............................................................................................................................. 29 Page 3 22.2 22.3 Syntax .................................................................................................................................................................... 29 Overview of HTML, CSS and JavaScript ................................................................................................... 29 Page 4 Writing scripts 1 Introduction 1.1 What are scripts? Scripts are short pieces of code that you can incorporate into your forms to make them do things that are outside the built-in functionality of the ZumeForms form designer. ZumeForms uses the well recognised and very powerful scripting language of JavaScript, together with the jQuery framework. This is a very well known language, and there is plenty of material on the internet about using this language. Complete web applications are built on JavaScript. You can pretty much make your forms do anything. 1.2 Why may you need to use scripts? One of the fundamental objectives of ZumeForms is to make it easy for anyone to design sophisticated forms, and to use those forms to automate sophisticated documents – without significant IT support. Ideally there would be no need to use scripts. However, there are some things that we have not implemented within the built-in functionality of ZumeForms, or which are particularly tailored to a specific user requirement. We have therefore built in the script editors to enable you to bridge these gaps. We have been releasing a steady stream of updates to the ZumeForms platform to do away with the needs for scripts. This will continue as the platform evolves. 1.3 ZumeForm extensions In addition to the standard JavaScript language and jQuery framework, we have incorporated into the ZumeForms platform a number of extensions and simplifications that make using scripts a little easier. For example, a reference to a field on a form would normally be written in JavaScript as “$('[name$="FieldName"]')”. However, we have included an extension so you can simple refer to these fields as “fld(‘FieldName’)”. These extensions are noted throughout this manual. 2 The Script Editors 2.1 Where are the Script Editors? There are 2 Script Editors: 2.1.1 The Form Script Editor There is a separate script editor for each form, i.e. the Form Script Editor. You write ‘Form Script’ in this editor. This can be found under the ‘Form’ menu item: Page 5 2.1.2 Field Script Editor There is also a script editor associated with each field, i.e. the Field Script Editor. You write ‘Field Script’ in here that only applies to the particular field you are working with: 2.1.3 What does the Script Editor look like? In each case, the Script Editor looks like this: Page 6 3 Languages 3.1 Supported languages and frameworks As noted above, you can write your scripts using JavaScript and the jQuery framework. We have also written some specific extensions that apply specifically to scripts written on the ZumeForms platform. 3.2 Comments in scripts When we describe what a script is doing we use “//” to denote that the contents after these forward slashes is a comment, and not code. 3.3 Ending lines of scripts with a semicolon You must include a “;” (semicolon) at the end of each line of code. 4 Script Variables 4.1 Declaring a variable You can store values in ‘variables’. Variables are used in your scripts to make things happen, e.g. to perform calculations (number variables), create words (string variables), and set other form field values. A variable does not exit in a script until it is ‘declared’, i.e. ‘brought to life’. A variable is declared as follows: var name; //This declares the variable ‘name’ Page 7 var number; //This declares the variable ‘number’ var name, number; //This declares multiple variables at once, i.e. ‘name’ and ‘number’. You need to include the comma between the variables 4.2 Initialisation of a variable When a variable is first declared it can also be initialized with a starting value: var name = “Tom”; //This will create the variable ‘name’ and give it a starting value of ‘Tom’ var number = 20; //This will create the variable ‘number’ and give it a starting value of ‘20’ var name = “Tom”, number = 20; //Multiple variables at once 4.3 Assignment of a variable You can also assign a value to a variable that has already been declared or initialized. When you assign a value to a variable you use the single ‘=’ (equals) sign. name = “Andrew”; number = 25; You need to be sure that you have already created the variable before you do this. Note that if a variable already has a value and you assign a new value to it, this will usually overwrite the original value and replace it with the new value. 5 Using ZumeForm form fields in variables and scripts 5.1 What is a form field? Each field on a form has a field name. This is set when you drag the field onto your form. You can also open up the properties of a form field and change its name. But, be careful doing this, as it may break the connection between the form field, scripts, and any associated document template (where you have referenced the field old name). The contents of a form field can be used in scripts by referencing the form field name. Put simply, you can use values that a user enters into a field on a form in your scripts, and you can then write back the value of a variable in a script to a field on a form, i.e. the data can go both ways. 5.2 Referencing a specific field on a form A field on a form is referenced in a script as: //This references the field on the form called ‘Result’ $('[name$="Result"]') Page 8 The value of a field on a form is referenced in a script as: //This references the value of the field on the form called ‘Result’ $('[name$="Result"]').val() The first part of this script i.e. $('[name$="Result"]'), is the field itself. The .val() part of this script is telling ZumeForms that we want to use the value of this field as it has been entered on the form by the user. 5.3 The ZumeForms simple field name object Because you are likely to be referencing a lot of form fields in your scripts we have extended JavaScript to include a simple short-hand way to refer to a form field: // The following code is equivalent to $('[name$="Result"]').val() fld(‘Result’).val(); 5.4 Setting the value of a variable in a script to the value of a field on a form To set the value of a variable in a script (result) to the value of a field on a form (Result) you do this: var result = $('[name$="Result"]').val(); You can then work with the contents of the variable ‘result’ in your scripts. If we use the ZumeForms short field reference: var result = fld(‘Result’).val(); 5.5 Setting the value of a field on a form to the value of a variable in a script To set a form field (Result) to the value of a variable that has been calculated in a script (result) you do this: $('[name$="Result"]').val(result); This sends the value of the script variable back to the form. This value can then be used in documents that are assembled using the form’s data. If we use the ZumeForms simple field reference to do the same thing: fld(‘fldname’).val(result); 5.6 Setting the value of a variable to the value of a field in the Field Script Editor If you are in the Field Script Editor for a particular field on a form, you can reference the value of that particular form field in a short-hand way using the $(this) command. The following script will set the variable ‘value’ to the value of the current field in which this script is placed: var value = $(this).val(); Page 9 5.7 Setting the value of a field to the value of a variable using $(this) Similarly, if you are in the Field Script Editor for a particular field on a form, you can set the value of the particular form field to the value of a script variable as follows: $(this).val(result); 5.8 Setting the value of one field with the value of another This will get the value in the text box field and put it into another text box called Text2_txt. Place this script in the Field Script Editor where the value will be entered by the user: // This sets the variable ‘str’ to the value of what is in the current field, i.e. $(this). var str = $(this).val(); // This sets the value of Text2_txt to the value of ‘str’. $('[name$="Text2_txt"]').val(str); // Or using the shortform field reference: fld(‘Text2_txt’).val(str); 5.9 Setting the value of one field with a modified version of the value of another This script will get the value of the text box that it is inserted into, convert the contents to sentence case, and then put the result into another text box, Text2-txt. Place this script in the field where the value will be entered by the user: // Set the variable ‘str’ to the value of what is in the current field, i.e. $(this). var str = $(this).val(); // Set the first character in ‘str’ to upper case, and then add that to the rest of the string in ‘str’ that is set to lowercase. str = str.charAt(0).toUpperCase() + str.substring(1).toLowerCase(); // This sets the value of Text2_txt to the modified value of ‘str’. $('[name$="Text2_txt"]').val(str); // Or using the shortform field reference: fld(‘Text2_txt’).val(str); Page 10 6 Setting the Record Name value When ZumeForms displays a Record on the My Records page it will reference the ‘Record Name’ of the form that created the Record. The Record Name is a special attribute of a Record that can be set to the value of any field on the form that was used to create the Record. You set a field as the Record Name by placing the following script in the Field Script Editor: // This sets the field #RecordName to the value of the field into which this script is copied, i.e. ‘$(this).val()’ $('#RecordName').val($(this).val()); A longhand way of doing this would be in two steps: var str = $(this).val(); $('#RecordName').val(str); 7 Referencing a value in a repeating section ZumeForms enables you to create a section of a form that repeats, so that you can enter multiple values for each field in the repeating section. For example, you could use a repeating section to collect a series of names, with a different name in each row of the repeat. The trick with referencing a form field value in a repeat is that the field name will have multiple values, one for each row of the repeat. So when you are accessing the form field value you need to understand where you are accessing it from, i.e. from within a row of the repeat itself, or outside the repeat entirely. 7.1 Accessing a form field value from within a repeat row An example of accessing a form field value from within a repeat would be when you want to add to words together within each repeat row, e.g. a first and last name, and then store the resulting value in a third form field within the same repeat row. In this scenario you can use the local context of ‘this’, i.e. ‘get me the value from this row within the repeat’. // ‘this’ is the context of the field that triggers the lookup. // Use this function to get the value of a field within a row within a repeat. fld(this, 'fldname').val(); 7.2 Setting a field within a repeat $(this).closest('.repeat').find('[name$="Proprietor_Name_Short_scr"]').val($(this).v al()); Page 11 8 Making fields show and hide on a form In most cases you will be able to make things show and hide on a form using the Rules Wizard. However, there may be times when you want to do with this script. 9 Doing basic maths functions You can perform basic functions on variables, like adding variables together. This function declares the variables ‘number1’, ‘number2’ and ‘result’, and assigns a value to result equal to number1 added to number2: var number1 = 10, number2 = 20; var result = number1 + number2 10 Simple Boolean logic Boolean logic is concerned with whether something is true or false. To find out when two values are equal, use the triple equals operator (“===”). 15.234 === 15.234 true We can also determine if two values are not equal using the triple not equal operator (“!==”). 15.234 !== 18.4545 true 11 Simple conditional logic 11.1 If logic Logic is used to make decisions in code, choosing to run one piece of code or another depending on the comparisons made. This requires use of something called a conditional. if (10 > 5) { // Run the code in here }; 11.2 If-else logic The if-else form of an if statement is used to run an alternative piece of code if the conditional is not true. The code in the if block below will be ignored, for example - only the code in the else block will be run. Page 12 if (43 < 2) { // Run the code in here }; else { // Run a different bit of code }; 11.3 Short if statement This is a shorthand way of writing an IF statement. The variable ‘isminor’ will either be set as true or false depending on the value of the variable ‘age’: var isminor = age < 18 ? true : false; 12 Looping Loops are a way of repeating the same block of code over and over. 12.1 While A while loop repeats a block of code while a condition is true. Like an if statement, the condition is found in parentheses. // After looping has finished the code carries on running from just after the closing brace (“}”) of the loop’s block. var i = 1; while (i < 10) { alert(i); i = i + 1; }; // i is now 10 12.2 For A for loop is similar to an if statement, but it combines three semicolon-separated pieces of information between the parentheses: initialization, condition and a final expression. The initialization part is for creating a variable to let you track how far through the loop you are - like i in the while example; the condition is where the looping logic goes - the same as the condition in the while example; and the final expression is run at the end of every loop. // This gives us alert boxes containing the numbers 1 to 10 in order. // i++ is equivalent to i = i + 1. Page 13 for (var i = 1; i < 10; i++) { alert(i); } 13 Alerts The alert function makes a pop-up on screen. // This makes a pop-up with the words ‘Hello, world’. alert("Hello, world."); // This makes a pop-up with the contents of the variable ‘string’. alert(string); 14 Functions 14.1 What is a function? Functions are reusable blocks of code that carry out a specific task. To execute the code in a function you call it. A function can be passed arguments to use, and a function may return a value to whatever called it. A function can perform an action on a form or on a field. A function can be called within a form when an event occurs, for example, if the content of a field changes, or a form loads, or just prior to a document being assembled. 14.2 Creating a function You can create and save functions as the value of a variable. You can then call the function using this variable and a pair of parentheses. This is also called invoking the function. To create a function called ‘add’, use the function keyword. You then list the arguments in parentheses, and then supply a block that contains the function’s code. Here’s a function that adds two numbers: var add = function (a, b) { return a + b; }; Page 14 a and b are the function’s parameters, and the value it returns is signified by the return keyword. The return keyword also stops execution of the code in the function; nothing after it will be run. To call the add function and store the result in the variable ‘result’: var result = add(1, 2); // variable ‘result’ is now 3. This calls add with the arguments 1 and 2, which, inside add, will be saved in the variables a and b. 14.3 Running a function on a form It is a good design principle to place functions in the Form Script Editor, and then call the functions from the Field Script Editor for the relevant field. Functions that run on form events, like loading a form, or assembling a document, can be called from the Form Script Editor itself. 15 Built-in system functions 15.1 What is a system function? There are a number of common tasks that a lot of ZumeForm users need done on a regular basis. Rather than requiring each user to write their own scripts to performs these functions, we have built a number of these common functions into the ZumeForms platform. You can use these functions by simply calling the built in system function. 15.2 getGender This system function determines the gender of someone based on the answer to the salutation field of the person combo: // This is based on the 6 item salutation field (Mr, Mrs, Miss, Ms, Dr (m), Dr (f)) // Where the value returned to variable ‘gender’ is male or female. var gender = getGender(value); 15.3 getAge This system function determines the age of a person based on a date field: // Where value passed in a date of birth from the field. // This calculates an age based on the content of the date field. var age = getAge(fld('DOB_Field').val()); Page 15 // Set a field to an age when a date field is changed. Place this script in the date field. fld('Age_Field_scr').val(getAge($(this).val())); 15.4 calcMinor(age) This function will search a form for a particular field and calculate if the value of that field is less than the age that is passed into the field, i.e. ‘age’: // The variable ‘isminor’ will return true if any of the values of child_age_scr are below 18. var isminor = $("[name$='child_age_scr']").calcMinor(18); 15.5 isTrue, for Yes/No Group, Radio Button CheckBox Group This system function determines whether a checkbox is true: // If one of the items checked has a true value, it will look for the word ‘true’. $('checkboxname').isTrue(); 15.6 isFalse, for Yes/No Group, Radio Button CheckBox Group This system function determines whether a checkbox is false: // If one of the items checked has a false value, it will look for the word ‘true’. $('checkboxname').isFalse(); 15.7 isChecked, for Yes/No Group, Radio Button CheckBox Group This system function determines whether a checkbox is checked: // Does not look at values, only looks to see if any item is checked, if any item is checked then it returns ‘true’. $('checkboxname').isChecked(); 15.8 CheckedValue, for Yes/No Group, Radio Button CheckBox Group This system function determines the checked value of a checkbox: // This will return the actual value checked. $('checkboxname').CheckedValue(); 15.9 SelectedValue, for Dropdown List and List Box This system function determines the value that has been selected in a drop-down: // Returns the value selected out of a drop-down or list. $('doprdownname').SelectedValue(); Page 16 15.10 changeVal This system function triggers the script to run on another field: // triggers the script for fldname $('fldname').changeVal(); 15.11 sumValues, for repeats This system function totals the value of a field within a repeat: // Adds the values of the identified field within a repeat. // Should be included in the field that is being added, so that it updates. var sum = $('fldname').sumValues(); fld(‘fldname’).val(sum); 15.12 Any This system function looks to see if there are any fields on a form: fld('fldname').Any(); 16 Adding functions 16.1 Function to add 2 numbers together This function takes two field values, ‘Number1’ and ‘Number2’, adds them together, and then places the answer into the ‘Result’ field: function calcResult(){ // Sets variable val1 to the value of Number1. // The parseInt ensures that the contents of Number 1 is a number. var val1 = parseInt($('[name$="Number1"]').val()); var val2 = parseInt($('[name$="Number2"]').val()); var total = val1 + val2; // Sets the value of the ResultNumber field to the value of the variable ‘total’. $('[name$="ResultNumber"]').val(total); Page 17 }; 16.2 Function to add 2 strings (words) together This function takes two field values, ‘Word1’ and ‘Word2’, adds them together with a space in between (i.e. ‘ ‘), and then places the answer into ‘ResultWord’ field: function addWord(){ var val1 = $('[name$="Word1"]').val(); var val2 = $('[name$="Word2"]').val(); var total = val1 + ' ' + val2; $('[name$="ResultWord"]').val(total); }; 17 Setting a field in a repeat In order to run this function you can create a button to setNames() or you could include it in a field to be set when there is a change to the value entered into the field. function setNames(){ // Go and look for each object where the type is Persons_rpt and identify a list of rows. // For each object found perform the function. // Count is the counter (index). // person_rpt is the object (row) in the collection on which the function is acting. $('[data-repeat="Persons_rpt"]').each(function(count, person_rpt) { // $(this) is each repeat as it goes through the loop. // So $(this).find ... means find inside this repeat only. var name_scr = $(this).find('[name$="Name_First_txt"]').val() + ' ' + $(this).find('[name$="Name_Last_txt"]').val(); Page 18 // Sets Name_scr to the value of the variable name_scr for each repeat. $(this).find('[name$="Name_scr"]').val(name_scr); }); }; 17.1 Counting how many repeats function repeatLength(){ // This counts the length of the data repeat object Persons_rpt. var length = $('[data-repeat="Persons_rpt"]').length; alert(length); OR // This counts the length of the data repeat object Persons_rpt. alert('There are ' + $('[data-repeat="Persons_rpt"]').length + ' persons.' ); }; 18 Setting and hiding system buttons These function set the labels on system buttons and hide buttons, after the form loads. Put this function in the Form Script Editor: function AfterFormLoad(){ // Find the object where the ID is zf-submit-payment. $('#zf-submit-draft').hide(); $('#zf-submit-payment').text('Pay now'); // Find the objects with the class btn-save. This one just saves the data, but does not fire any of the notifications or assemblies. Page 19 $('.btn-save-data').hide(); // Find the objects with the class btn-save-data. This saves the data and also submits to the assembly engine. [Should really be called ‘.btn-submit’] $('.btn-save').hide(); }; 19 Testing for conditions 19.1 Testing for a condition based on a number This function tests for a number of results: function conditionNumber(){ var val1 = parseInt($('[name$="Number1"]').val()); var val2 = parseInt($('[name$="Number2"]').val()); var total = 0; if (val1 > 5) { total = val1 + val2; }; else{ total = val1 * val2; }; //single = when assigning value to variable if (val1 > 5 && val2 < 4) { total = val1 + val2; }; Page 20 //use && for AND, use == for equals, use || for OR if (val1 > 5 && Val2 == 7 || val2 < 4) { total = val1 + val2; }; $('[name$="Result"]').val(total); }; 19.2 Testing for a condition based on a string (word) This function tests for a string result ‘Andrew’ and ‘BruceFallen’: function conditionString(){ var val1 = $('[name$="Word1"]').val(); var val2 = $('[name$="Word2"]').val(); var total = ''; if (val1 == 'Andrew' && val2 == 'Bruce'+'Fallen') { total = val1 + val2; }; else{ total = ''; }; $('[name$="ResultWord"]').val(total); }; 19.3 Testing for a condition based on a checkbox (Boolean) This function tests for certain conditions: function conditionCheck(){ Page 21 // For dropdown list or list box var totalSelect = $('[name$="Field_cho"]').SelectedValue(); // For radio or yn control or checkbox group var totalCheck = $('[name$="Field_cho"]').CheckedValue(); // Does 'Yes' appear anywhere in totalSelect if (totalSelect.indexOf('Yes') >= 0) { // DO SOMETHING }; // Does totalSelect equal 'Man' if (totalSelect == 'Man') { // DO SOMETHING }; }; 20 Setting headings and labels 20.1 Setting a heading to the value of a field One alternative: // gets the option where the value is true var heading = $('#HEADING_1003'); // sets the prompt - which is just text $(heading).text($(this).val()); Another alternative: // sets HEADING_1003 text to the value of the current field Page 22 $('#HEADING_1003').text($(this).val()); 20.2 Setting the label of a checkbox Place the following script in the field that will be used for the label: // gets the option where the value is true var chk = $('#field_chk'); // sets the prompt - which is just text $(chk).find('.control-label').text($(this).val()); 20.3 Set node label in a yn control Place the following script in the field that will be used for the node label: // gets the option where the value is true var yn = $('[name$="field_yn"][value="true"]'); // sets the prompt - which is just text $(yn)[0].nextSibling.nodeValue = $(this).val(); // 1. call this from the field // setcheck(this); // 2. put this in the shared script // function setcheck(ctrl){ //$(yn)[0].nextSibling.nodeValue = $(ctrl).val(); // sets the prompt - which is just text //}; 20.4 Setting a yn labels with script on load This function will replace the option labels in a yn control after the form is loaded. Place this script in the Form Script Editor: Page 23 function AfterFormLoad(){ setYNprompts(); //This calls the setYNprompts function }; function setYNprompts() { $($('[name$="Field_yn"][value="true"]'))[0].nextSibling.nodeValue = "True Prompt"; $($('[name$="Field_yn"][value="false"]'))[0].nextSibling.nodeValue = "False Prompt"; }; 20.5 Setting the value of a heading The following function call is placed in the Field Script Editor of the field that will trigger the change: LabelChange(this); //This will call the function when the field value changes This function is placed in the Form Script Editor: function LabelChange(ctrl){ //gets value of yes-no control and puts it in the variable ‘value’ var value = $(ctrl).CheckedValue(); //sets another field label to the value of the variable ‘value’ $('#Field_txt').find('.control-label').text(value); //sets value of another field to the ‘value’ of the yes/no control $('[name$="Field_txt"]').val(value); }; Page 24 21 Triggering events These functions run on the events, they do not need to be called separately – they will be triggered automatically when the event occurs. 21.1 AfterFormLoad This function will run after the form loads: function AfterFormLoad(){ alert('After Form Load'); // Logic to be run after form loads }; 21.1.1 Logic - JUST OPERATES ON ONE PAGE function DemoAdding2numbers_AfterFormLoad(){ alert('Page only After Form Load'); }; 21.2 BeforeDataLoad This function runs immediately after a form has loaded but before any data has been loaded into the form: function BeforeDataLoad(){ alert(Before Data Load'); // Logic to be run after data is loaded into the form }; 21.2.1 Logic - JUST OPERATES ON ONE PAGE function DemoAdding2numbers_BeforeDataLoad(){ alert('Page only Before Data Load'); }; 21.3 afterDataLoad This function will run immediately after the data in a record has been loaded into a form. This would update any calculations in the form. function afterDataLoad(){ alert('After Data Load'); // Logic to be run after data is loaded into the form Page 25 }; 21.4 BeforeAssembly This function will run immediately before assembly of a document. You may want to include other functions to calculate values immediately before the document is prepared: function BeforeAssembly(){ alert('Before Assembly'); // Logic to be run before assembly of document }; 21.5 Runs a function every time there is input on any page $( "input, select" ).change(function() { // xyzfunction(); alert('Field has changed'); }); 21.6 Runs function every time input within repeat section $('#SectionName').find( "input, select" ).change(function() { // xyzfunction(); alert('Field has changed'); }); 22 Check status of a number of fields function Assets(){ var sum = $('[name$="Asset_Value_num"]').sumValues(); $('#SelectedForm').find('[name$="Total_Assets_scr"]').val(sum); } /// <summary> Page 26 /// Need to analyse the rows in Assets_rpt and see if there are any entries for each 'Held' choice: /// If any "Personally", BUT not "Real Estate, Home', then HasAssetsPersonally_scr = 'true' /// If any "Spouse's Name" , BUT not "Real Estate, Home', then HasAssetsSpouse_scr = 'true' /// If any "Jointly with spouse" OR "Jointly with other" , BUT not "Real Estate, Home', then HasAssetsJoint_scr = 'true' /// If any "Tenants in common with spouse" OR "Tenants in common with other" , BUT not "Real Estate, Home', then HasAssetsTIC_scr = 'true' /// If any "Do not know", BUT not "Real Estate, Home', then HasAssetsDontKnow_scr = 'true' /// </summary> function AssetAnalysis(){ $('#SelectedForm').find('[name$="HasAssetsPersonally_scr"]').val('false'); $('#SelectedForm').find('[name$="HasAssetsSpouse_scr"]').val('false'); $('#SelectedForm').find('[name$="HasAssetsJoint_scr"]').val('false'); $('#SelectedForm').find('[name$="HasAssetsTIC_scr"]').val('false'); $('#SelectedForm').find('[name$="HasAssetsDontKnow_scr"]').val('false'); $('.repeat[name*="Assets_rpt"]').each(function() { if( $(this).find('[name$="Asset_Held_cho"]').val() == "Personally" && $(this).find('[name$="Asset_Type_cho"]').val() != "Real Estate, Home"){ $('#SelectedForm').find('[name$="HasAssetsPersonally_scr"]').val('true'); } }); $('.repeat[name*="Assets_rpt"]').each(function() { if( $(this).find('[name$="Asset_Held_cho"]').val() == "Solely by Spouse" && $(this).find('[name$="Asset_Type_cho"]').val() != "Real Estate, Home"){ $('#SelectedForm').find('[name$="HasAssetsSpouse_scr"]').val('true'); Page 27 } }); $('.repeat[name*="Assets_rpt"]').each(function() { if( $(this).find('[name$="Asset_Held_cho"]').val() == "Jointly with spouse" && $(this).find('[name$="Asset_Type_cho"]').val() != "Real Estate, Home"){ $('#SelectedForm').find('[name$="HasAssetsJoint_scr"]').val('true'); } }); $('.repeat[name*="Assets_rpt"]').each(function() { if( $(this).find('[name$="Asset_Held_cho"]').val() == "Tenant in common with spouse" && $(this).find('[name$="Asset_Type_cho"]').val() != "Real Estate, Home" || $(this).find('[name$="Asset_Held_cho"]').val() == "Tenant in common with other" && $(this).find('[name$="Asset_Type_cho"]').val() != "Real Estate, Home"){ $('#SelectedForm').find('[name$="HasAssetsTIC_scr"]').val('true'); } }); $('.repeat[name*="Assets_rpt"]').each(function() { if( $(this).find('[name$="Asset_Held_cho"]').val() == "Do not know" && $(this).find('[name$="Asset_Type_cho"]').val() != "Real Estate, Home"){ $('#SelectedForm').find('[name$="HasAssetsDontKnow_scr"]').val('true'); } }); } 23 Data binding With this function you will be able to load the entries within a repeat into a dropdown list on another page: function Before_Proprietor_rpt_FormInit(){ Page 28 var cbo = $('[name$="Principal_Name_scr"]'); var options = ''; if(typeof mydata !== 'undefined' ){ if(typeof mydata.Principal_rpt !== 'undefined'){ $.each(mydata.Principal_rpt, function (i, prin) { var name = ''; if(typeof prin.Principal_Name_scr !== 'undefined' && prin.Principal_Name_scr !== null) name = prin.Principal_Name_scr; options += '<option value="' + name + '">' + name + '</option>\n\n'; }); } } $(cbo).empty(); $(cbo).append($(options)); }; 24 Resources 24.1 Shortcuts for script editor https://github.com/ajaxorg/ace/wiki/Default-Keyboard-Shortcuts 24.2 Syntax http://www.csharp-examples.net/dataview-rowfilter/ 24.3 Overview of HTML, CSS and JavaScript http://htmldog.com/ Page 29
© Copyright 2024 Paperzz