DayPilot Scheduler数据量大渲染缓慢,如何实现房间数据分块加载?
Dealing with large datasets in DayPilot Scheduler can absolutely grind rendering to a halt—chunked loading is the perfect fix here. Let’s walk through how to implement this for your room resources step by step:
1. Set Up Scheduler for Chunked Loading
First, initialize your scheduler with virtual scrolling (to only render visible rooms) and set up a scroll handler to trigger loading more chunks as the user scrolls. We’ll start with an empty resource list and load the first chunk on initialization:
const dp = new DayPilot.Scheduler("dp", { height: "100vh", virtualScroll: true, // Critical: only renders visible resources timeRangeSelectedHandling: "Enabled", // Keep your existing config here... // Handle scroll events to load more rooms onScroll: async (args) => { // Trigger load when we're 50px from the bottom of the resource list const isNearBottom = args.scrollTop + args.viewportHeight >= args.scrollHeight - 50; if (isNearBottom) { await loadNextRoomChunk(); } } }); // Load first chunk on init await loadNextRoomChunk(); dp.init();
2. Implement the Chunk Loading Logic
Create a function to fetch paginated room data from your backend, handle loading states, and append new rooms to the scheduler. We’ll add a loading indicator to keep users informed:
let currentPage = 0; const chunkSize = 20; // Adjust based on what feels smooth for your UI let isLoading = false; // Prevent duplicate load triggers async function loadNextRoomChunk() { if (isLoading) return; isLoading = true; // Add a temporary "Loading..." resource dp.resources.add({ id: "loading", name: "Loading more rooms..." }); try { // Fetch paginated rooms from your API const response = await fetch(`/api/rooms?page=${++currentPage}&size=${chunkSize}`); const newRooms = await response.json(); // Remove loading indicator dp.resources.remove("loading"); // Append new rooms to the scheduler dp.resources.add(newRooms); // Stop loading if we've fetched all rooms if (newRooms.length < chunkSize) { dp.message("All rooms loaded"); } } catch (err) { console.error("Failed to load rooms:", err); dp.resources.remove("loading"); dp.message("Error loading rooms—please try again"); } finally { isLoading = false; } }
3. Backend Requirements
Make sure your backend endpoint returns room data in DayPilot’s resource format—each room object needs at least an id and name field. Example response:
[ { "id": "room1", "name": "Conference Room A" }, { "id": "room2", "name": "Conference Room B" } ]
4. Optional: Add a "Load More" Button
If you prefer explicit loading over scroll-triggered, replace the scroll handler with a button click event:
<button id="loadMoreBtn">Load More Rooms</button>
document.getElementById("loadMoreBtn").addEventListener("click", loadNextRoomChunk);
Visual Example
Here’s how the chunked loading looks in action:
内容的提问来源于stack exchange,提问作者Vitaly

