- Regular Section
- Calculate a response from a preceding question responses
- Calculate a static default date response
- Calculate sum of several responses
- Calculate total by multiplying responses
- Calculate value from Cascading Select
- Calculate values selected in a multiselect question
- Calculate values selected in a multiselect question then concatenate into a string
- Compare pre populated date and manually entered date and use response in skip logic condition
- Display customer service support number based on country selection
- Calculate Age from an entered Date Of Birth
- Repeat Section
- Calculate a response from a preceding question responses
- Calculate a response by concatenating response from previous question in regular section
- Calculate responses in 2nd Repeat section with details from the 1st Repeat Section.
- Calculate response in repeat section by adding a value from a previous question in regular section
- Calculate a static default date response
- Calculate sum in a Regular section from responses in a Repeat section
- Calculate sum within repeating questions and then calculate grand total in a regular section
- Calculate Age from an entered Date Of Birth
1. Regular Section
-
- Calculate a response from a preceding question responses. Example: Generating a unique id by concatenating the registration date and a member ID passed via DDH
-
Q1 Caption: Registration Date
Q1 Name: registration_date
Q2 Caption: Member ID (automatically populated from the HDD)
Q2 Name: member_id
Q3 Caption: Unique member ID
Q3 Name: unique_member_id/*JavaScript code on Q3*/
var dt = tw.register.registration_date.value;
var timestamp = "" + dt.getFullYear() + dt.getDate() + (dt.getMonth() +1);
tw.register.unique_member_id.value = timestamp + "-" + tw.register.member_id.value.slice(tw.register.member_id.value.length - 3);
-
- Calculate a static default date response. Example: set date to 1/1/2017
-
-
Q1 Caption: Registration Date
Q1 name: registration_date/*JavaScript code on Q1*/
tw.register.registration_date.value = new Date(2017, 1, 1);
-
-
- Calculate sum of several responses
-
-
Q1 Caption: Number of Male Household Members
Q1 Name: number_of_male_household_members
Q2: Caption: Number of female Household Members
Q2: Name: number_of_female_household_members
Q3: Caption: Total number of Household Members
Q3: Name: total_members/*JavaScript code on Q3*/
tw.register.total_members.value=tw.register.number_of_male_household_members.value + tw.register.number_of_female_household_members.value;
-
-
- Calculate total by multiplying responses
-
-
Q1 Caption: How many fish do you catch per day?
Q1 Name: how_many_fish_do_you_catch_per_day
Q2 Caption: How many days do you fish?
Q2 Name: how_many_days_do_you_fish
Q3 Caption: How many kgs is each fish?
Q3 Name: how_many_kgs_is_each_fish
Q4 Caption: Total fish production volume
Q4 Name: total_fish_production_volume/*Javascript code on Q4*/
tw.client_id.total_fish_production_volume.value = tw.client_id.how_many_fish_do_you_catch_per_day.value * tw.client_id.how_many_days_do_you_fish.value * tw.client_id.how_many_kgs_is_each_fish.value;
-
-
- Calculate value from Cascading Select
Formula for getting a value from one of the columns. Example : With a section name as info and the cascading select name as select, then get the value for Place.
Syntax: tw.<section name>.<question name>["Column name"].value
-
Q1 Caption: Select Place
/*JS on Q1*/
tw.info.select["Place"].value
-
- Calculate values selected in a multiselect question
Accessing values of multi select
Syntax: tw.<section_name>.multi_select.value.length-
/*get count of selected answers*/
tw.section_name.multi_select.value[0]
/*access first selected answer*/
tw.section_name.multi_select.value[i]
/*access answer on i-th place (count from 0)*/
JS on Q1
Example :
tw.section_name.multi_select.value.length
/*Calculates the total number of record count*/
-
- Calculate values selected in a multiselect question then concatenate into a string
-
Section name: member_hobbies
Q1 Caption: Hobbies
Q1 Name: hobbies
Type: Multiselect
Q2 Caption: Selected Hobbies
Q2 Name: selected_hobbies
Type: Text
var hobbies = tw.member_hobbies.hobbies.value[0];
var i = 0;
for (i = 1; i < tw.member_hobbies.hobbies.value.length; i ++)
{
hobbies = hobbies + tw.member_hobbies.hobbies.value[i];
}
tw.member_hobbies.selected_hobbies.value = hobbies;
-
- Compare pre populated date and manually entered date and use response in skip logic condition.
-
Section name: building_details
Q1 Caption: Pre Populated Date
Q1 Name: pre_populated_date
Type: Text
Q2: Caption: Today's Date
Q2: Name: todays_date
Type: Date
Q3: Caption: View Date
Q3: Name: view_date
Type: Date
/*JS on Q3 to fetch the prepopulated date in same format as Today's Date*/
tw.building_details.view_date.value = new Date(tw.building_details.pre_populated_date.value);
Q4: Caption: Same Date?
Q4: Name: same_date
Type: Single Select with Options Yes/No
/*JS on Q4 to check if the prepopulated date Today's Date are the same*/
var same_day = "Yes";
if (tw.building_details.view_date.value.getTime() !== tw.building_details.todays_date.value.getTime())
{
same_day = "No";
}
tw.building_details.same_date.value = same_day;
-
- Displaying customer service support number based on country selection.
-
Section name: checkin_complete_1
Q1 Caption: Pre Populated Date
Q1 Name: pre_populated_date
Type: Picklist
Q2: Caption: Support Number
Q2: Name: Support Number
Type: Number
var pick = tw.checkin_complete_1.country.value;
if (pick == "Uganda")
{
tw.checkin_complete_1.support_number.value = "099209209";
}
else if (pick == "Kenya")
{
tw.checkin_complete_1.support_number.value = "099208888";
}
else if (pick == "Tanzania")
{
tw.checkin_complete_1.support_number.value = "099206666";
}
-
- Calculate Age from an entered Date Of Birth.
-
Section name: age_validation
Q1 Caption: What is the age of the patient?
Q1 Name: what_is_the_age_of_the_patient
Type: Free Text
var today = new Date();
var DOB = tw.age_validation.date_of_birth.value;
tw.age_validation.what_is_the_age_of_the_patient.value = today.getFullYear() - DOB.getFullYear();
-
2. Repeat Section
- Calculate a response from a preceding question responses. Example: Generating a unique id by concatenating the registration date and a member ID passed via DDH
-
-
-
Regular section:
Q1 Caption: Registration Date
Q1 Name: registration_date
Q2 Caption: Member ID (automatically populated from the HDD)
Q2 Name: member_idRepeat section:
Q3 Caption: Name of applicant
Q3 Name: name_of_applicant
Q4 Caption: Unique Member ID
Q4 Name: unique_member_id/*Javascript code on Q4*/
var dt = tw.register.registration_date.value;
/* just to have our timestamp formatting code a bit cleaner */
var timestamp = "" + dt.getFullYear() + dt.getDate() + (dt.getMonth() +1);
/* '"" +' is needed to treat */
/* year, day, month and time as strings not numbers so it actually concatenates those values instead of adding them */
var memberid = tw.register.member_id.value;
/* assigning tw.demographic.current.member_id.value to memberid variable so the code for taking last 3 chars can be a bit shorter */
var nameOfApplicant = tw.demographic.current.name_of_applicant.value.replace(/\s+/g, '_').toLowerCase();
/* replace whitespaces with dashes so name like "John Doe" will become "john_doe" */
tw.demographic.current.unique_member_id.value = timestamp + nameOfApplicant + "-" + memberId.slice(memberId.length - 3);
-
-
-
- Calculate a response by concatenating response from previous question in regular section. Example: Generate a unique id for each dependent generated by a repeating section by increasing the head of household id with each repeat.
-
-
Regular Section: Client ID
Q1 Caption: Head of household Unique ID
Q1 name: head_of_household_unique_id
Repeat Section: Demographic
Q2 Caption: Dependent Unique ID
Q2 name: dependent_unique_id/*Javascript code on Q2*/
tw.demographic.current.dependent_unique_id.value = tw.client_id.head_of_household_unique_id.value + "-" + (tw.demographic.currentIndex + 2);
-
-
-
Calculate responses in 2nd Repeat section with details from the 1st Repeat Section.
Example: Prepopulate 2nd Repeat section with children's names from the 1st Repeat Section.
First ask the names of children in repeat section and then later be able to populate each child's details in subsequent repeat sections.-
-
Repeat Section: children_names
Q1 Caption: Enter name of child
Q1 Name: enter_name_of_child
Repeat Section: children_details
Q2 Caption: Name
Q2 Name: name/*JavaScript code on Q2*/
var index = tw.children_details.currentIndex;
tw.children_details.current.name.value = tw.children_names[index].enter_name_of_child.value;
-
-
- Calculate response in repeat section by adding a value from a previous question in regular section.
Example: Pre-populate the last name of a dependent with the last name of the head of household previously gathered, but allow for editing in case the child/spouse has a different last name; dependent information is in a repeating section-
-
Regular Section: client_id
Q1 Caption: Head of household last name
Q1 Name: head_of_household_last_name
Repeat Section: demographic
Q2 Caption: Dependent last name
Q2 Name: dependent_last_name/*JavaScript code on Q2*/
tw.demographic.current.dependent_last_name.value = tw.client_id.head_of_household_last_name.value;
-
-
- Calculate a static default date response.
Example: Set a default value for a date question so that MUs don't waste time scrolling down such as set set of registration to 1/1/2017-
-
Repeat Section: registration
Q1 Caption: Date of registration
Q1 Name: date_of_registration/*JavaScript code on Q1*/
tw.registration.current.date_of_registration.value = new Date(2017, 0, 1);
-
-
- Calculate sum in a Regular section from responses in a Repeat section.
Example: Land use – sum up land of each crop collected during repeat section to display total land at the end.-
-
Repeat Section: crops_and_land
Q1 Caption: How much land do you use for crop?
Q1 Name: how_much_land_do_you_use_for_crop
Regular section: total_land
Q2 Caption: How much total land do you have?
Q2 Name: how_much_total_land_do_you_have/*JavaScript code on Q2*/
var totalLandForCrops = 0;
for (var i = 0; i < tw.crops_and_land.length; i++)
{
/* iterate over all repeated sections */
var currentcrop = tw.crops_and_land[i];
totalLandForCrops += currentcrop.how_much_land_do_you_use_for_crop.value;
}
tw.total_land.how_much_total_land_do_you_have.value = totalLandForCrops;
-
-
-
Calculate sum within repeating questions and then calculate grand total in a regular section. Example: Sales Invoice
Multiple the quantity times price of a line item, and then sum the line items. Each line item being a repeated series of questions.
-
-
Repeat section: line_item
Iteration 1
Q1 Caption: Select type of lamp
Q1 Name: select_type_of_lamp
Q2 Caption: Number of lamps sold
Q2 Name: number_of_lamps_sold
Q3 Caption: Cost per lamp (could be passed in or not)
Q3 Name: cost_per_lamp
Q4 Caption: Line total
Q4 Name: line_totalRegular Section: totals
Q5 Caption: Invoice total
Q5 Name: invoice_total/*JavaScript code on Q4*/
tw.line_item.current.line_total.value = tw.line_item.current.number_of_lamps_sold.value * tw.line_item.current.cost_per_lamp.value;/*JavaScript code on Q5*/
var total = 0;
for (var i = 0; i < tw.line_item.length ; i++)
{
var line_item = tw.line_item[i];
total += line_item.line_total.value;
}
tw.totals.invoice_total.value = total;
-
-
Calculate Age from an entered Date Of Birth
-
Section name: age_calculation
Q1 Caption: What is the age of the patient?
Q1 Name: what_is_the_age_of_the_patient
Type: Free Text
var today = new Date();
var DOB = tw.age_validation.date_of_birth.value;
tw.age_validation.current.what_is_the_age_of_the_patient.value = today.getFullYear() - DOB.getFullYear();
-
-
- Calculate a response from a preceding question responses. Example: Generating a unique id by concatenating the registration date and a member ID passed via DDH
Comments
0 comments
Please sign in to leave a comment.