Skip to main content

DHTMLX Booking. Card template example

Live example​

cardTemplate replaces the left block of a card with HTML of your own. The function receives the card object and returns a string, so fields can be reordered, merged, or drawn as new elements. This demo builds a round preview image, a five star rating, an experience line, and a Telemedicine badge.

function getPhotoElement(preview) {
    return preview
        ? `<div class="card-photo" style="background-image: url(${preview})"></div>`
        : `<div class="card-photo-empty"></div>`;
}

function getRatingElement(count) {
    return [...Array(5).keys()].map
    (
        i => `<i class="card-star${i + 1 > Math.round(count) ? "-disabled" : ""} wxi-star"></i>`
    )
    .join("");
}

function getConsultationElement(consultation) {
    return consultation ? `
        <div class="card-consultation">
            <svg xmlns="http://www.w3.org/2000/svg" width="8" height="8" viewBox="0 0 8 8" fill="none" class="card-consultation-icon">
                <circle cx="4" cy="4" r="4" fill="#00D19A"/>
            </svg>
            <span class="card-consultation-text" title="Online consultation available">Telemedicine</span>
        </div>
    ` : "";
}

function cardPreviewTemplate({ item }) {
    const [clinic, address] = item.details.split(".");
  	const subtitle = item.subtitle.split(" ").slice(0, 2).join(" ");
  
    return `
    <div class="custom-preview">
        <div class="card-container">
            <div class="card-photo-wrapper">
                ${getPhotoElement(item.preview, "card")}
            </div>
        
            <div class="card-content">
                <div class="card-header">
                    <div class="card-top-row">
                        <div class="card-category">${item.category}</div>
                        <div class="card-price">${item.price}</div>
                    </div>
                    <div class="card-title">${item.title}</div>
                </div>
                <div class="card-review-row">
                    <div class="card-review-count">
                        ${item.review.count}
                        reviews
                    </div>
                    <div class="card-rating">
                        ${getRatingElement(item.review.stars)}
                    </div>
                </div>
                <div class="card-info-row">
                    <div class="card-experience">Experience: ${subtitle}</div>
                    ${getConsultationElement(item.consultation)}
                </div>
            </div>
        </div>

        <div class="card-details">
            <span class="card-details-clinic">${clinic}.</span><span class="card-details-address">${address}</span>
        </div>
    </div>
    `
}

const { Booking, template } = booking;

const widget = new Booking("#root", {
	start: buildToday(),
    data: dataset,
    cardTemplate: template(item => cardPreviewTemplate(item)),
});

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>

<style>
.custom-preview {
        display: flex;
        flex-direction: column;
        width: 100%;
        height: 100%;
        gap: 16px;
        margin: 10px 0;
    }

    .card-container {
        display: flex;
        flex-direction: row;
        gap: 20px;
    }

    .card-photo-wrapper {
        width: auto;
        display: flex;
        flex-direction: column;
        gap: 4px;
    }

    .card-photo,
    .card-photo-empty {
        border-radius: 50%;
        overflow: hidden;
        background-repeat: no-repeat;
        background-position: center;
        background-size: cover;
        height: 100px;
        width: 100px;
    }

    .card-photo-empty {
        text-align: center;
        border: 1px solid;
        border-color: var(--wx-color-secondary-border-disabled);
        background-image: var(--empty-photo-bg-url);
    }

    .wx-size-tablet .card-image,
    .wx-size-mobile .card-image,
    .wx-size-mobile .card-image-empty {
        height: 80px;
        width: 80px;
    }

    /* content section */
    .card-content {
        display: flex;
        flex-direction: column;
        flex-grow: 1;
        overflow: hidden;
        line-height: var(--wx-line-height);
        gap: 8px;
        position: relative;
    }

    .card-content > div {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    /* header section */
    .card-header {
        display: flex;
        flex-direction: column;
    }

    .card-top-row {
        display: flex;
        justify-content: space-between;
    }

    .card-category {
        font-size: 14px;
    }

    .card-title, 
    .card-price {
        font-size: 16px;
        font-weight: var(--wx-font-weight-md);
    }

    .card-title {
        line-height: 24px;
    }

    .card-review-row {
        display: flex;
        align-items: center;
        height: 20px;
        gap: 8px;
    }

    .card-rating,
    .card-review-count {
        display: inline-block;
        text-align: center;
    }
    
    .card-review-count {
        font-size: 12px;
    }

    .card-star {
        color: var(--wx-color-warning);
    }

    .card-star-disabled {
        color: var(--wx-color-font-disabled);
    }

    .card-info-row {
        display: flex;
        align-items: center;
        gap: 12px;
    }

    .card-experience {
        font-size: 12px;
    }

	.card-consultation {
		display: flex;
		padding: 0 12px;
		font-size: 12px;
		align-items: center;
		gap: 4px;
		border-radius: 100px;
		border: 1px solid #DFDFDF;
		background: var(--wx-background-alt);
		max-width: 100%;
		overflow: hidden;
	}

	.card-consultation-icon {
		flex-shrink: 0;
	}

	.card-consultation-text {
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		min-width: 0;
	}

    .card-details-clinic {
        font-weight: var(--wx-font-weight-md);
    }
</style>

<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: "Silverstone Hospital. 225 Oak Rd, Miami, FL 33101",
            preview: "https://snippet.dhtmlx.com/codebase/data/booking/01/img/01.jpg",
            price: "$35",
            consultation: true,
            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: "Greenwood Medical Center. 102 Maple St, Denver, CO 80203",
            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 booking screen usually lives inside a product with its own visual language. The default card rarely matches it. A template lets the card carry a distance, a service level, a certification icon, or stars instead of a number. It also splits data that arrives in one field and belongs in two, the way this demo handles details. The function runs per card, so a record with no preview image renders a placeholder instead of a hole.

How to do this in your app​

  • Import the helper: const { Booking, template } = booking gives you the template helper the property expects.
  • Write the function: it takes { item }, the card record from data, and returns an HTML string. Formatting happens here, as with item.details.split(".") in the demo.
  • Pass it in: cardTemplate: template(item => cardPreviewTemplate(item)).
  • Style your markup: the class names in the string are yours, and the widget CSS variables such as --wx-color-warning are available for colors that should follow the theme.
  • Optional, carry your own fields: put them on the card record in data. This demo sets consultation: true on a card, and the template draws the badge whenever item.consultation is true.

Good to know​

  • The template overrides cardShape: once cardTemplate is set, it decides what appears on the card, so field switches are no longer the way to hide something.
  • Incomplete records need a fallback: getPhotoElement returns an empty circle when a card has no preview, which keeps rows the same height across the list.
  • The card object is passed as it is: fields Booking knows nothing about, such as consultation, are still on item and can be read inside the function.

FAQ​

How do you customize the appearance of a booking card?​

Assign cardTemplate a function wrapped in the template helper. It receives the card and returns HTML, which the widget renders in the left block of that card.

Can a card template display a field that Booking does not know?​

Yes. The whole record from data is passed to the function, so any extra property you store on a card is available inside the template.

Does a template disable the cardShape settings?​

Yes. When both are set, cardTemplate wins, and everything the card shows comes from your HTML.

API reference​

Additional resources​