Skip to main content

DHTMLX Booking. Lazy rendering example

Live example​

renderType decides how many cards the widget puts into the page. Set to "lazy", it renders only the cards currently on screen, which is what keeps a very long list responsive. This demo starts with a thousand cards and reloads at 10 000, 20 000, or 50 000 from a select next to the widget.

const select = document.querySelector("#form-select");
const labels = ["1K", "10K", "20K", "50K"];
const dataSizes = [1000, 10000, 20000, 50000];

// generate options for the select control
dataSizes.forEach((id, i) => {
    const opt = document.createElement("option");
    opt.value = id;
    opt.innerHTML = labels[i];
    select.appendChild(opt);
});

function onConfirm(event) {
    const { confirm } = event;
    setTimeout(() => {
        Math.random() < 0.5 ? confirm.error() : confirm.done();
    }, 1000);
}

function generateAndLoadData(ind) {
    widget.setConfig({ data: getRandomCards(dataSizes[ind]) });
  	// setConfig rebuilds the widget, so the handler has to be attached again
    widget.setConfirmHandler(onConfirm);
}



const widget = new booking.Booking("#root", {
	start: buildToday(),
    data: getRandomCards(1000),
    renderType: "lazy"
});

widget.setConfirmHandler(onConfirm);
<!-- auxiliary controls for interacting with the sample -->
<link rel="stylesheet" href="https://snippet.dhtmlx.com/codebase/assets/css/auxiliary_controls.css">

<div style="display: flex;flex-direction: column; width: 100%; height: 100%;">
    <div class="dhx_sample-controls"style="display:flex;background: #fff;height:150px;">
    	<div class="select" style="height:60px;">
            <label for="form-select" class="dhx_sample-label">Select dataset size:</label>
	        <select name="form-select" class="dhx_select dhx_sample-input" id="form-select" style="width: 220px;" onchange="generateAndLoadData(this.selectedIndex)"></select>
        </div>
    </div>

    <!-- component container -->
   <div id="root" style="height: calc(100% - 100px); width: 100%;"></div>
</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();}

    function getRandomCards(num) {
        const result = [];
        let currentStart = 8;
        num = Number(num);

        function getNextStart() {
            const slot = currentStart;
            currentStart = currentStart >= 15 ? 8 : currentStart + 1;
            return slot;
        }

        for (let i = 0; i < num; i++) {
            const slotStart = getNextStart();
            const card = {
                id: "card_" + i,
                title: "Item " + (i + 1),
                category: "Category " + ((i % 2) + 1),
                subtitle: "Subtitle",
                details: "Details",
                preview: "",
                price: "100",
                review: {
                    stars: 4,
                    count: 100,
                },
                slots: [
                    {
                        from: slotStart,
                        to: slotStart + 7,
                        size: 40,
                        gap: 20,
                        days: [1, 2, 3, 4, 5],
                    },
                    {
                        from: slotStart,
                        to: slotStart + 6,
                        size: 40,
                        gap: 20,
                        days: [6],
                    },
                ],
            };
            result.push(card);
        }

        return result;
    }
</script>

Use case​

Some catalogs are simply large. A hospital network lists every practitioner, a service company lists every technician, a shared resource pool lists every room and every machine in the plant. The data usually arrives as one export, so the widget receives the whole set and the browser has to survive it. Rendering the visible part is what makes the difference between a list that scrolls and a tab that freezes. It also pairs well with a search bar, since a person who types a name does not care how many cards sit below the fold.

How to do this in your app​

  • Turn it on: pass renderType: "lazy" to the constructor. The other value is "default", which renders every card that is loaded.
  • Load the records as usual: nothing about data changes, and the demo swaps whole sets with widget.setConfig({ data }). The call rebuilds the widget, so the demo sets the confirm handler again after each swap.
  • Give the container a height: the controls bar is 100px tall, the demo sizes the widget with calc(100% - 100px), and the widget fills the element it is mounted in.
  • Optional, narrow the set as well: loading one region or one branch cuts the payload, which rendering alone cannot do.

Good to know​

  • Lazy rendering is about the page, not the network: every loaded card is still in memory, so a 50 000 record response is still a 50 000 record response.
  • The search bar keeps working across the whole set: filterShape searches the loaded records, not only the ones currently drawn.
  • You can switch it later: setConfig({ renderType: "lazy" }) applies it to a running widget, since the method rebuilds it with the new configuration. Set the confirm handler again after that call, because the rebuild clears it. The open day, the selected slot, and the search text are cleared as well.

FAQ​

How do you handle thousands of cards in a booking widget?​

Set renderType: "lazy" so only the visible cards are rendered. The demo shows the same widget at 1 000 and at 50 000 records with no change to the data format.

Does lazy rendering make the data load faster?​

No. It reduces what the browser draws, not what the server sends. Combine it with loading a narrower set when the response itself is the problem.

What is the default render type?​

"default", which renders every card that has been loaded. That is the right choice for the short lists most deployments start with.

API reference​

  • renderType: Rendering all cards or only the visible ones
  • setConfig(): Swapping the records or the render type at runtime
  • data: The record shape a large set follows

Additional resources​