- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2023 04:24 AM
Hi All,
I have written a catalog client script to hide the sections and it is working fine but the script is not that efficient.
Can someone please help me if I can use for loop and make the code efficient? If yes, then how?
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
//Type appropriate comment here, and begin script below
if (newValue == 'travel_bookings') {
//The following four fields are in the Cancel Travel section making them to non mandatory so that we can hide the section
g_form.setMandatory('program_name', 'false');
g_form.setMandatory('session_code_cxxxx_or_block_1_or_2_as_per_your_learning_invite', 'false');
g_form.setMandatory('booking_number_as_locat', 'false');
g_form.setMandatory('reason_for_cancellation', 'false');
g_form.setDisplay('travel_cancellation', 'false');
//Making the Travel Details section visible and making the fields mandatory.
g_form.setDisplay('travel_details', 'true');
g_form.setMandatory('program', 'true');
g_form.setMandatory('session_as_per_your_learning_invite', 'true');
g_form.setMandatory('session_location_as_per_learning_invite', 'true');
g_form.setMandatory('session_date', 'true');
g_form.setMandatory('gender', 'true');
g_form.setMandatory('title_mr_mrs_miss_other_if_other_enter_your_legal_title', 'true');
g_form.setMandatory('first_name_legal_name_as_shown_on_drivers_licence_or_passport', 'true');
g_form.setMandatory('surname_legal_name_as_shown_on_drivers_licence_or_passport', 'true');
g_form.setMandatory('over_18_years', 'true');
g_form.setMandatory('email_address', 'true');
g_form.setMandatory('mobile_number_whilst_travelling', 'true');
g_form.setMandatory('will_you_be_driving_to_attend', 'true');
g_form.setMandatory('are_flights_required', 'true');
g_form.setMandatory('is_accommodation_required', 'true');
g_form.setMandatory('is_an_additional_nights_accommodation_required_due_to_travel_time_back_from_training_location', 'true');
g_form.setMandatory('is_accommodation_required_a_day_prior_to_program_commencing_due_to_travel_time_to_training_location', 'true');
}
//Another Section
if (newValue == 'cancel_travel') {
//The following fields are in the Travel Details section making them to non mandatory so that we can hide the section
g_form.setMandatory('program', 'false');
g_form.setMandatory('session_as_per_your_learning_invite', 'false');
g_form.setMandatory('session_location_as_per_learning_invite', 'false');
g_form.setMandatory('session_date', 'false');
g_form.setMandatory('gender', 'false');
g_form.setMandatory('title_mr_mrs_miss_other_if_other_enter_your_legal_title', 'false');
g_form.setMandatory('first_name_legal_name_as_shown_on_drivers_licence_or_passport', 'false');
g_form.setMandatory('surname_legal_name_as_shown_on_drivers_licence_or_passport', 'false');
g_form.setMandatory('over_18_years', 'false');
g_form.setMandatory('email_address', 'false');
g_form.setMandatory('mobile_number_whilst_travelling', 'false');
g_form.setMandatory('will_you_be_driving_to_attend', 'false');
g_form.setMandatory('are_flights_required', 'false');
g_form.setMandatory('is_accommodation_required', 'false');
g_form.setMandatory('is_an_additional_nights_accommodation_required_due_to_travel_time_back_from_training_location', 'false');
g_form.setMandatory('is_accommodation_required_a_day_prior_to_program_commencing_due_to_travel_time_to_training_location', 'false');
g_form.setDisplay('travel_details', 'false');
/Making the Cancel Travel section visible and making the fields mandatory.
g_form.setDisplay('travel_cancellation', 'true');
g_form.setMandatory('program_name', 'true');
g_form.setMandatory('session_code_cxxxx_or_block_1_or_2_as_per_your_learning_invite', 'true');
g_form.setMandatory('booking_number_as_locat', 'true');
g_form.setMandatory('reason_for_cancellation', 'true');
}
//If None is selected in the select box, then hide both the sections and make all the mandaotry fields to non mandaotry
if (newValue == '') {
g_form.setMandatory('program_name', 'false');
g_form.setMandatory('session_code_cxxxx_or_block_1_or_2_as_per_your_learning_invite', 'false');
g_form.setMandatory('booking_number_as_locat', 'false');
g_form.setMandatory('reason_for_cancellation', 'false');
g_form.setMandatory('program', 'false');
g_form.setMandatory('session_as_per_your_learning_invite', 'false');
g_form.setMandatory('session_location_as_per_learning_invite', 'false');
g_form.setMandatory('session_date', 'false');
g_form.setMandatory('gender', 'false');
g_form.setMandatory('title_mr_mrs_miss_other_if_other_enter_your_legal_title', 'false');
g_form.setMandatory('first_name_legal_name_as_shown_on_drivers_licence_or_passport', 'false');
g_form.setMandatory('surname_legal_name_as_shown_on_drivers_licence_or_passport', 'false');
g_form.setMandatory('over_18_years', 'false');
g_form.setMandatory('email_address', 'false');
g_form.setMandatory('mobile_number_whilst_travelling', 'false');
g_form.setMandatory('will_you_be_driving_to_attend', 'false');
g_form.setMandatory('are_flights_required', 'false');
g_form.setMandatory('is_accommodation_required', 'false');
g_form.setMandatory('is_an_additional_nights_accommodation_required_due_to_travel_time_back_from_training_location', 'false');
g_form.setMandatory('is_accommodation_required_a_day_prior_to_program_commencing_due_to_travel_time_to_training_location', 'false');
g_form.setDisplay('travel_details', 'false');
g_form.setDisplay('travel_cancellation', 'false');
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2023 09:01 AM
Hi @Rooma1 ,
You can leverage functions and built-in array methods. I haven't fully tested and I didn't give each array the full set of fields as I think it will be a good exercise for you to do.
But give this a try.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
//create an array of the necessary fields or for each section
//This will be used for both looping and determining which fields to make mandatory or not and or show/hide
var cancelTravelSection = ['program_name','session_code_cxxxx_or_block_1_or_2_as_per_your_learning_invite','booking_number_as_locat','reason_for_cancellation']
var travelDetailsSection = ['program','session_as_per_your_learning_invite','session_location_as_per_learning_invite','session_date']
//create a function to set the fields/sections attribute
function settings(trueFalse){
return function(field){
g_form.setMandatory(field, trueFalse);
}
}
//use the function to make the appropriate settings in the loop
//pass parameters
//@section : pass the section name as an argument
//@fields : pass the array of the appropriate fields
//@trueFalse : should they display and be mandatory
function configure(section, fields, trueFalse){
fields.forEach(settings(trueFalse));
g_form.setDisplay(section, trueFalse);
}
switch (newValue) {
case 'travel_bookings':
configure('travel_cancellation',cancelTravelSection, false); //configure Travel Cancellation section and fields
configure('travel_details', travelDetailsSection, true); //configure Travel Details section and fields
break;
case 'cancel_travel':
configure('travel_cancellation',cancelTravelSection, true); //configure Travel Cancellation section and fields
configure('travel_details', travelDetailsSection, false); //configure Travel Details section and fields
break;
default:
configure('travel_cancellation',cancelTravelSection, false); //configure Travel Cancellation section and fields
configure('travel_details', travelDetailsSection, false); //configure Travel Details section and fields
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2023 08:33 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2023 09:01 AM
Hi @Rooma1 ,
You can leverage functions and built-in array methods. I haven't fully tested and I didn't give each array the full set of fields as I think it will be a good exercise for you to do.
But give this a try.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
//create an array of the necessary fields or for each section
//This will be used for both looping and determining which fields to make mandatory or not and or show/hide
var cancelTravelSection = ['program_name','session_code_cxxxx_or_block_1_or_2_as_per_your_learning_invite','booking_number_as_locat','reason_for_cancellation']
var travelDetailsSection = ['program','session_as_per_your_learning_invite','session_location_as_per_learning_invite','session_date']
//create a function to set the fields/sections attribute
function settings(trueFalse){
return function(field){
g_form.setMandatory(field, trueFalse);
}
}
//use the function to make the appropriate settings in the loop
//pass parameters
//@section : pass the section name as an argument
//@fields : pass the array of the appropriate fields
//@trueFalse : should they display and be mandatory
function configure(section, fields, trueFalse){
fields.forEach(settings(trueFalse));
g_form.setDisplay(section, trueFalse);
}
switch (newValue) {
case 'travel_bookings':
configure('travel_cancellation',cancelTravelSection, false); //configure Travel Cancellation section and fields
configure('travel_details', travelDetailsSection, true); //configure Travel Details section and fields
break;
case 'cancel_travel':
configure('travel_cancellation',cancelTravelSection, true); //configure Travel Cancellation section and fields
configure('travel_details', travelDetailsSection, false); //configure Travel Details section and fields
break;
default:
configure('travel_cancellation',cancelTravelSection, false); //configure Travel Cancellation section and fields
configure('travel_details', travelDetailsSection, false); //configure Travel Details section and fields
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2023 09:50 AM
Hey @ChrisBurks,
Thanks for your suggestion. I have tried and tested the code and it is working perfectly fine.
The only thing i didn't understand is after call configure function and passing the parameter as Section, array of variables and true/false, how it is working? I didn't understand the setting function.
Can you please explain a bit?
Thanks,
Rooma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2023 10:41 AM
Sure thing.
In JavaScript arrays have some built-in methods to perform iterative loops. In this scenario the .forEach method is used. Works just like a for loop. It takes a function as an argument and it iterates through each item passing the item to the function.
However, in this case we also needed to pass another argument. Another feature in JavaScript for functions is that they can pass values to a given function as it's return (settings in this case).
In this scenario we are passing the "trueFalse" given to the callback settings. Then settings is passing the item (field) from the array to the anonymous function as the return.