Skip to main content

๐ŸŽ› Conditionally Show or Hide Related Events

It's easy to use Conditional Logic in Slate forms to control display of an entire Related Events Selector, but what if you want to hide only some events? Unfortunately, the built-in event filters (date range and folders) are rather limited, and going beyond those basic filters requires JavaScript. The examples below may help get you started.

Suppose admissions counselors are working a call list and want to easily schedule follow-up appointments without leaving the call list form. Assuming each scheduler slot has the counselor's name in the title, you can use a form merge field to pass the counselor's name into the form (where you can access it in JavaScript). Then you can filter the related events based on title.

Set up the merge field export:

user short name export.png

Set up a hidden field to capture the contents of the merge field with a calculation formula:

use calc name.png

Use a script to filter the related events:

//Find hidden related event selector options
var $current_user_first = Form.getValue('current_user_first');
var $rel_selector = '[data-export="schedule_appointment"]';

// Find related events and run a function for each one
$($rel_selector + ' input').each(function (i, el) {
ย  ย  var elem = $(el);

ย  ย  // Remove each related event that doesn't have the current user first name in the description
ย  ย  if (elem.val().indexOf($current_user_first) == -1) {
ย  ย  ย  ย  elem.parent().remove();
ย  ย  }

});

// Replace related event selector with error message if no options remain
if ($($rel_selector + 'input').length == 0) {
ย  ย  $($rel_selector).text('No appointment slots found with your name in the title!');
}

It's easy to show related events one or more days after the main event, but what if you want to show only related events one and a half hours after the main event?

// Setup
var event_timezone = '-0400'; // UTC offset; Eastern Daylight Time is -0400
var r_export_key = 'related_event'; // Export key of related events field
var target_offset = 1.5; // Hours after main event

Date.prototype.addHours = function (h) {
ย  ย  this.setTime(this.getTime() + (h * 60 * 60 * 1000));
ย  ย  return this;
};

var form_date = FW.parseDateTime($('#form_date').val() + event_timezone);
var target_date = form_date.addHours(1.5);

// Find related events and remove all that don't match the target time
$('[data-export="' + r_export_key + '"] input').each(function (i, el) {
ย  ย  var elem = $(el);

ย  ย  // Painfully transform the related event timestamp into a format JS can parse
ย  ย  r_datestring = elem.val().split(',')[4] // Split on commas, select 5th element
ย  ย  r_datestring = r_datestring.split('-')[0].trim(); // Split on dash, select first element, trim spaces
ย  ย  r_array = r_datestring.split(" at ");
ย  ย  r_array[0] = r_array[0].split(" ");
ย  ย  r_array[0][0] = r_array[0][0].slice(0, 3); // Shorten month
ย  ย  r_array[0] = r_array[0].join(' ') + ' ' + form_date.getFullYear(); // Collapse and add year
ย  ย  r_datestring = r_array.join(' ') + ' GMT' + event_timezone; // Collapse and add timezone

ย  ย  r_date = FW.parseDateTime(r_datestring);
ย  ย  if (r_date.valueOf() != target_date.valueOf()) {
ย  ย  ย  ย  elem.parent().remove();
ย  ย  }
});