Skip to main content

api.getCalendar()

PRO

The functionality is available in PRO Edition only

Description

Returns a calendar instance from the registry, or the global calendar

Usage

api.getCalendar(id?: TID): ICalendar | undefined;

Parameters

  • id — (optional) the id of an entry in the calendars registry

Returns

The method resolves the calendar in this order:

  • id provided and found in the registry — returns that calendar instance
  • id missing or not found — returns the global calendar
  • No global calendar set — returns undefined

Example

The example retrieves specific calendars by id and uses the global fallback:

<script>
import { Gantt } from "@svar-ui/svelte-gantt";
import { getData } from "../data";

const data = getData();

const calendars = [
{ id: "standard", name: "Standard", weekHours: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 } },
{ id: "weekends", name: "Weekends", weekHours: { saturday: 8, sunday: 8 } },
];
</script>

<Gantt
{calendars}
calendar="standard"
tasks={data.tasks}
init={(api) => {
const stdCalendar = api.getCalendar("standard"); // Calendar instance for "standard"
const wkCalendar = api.getCalendar("weekends"); // Calendar instance for "weekends"
const global = api.getCalendar(); // global (returns "standard" since calendar="standard")
}}
/>

The returned instance is a live calendar object from the underlying scheduler library.

note

The object returned by api.getCalendar() is a live calendar instance. If you modify it directly, Gantt does not detect the change automatically, reassign tasks to trigger recalculation.

Serialize a calendar instance

To capture the current state of a calendar including runtime-added rules, call serialize() on the returned instance:

const config = api.getCalendar("standard").serialize();

The inline global calendar (defined via a CalendarConfig object or true) doesn't appear in api.serialize({ data: "calendars" }) because it lives outside the registry. Use api.getCalendar().serialize() to capture it separately.


Related articles: