Rows n’ Columns Docs
Visit HomepagePricing
  • Introduction
  • License
  • Demos
  • Getting started
    • Installation
    • Spreadsheet state
    • Headless UI
    • Imperative Spreadsheet API
    • Examples
  • ⚙️Configuration
    • Features
      • Data validation
      • Formula evaluation
      • Real-time data
      • Cell editors
      • Cell renderer
      • Structured Cell Renderer
      • Theming
      • Styling
      • Context menu
      • Localisation
      • Undo/Redo
      • Conditional formatting
      • Named ranges
      • Structured references
        • Schema based tables and columns
        • Calculated columns
      • Basic filter or Excel AutoFilter
      • Charts
      • Embedded content
      • Calculate on-demand
      • Drag and Drop
      • Pivoting and Grouping (Coming soon)
      • Tokenizer
      • Lazy loading/Infinite scrolling
      • OpenAI/Chat GPT Integration
      • Search
      • Formula protection
      • Autofill
      • Export canvas as image
    • Components
      • Canvas Grid
      • Toolbar
      • Sheet Tabs
      • Sheet Switcher
      • Sheet Status
      • Range Selector
      • Formula Input
      • Selection Input
      • SheetSearch
      • NamedRangeEditor
      • DeleteSheetConfirmation
      • TableEditor
      • Cell Format Editor
      • Conditional Format Editor
      • Data Validation Editor
      • Insert Link Editor
      • Insert Image Editor
    • API
      • Cell Data
      • Sheets
      • SpreadsheetProvider
      • useSpreadsheet
      • Modules
      • SheetCell
    • Using Spreadsheet with NextJS
    • Keyboard shortcuts
  • Collaboration
    • Real time collaboration
    • Yjs Collaboration
    • Supabase realtime Collaboration
  • Charts
    • Charts
    • Custom charts
  • Excel and Google sheets
    • CSV
    • Excel
    • Google sheets (Coming soon)
  • Functions
    • Named functions
    • Array formulas
  • Data persistence
    • Server side data persistence
    • React Query integration
  • Specifications
    • Browser support
    • Third party licenses
  • Support
    • Contact support
    • Report bugs
    • Feature requests
Powered by GitBook
On this page
  • Hooks for Lazy loading
  • Usage

Was this helpful?

  1. Configuration
  2. Features

Lazy loading/Infinite scrolling

Load data based on the visible viewport of the grid

CanvasGrid invokes onViewPortChange callback whenever user scrolls or pans around the spreadsheet.

This can be used to load data for the next set of visible rows.

Do note that lazy loading can affect calculations if cell dependents are out of the viewport and not loaded initially.

We recommend lazy loading only if calculations are moved to the server side.

import { CanvasGrid } from "@rowsncolumns/spreadsheet"

const App = () => {
  const [ sheetData, onChangeSheetData ] = useState<SheetData<T>>({})
  <CanvasGrid
    onViewPortChange={(viewport: ViewPortProps) => {
      const {
        columnStartIndex, 
        columnStopIndex,
        rowStartIndex,
        rowStopIndex,
        visibleColumnStartIndex,
        visibleColumnStopIndex,
        visibleRowStartIndex,
        visibleRowStopIndex      
      } = viewport
      
      // Throttle fetch request
      // Move it out of the render loop, this is just an example
      throttle(
        fetch(`/rows?start=${rowStartIndex}&end=${rowStopIndex}`)
          .then(rowData => {
            // Append new row data
            onChangeSheetData(prev => {
              const newRowData = prev[sheetId]
                  .splice(rowStartIndex, rowStopIndex - rowStartIndex, ...rowData)
              return {
                ...prev,
                [sheetId]: newRowData
              }
            })
          })
      , 300)
    }}
  />
}

Hooks for Lazy loading

@rowsncolumns/spreadsheet-state exports the useAsyncDatasource hook to easily load an asynchronous paged data source

Usage

import { useSpreadsheetState, useAsyncDataSource } from '@rowsncolumnse/spreadsheet-state'
import { CircularLoader } from '@rowsncolumns/ui'

const App = () => {
  const locale = 'en-US'
  const [sheets, onChangeSheets] = useState<Sheet[]>(mockSheets);
  const [sheetData, onChangeSheetData] = useState<SheetData<CellData>>({});
  const { enqueueCalculation, activeSheetId } = useSpreadsheetState({
    sheets,
    sheetData,
    onChangeSheetData,
    onChangeSheets
  })
  const { onViewPortChange, isLoading } = useAsyncDatasource<CellData>({
    sheetId: activeSheetId,
    locale,
    onChangeSheetData,
    enqueueCalculation,
    getRowData: useCallback(
      async (sheetId, [rowStartIndex, rowStopIndex]) => {
        await new Promise((res) => setTimeout(res, 1000));
        return [
          null,
          {
            values: [
              null,
              {
                userEnteredValue: {
                  formulaValue: "=SUM(E11,4)",
                },
              },
            ],
          },
        ];
      },
      []
    ),
  });
  
  return (
    <>
      <CanvasGrid
        onViewPortChange={onViewPortChange}
      />
      {isLoading ? (
        <CircularLoader className="absolute left-1/2 bottom-1/2 z-10 bg-rnc-background/60 border-solid border-px border-rnc-border p-5 rounded-md" />
      ) : null}
  </>
  )
}
PreviousTokenizerNextOpenAI/Chat GPT Integration

Last updated 1 year ago

Was this helpful?

⚙️