
| Demo | |
|---|---|
| Downloads | |
| Languages | JavaScript |
Overview
-
The JavaScript Scheduler component supports several standard time header group sizes.
-
If you need to display special groups, you can use the time header customization API to insert custom segments using active areas.
-
Includes a trial version of DayPilot Pro for JavaScript (see License below)
License
Licensed for testing and evaluation purposes. Please see the license agreement included in the sample project. You can use the source code of the tutorial if you are a licensed user of DayPilot Pro for JavaScript.
Scheduler Time Header Groups
The JavaScript Scheduler component supports the following time header groups:
-
Hour
-
Day
-
Week
-
Month
-
Quarter
-
Year
-
None
-
Cell
The time header group can be set using the groupBy property of the time header row object. The time headers are specified using the timeHeaders Scheduler property.
The following example displays three rows. The first row displays the month name, the second row displays the week number and the third row displays the day of month.
For this standard layout, set the middle row's groupBy to "Week". Keep the first row grouped by "Month" and the last row grouped by "Day", with format: "d" to display day numbers.
We will modify the second row to split the timeline into alternating workweek and weekend segments.
Let’s start with changing the group size of the second row to "None", which means the whole row will display only one cell.
const dp = new DayPilot.Scheduler("dp", {
timeHeaders: [
{groupBy: "Month"},
{groupBy: "None"},
{groupBy: "Day", format: "d"}
],
// ...
});
Custom Segments in the Scheduler Time Header
Now we will use the onBeforeTimeHeaderRender event to insert our own date segments using active areas. The active areas can be used to add additional information to the time header cells, action icons, or customize the appearance (see also JavaScript Scheduler Time Header Customization tutorial).
const dp = new DayPilot.Scheduler("dp", {
timeHeaders: [
{groupBy: "Month"},
{groupBy: "None"},
{groupBy: "Day", format: "d"}
],
// ...
onBeforeTimeHeaderRender: (args) => {
if (args.header.level === 1) {
args.header.text = "";
let start = args.header.start;
args.header.areas = [];
while (start < args.header.end) {
const dayOfWeek = start.getDayOfWeek();
let isWeekend;
let end;
if (dayOfWeek === 0) {
isWeekend = true;
end = start.addDays(1);
}
else if (dayOfWeek === 6) {
isWeekend = true;
end = start.addDays(2);
}
else {
isWeekend = false;
end = start.addDays(6 - dayOfWeek);
}
if (end > args.header.end) {
end = args.header.end;
}
args.header.areas.push({
start,
end,
top: 0,
bottom: 1,
backColor: isWeekend ? "#e0e0e0" : null,
horizontalAlignment: "center",
verticalAlignment: "center",
style: "box-sizing: border-box; border-right: 1px solid #ccc;",
text: isWeekend ? "Weekend" : "Week " + start.weekNumber()
});
start = end;
}
}
}
});
Header levels are zero-based, so level === 1 targets only the second row. With groupBy: "None", this row has a single cell spanning the timeline. Clear its default text and fill args.header.areas with date-based segments; the month and day rows stay unchanged.
Starting at the header's first date, check the day of the week: Sunday (0) needs a one-day weekend segment, Saturday (6) starts a two-day weekend, and a weekday starts a workweek segment ending on Saturday. Clamp each segment's end to args.header.end so it cannot extend past the visible month. Assigning start = end advances the loop without gaps or overlaps.
Each active area uses start and end dates to align with the day columns. The top and bottom offsets fill the header height, while the alignment properties center the label. Weekend areas get a gray background; workweek labels use the week number of the segment's first day. The border separates neighboring segments.
Full Source Code
The downloadable sample uses Vite and imports DayPilot from daypilot-pro-javascript. Run npm install and npm run dev to start it, or npm run build to create a production build. You can generate a starter project using DayPilot UI Builder.
This is the full src/app.js module of the Scheduler component with custom time header segments. The page loads it using a module script and provides the dp container. The sample uses the current month and 60-pixel day columns so even a single-day weekend at the month boundary can display its label; select a date range in a resource row to create an event. Events are stored in memory and are cleared when you reload the page.
import {DayPilot} from "daypilot-pro-javascript";
const dp = new DayPilot.Scheduler("dp", {
timeHeaders: [
{groupBy: "Month"},
{groupBy: "None"},
{groupBy: "Day", format: "d"}
],
scale: "Day",
cellWidth: 60,
days: DayPilot.Date.today().daysInMonth(),
startDate: DayPilot.Date.today().firstDayOfMonth(),
timeRangeSelectedHandling: "Enabled",
onTimeRangeSelected: async (args) => {
const modal = await DayPilot.Modal.prompt("Create a new event:", "Event 1");
dp.clearSelection();
if (modal.canceled || !modal.result) {
return;
}
dp.events.add({
start: args.start,
end: args.end,
id: DayPilot.guid(),
resource: args.resource,
text: modal.result
});
},
onBeforeTimeHeaderRender: (args) => {
if (args.header.level === 1) {
args.header.text = "";
let start = args.header.start;
args.header.areas = [];
while (start < args.header.end) {
const dayOfWeek = start.getDayOfWeek();
let isWeekend;
let end;
if (dayOfWeek === 0) {
isWeekend = true;
end = start.addDays(1);
}
else if (dayOfWeek === 6) {
isWeekend = true;
end = start.addDays(2);
}
else {
isWeekend = false;
end = start.addDays(6 - dayOfWeek);
}
if (end > args.header.end) {
end = args.header.end;
}
args.header.areas.push({
start,
end,
top: 0,
bottom: 1,
backColor: isWeekend ? "#e0e0e0" : null,
horizontalAlignment: "center",
verticalAlignment: "center",
style: "box-sizing: border-box; border-right: 1px solid #ccc;",
text: isWeekend ? "Weekend" : "Week " + start.weekNumber()
});
start = end;
}
}
}
});
dp.init();
const app = {
init() {
const resources = [
{name: "Resource 1", id: "R1"},
{name: "Resource 2", id: "R2"},
{name: "Resource 3", id: "R3"},
{name: "Resource 4", id: "R4"},
{name: "Resource 5", id: "R5"},
{name: "Resource 6", id: "R6"},
{name: "Resource 7", id: "R7"},
{name: "Resource 8", id: "R8"},
{name: "Resource 9", id: "R9"}
];
dp.update({resources, events: []});
}
};
app.init();
DayPilot




