Lazy Load Implementation
When dealing with very large datasets (e.g., millions of records), rendering all data at once can cause performance issues. Although StkTable has built-in virtual scrolling optimization, loading all data into memory at once still consumes significant resources.
By listening to the table's scroll event, you can get the startIndex and endIndex of the current visible area, enabling a lazy loading solution that loads data on demand.
Implementation Approach
- Create a large placeholder array and pass it to
dataSource - Listen to the
scrollevent to getstartIndexandendIndex - Calculate which data pages need to be loaded based on scroll position
- Fetch data from the API on demand and fill it into the corresponding positions
loading
Implementation Details
Core Logic
typescript
// Assume total data is 100,000 records, 100 per page
const totalCount = 100000;
const pageSize = 100;
// Create placeholder array
const tableData = ref<Array<Record<string, any>>>(
Array(totalCount).fill(null).map((_, i) => ({ id: i + 1, __placeholder: true }))
);
// Listen to scroll event
function onScroll(ev: Event, data: { startIndex: number; endIndex: number }) {
const { startIndex, endIndex } = data;
// Calculate the page range to load
const startPage = Math.floor(startIndex / pageSize);
const endPage = Math.floor(endIndex / pageSize);
// Load data pages within range
for (let page = startPage; page <= endPage; page++) {
loadDataPage(page);
}
}
// Load data for a specific page
async function loadDataPage(page: number) {
// Check if already loaded
const startIndex = page * pageSize;
if (!tableData.value[startIndex]?.__placeholder) return;
// Fetch data from API
const response = await fetchData(page, pageSize);
// Fill into corresponding positions
response.forEach((item, index) => {
tableData.value[startIndex + index] = item;
});
}Boundary Case Handling
When the scroll position is exactly between two pages (e.g., startIndex=95, endIndex=105), you need to load both pages simultaneously (page 0 and page 1) to ensure complete data display in the visible area.
Important Notes
- The placeholder array must be pre-allocated to ensure virtual scrolling can correctly calculate scroll height
- Loaded data should be cached properly to avoid duplicate requests
- Consider adding loading state indicators to improve user experience