DHTMLX Booking. Initialization example
Live example
Booking turns a list of people or services and their working hours into a bookable schedule. The new booking.Booking("#root", { data, start }) constructor draws the card list, the search bar, the day picker, and the slot grid, and setConfirmHandler receives every slot the visitor confirms.
const widget = new booking.Booking("#root", {
start: buildToday(),
data: dataset,
});
widget.setConfirmHandler(function(event){
const { confirm } = event;
setTimeout(() => {
Math.random() < 0.5 ? confirm.error() : confirm.done();
}, 1000);
});<!-- component container -->
<div id="root" style="height:100%"></div>
<!-- dataset -->
<script>
function buildToday(){const params = new URLSearchParams(typeof location !== 'undefined' ? location.search : '');const fixed = params.get('fixedDate') || (typeof window !== 'undefined' && window.FIXED_DATE);return fixed ? new Date(fixed) : new Date();}
const dataset = [
{
id: "ee828b5d-a034-420c-889b-978840015d6a",
title: "Natalie Tyson",
category: "Therapist",
subtitle: "2 years of experience",
details: "Cleveland Clinic\n9500 Euclid Ave",
preview: "https://snippet.dhtmlx.com/codebase/data/booking/01/img/01.jpg",
price: "$35",
review: {
stars: 4,
count: 120,
},
slots: [
{
from: 9,
to: 20,
days: [1, 2, 3, 4, 5],
},
{
from: 10,
to: 18,
days: [6, 0],
},
],
},
{
id: "5c9b64ad-1830-4e5b-a5f9-8acea10706df",
title: "James Anderson",
category: "Allergist",
subtitle: "3 years of experience",
details: "UCLA Medical Center\n57 Westwood Plaza",
preview: "https://snippet.dhtmlx.com/codebase/data/booking/01/img/11.jpg",
price: "$30",
review: {
stars: 4,
count: 64,
},
slotSize: 45,
slotGap: 10,
slots: [
{
from: "9:15",
to: 17,
days: [1, 2, 3, 4, 5],
},
],
},
// ... 44 more items (see Live Editor for full data)
];
function getDate(addDays, hoursValue = 0, minutesValue = 0) {
const date = buildToday();
const secondsValue = 0; // round to minutes
const msValue = 0;
date.setDate(date.getDate() + addDays);
date.setHours(hoursValue, minutesValue, secondsValue, msValue);
return date.getTime();
}
</script>Use case
A clinic portal, a service center, an internal room and equipment desk: each screen shows who is free and when, then takes the request. One data array carries the whole page, so your app supplies records and handles the answer. Working hours live inside each card, which is how one specialist covers weekdays while another covers the weekend. The confirmation stays on your side: the handler gets the chosen slot and the filled form, then reports the result with confirm.done() or confirm.error(). Swap the demo timeout for a request to your own booking endpoint and the screen is production work.
How to do this in your app
- Add a container with a height:
<div id="root" style="height:100%"></div>. The widget fills the element it is given, so the parent needs a height as well. - Create the widget:
new booking.Booking("#root", { data, start })takes the container selector and a config object. - Describe each card:
idandtitleare required.category,subtitle,details,preview,price, andreviewfill in the rest of the card. - Set the working hours: every rule in a card's
slotsarray takesfromandto, as a number of hours (9) or an"H:mm"string ("9:15"), plus an optionaldayslist from 0 for Sunday to 6 for Saturday. - Answer the confirmation:
setConfirmHandler(fn)runs when a visitor confirms a slot. The callback receives{ slot, data, confirm }, wheredatais the filled form. Callconfirm.done()once your server accepts the reservation, orconfirm.error()when it refuses. - Optional, choose the first day:
starttakes aDateand defaults to the current date.
Good to know
- The narrower rule wins: the widget defaults to 60-minute slots with no gap.
slotSizeandslotGapon a card override those defaults, andsizeandgapinside aslotsrule override the card. Natalie Tyson keeps the default. James Anderson uses 45 minutes with a 10-minute gap. - Overlapping rules cancel a day: when two rules cover the same day, their
from–toranges must not overlap, or the widget drops every slot for that day. - Booked times are data, not state: pass
usedSlotswith the timestamps of the slots you already sold, and those slots disappear from the grid. - The demo answers for the server: it waits one second and picks success or failure at random, so both results of the booking dialog are visible.
FAQ
How do you create a booking widget in JavaScript?
Include booking.js and booking.css, add a container element with a height, and call new booking.Booking("#root", { data }). The constructor takes the container and a config object, and the widget renders as soon as it has cards to show.
How do you set different working hours for different days?
Add several objects to the card's slots array and give each one a days list, where 0 is Sunday and 6 is Saturday. A rule with no days applies to every day that no other rule claims.
Where are confirmed bookings stored?
confirm.done() hides the slot for the rest of the session by adding its start time to usedSlots. Booking does not save that change. setConfirmHandler passes the slot and the filled form to your code, so you send them to your endpoint and report the result with confirm.done() or confirm.error(). On the next load, include the sold timestamp in usedSlots, or the slot is free again.
API reference
- Initialization guide: Including the source files and creating the widget
- data: Card fields, slot rules, used and available slots
- setConfirmHandler(): Handling a confirmed slot and reporting the result
- start: The first date the widget shows slots for
Related examples
- Start and end dates: limit the bookable period to a fixed range
- Slot duration and gap: set one slot length and one break for every card
- Backend integration: load cards from a server and save reservations back