FreeGridv1.15.1한국어

Server data and infinite scroll

Load rows from your server in blocks as people scroll with a datasource; sorting, filters and the quick search go along, with infinite scroll or pages.

Live demoPages & server data

Page through rows, or load them from your server in blocks as people scroll.

Code for this demo
import { createGrid, type ColumnDef, type GridApi, type GridOptions, type Row, type ServerParams } from '@freegridjs/core';
import { makeRows } from '../data';
import { button, separator, text, tr, type DemoContext } from '../ui';

type Mode = 'pages' | 'server' | 'infinite' | 'serverPages';

/** A pretend server in the browser: sorts, filters and answers after a short delay. */
function fakeServer(rows: Row[], log: (m: string) => void, knowsTotal = true) {
  return {
    getRows(p: ServerParams): Promise<{ rows: Row[]; total?: number }> {
      let list = rows;
      const q = p.quickFilter.trim().toLowerCase();
      if (q) list = list.filter(r => `${r.name} ${r.company} ${r.country}`.toLowerCase().includes(q));
      const s = p.sort[0];
      if (s) {
        const dir = s.dir === 'desc' ? -1 : 1;
        list = [...list].sort((a, b) => (a[s.colId] < b[s.colId] ? -dir : a[s.colId] > b[s.colId] ? dir : 0));
      }
      log(`getRows → ${p.start}–${p.end}${s ? ` sort ${s.colId} ${s.dir}` : ''}${q ? ` "${q}"` : ''}`);
      const page = list.slice(p.start, p.end);
      return new Promise(resolve => setTimeout(() => resolve(knowsTotal ? { rows: page, total: list.length } : { rows: page }), 350));
    },
  };
}

export default function mount(ctx: DemoContext) {
  const { lang } = ctx;
  const h = (en: string, ko: string) => tr(lang, { en, ko });
  const columns: ColumnDef[] = [
    { field: 'id', header: 'ID', type: 'number', width: 90 },
    { field: 'name', header: h('Name', '이름'), width: 170 },
    { field: 'company', header: h('Company', '회사'), width: 150 },
    { field: 'country', header: h('Country', '국가'), width: 150, filter: false },
    { field: 'total', header: h('Total', '합계'), type: 'number', format: '#,##0', width: 130 },
    { field: 'joined', header: h('Joined', '가입일'), type: 'date', width: 130 },
  ];
  const big = makeRows(100000);

  let grid: GridApi | null = null;
  const show = (mode: Mode) => {
    grid?.destroy();
    const options: Partial<GridOptions> =
      mode === 'pages' ? { data: makeRows(1000), pagination: { pageSize: 50, pageSizes: [20, 50, 100] } }
        : mode === 'server' ? { datasource: fakeServer(big, ctx.log), blockSize: 100 }
          : mode === 'infinite' ? { datasource: fakeServer(big, ctx.log, false), blockSize: 100 }
            : { datasource: fakeServer(big, ctx.log), pagination: { pageSize: 50, pageSizes: [20, 50, 100] } };
    // the pretend server only understands sorting and the quick search
    const cols = mode === 'pages' ? columns : columns.map(c => ({ ...c, filter: false as const }));
    grid = createGrid(ctx.host, { columns: cols, locale: lang, theme: 'erp', statusBar: true, ...options });
    grid.on('pageChange', e => ctx.log(`pageChange → ${e.page + 1} / ${e.pageCount}`));
    grid.on('rowsLoaded', e => ctx.log(`rowsLoaded → ${e.start}–${e.end}${e.total !== undefined ? ` of ${e.total}` : ''}`));
  };

  text(ctx, h('The server modes fetch 100 rows at a time from a pretend server with a short delay. Scroll fast to see the placeholders.',
    '서버 모드는 지연이 있는 가짜 서버에서 100행씩 받아 옵니다. 빠르게 스크롤하면 불러오는 동안의 자리 표시를 볼 수 있습니다.'));
  separator(ctx);
  button(ctx, h('Pages (1,000 rows in the browser)', '페이지 (브라우저의 1,000행)'), () => show('pages'));
  button(ctx, h('Server, 100,000 rows', '서버, 10만 행'), () => show('server'));
  button(ctx, h('Infinite scroll', '무한 스크롤'), () => show('infinite'));
  button(ctx, h('Server pages', '서버 페이지'), () => show('serverPages'));
  separator(ctx);
  button(ctx, h('Search "Kim" on the server', '서버에서 "Kim" 검색'), () => grid?.setQuickFilter('Kim'));
  button(ctx, h('Clear search', '검색 지우기'), () => grid?.setQuickFilter(''));
  button(ctx, h('Reload rows', '다시 불러오기'), () => grid?.refreshRows());
  show('pages');
  return grid;
}

When the data lives on a server, pass a datasource instead of data. The grid asks getRows only for the rows on screen, in blocks of blockSize, keeps up to maxBlocks blocks in memory and draws grey placeholders until rows arrive. Sorting, column filters and the quick search are sent along so the server can apply them, and every change asks again from the top.

Return total when you know it, and the scrollbar has its final size from the start. Leave it out for infinite scroll: the grid keeps loading as people scroll until a block comes back short. Add pagination as well and every page becomes one request. refreshRows() drops the cached blocks and asks again, for example after a save.

// Rows from your server, 100 at a time, as people scroll
createGrid(el, {
  columns,
  datasource: {
    async getRows({ start, end, sort, filters, quickFilter }) {
      const res = await fetch('/api/orders?' + new URLSearchParams({
        start, end, sort: JSON.stringify(sort), filters: JSON.stringify(filters), q: quickFilter,
      }));
      const { rows, total } = await res.json();
      return { rows, total };        // leave out total for infinite scroll
    },
  },
  blockSize: 100,                    // rows per request
  maxBlocks: 100,                    // blocks kept in memory
  loadingOverlay: 'Loading orders…',
  // pagination: { pageSize: 50 },   // add this for server-side pages
});

grid.refreshRows();                  // drop the cached rows and ask again
grid.on('rowsLoadFailed', e => console.error(e.error));

Changes on the server

applyTransaction updates, adds and removes server rows in place, and refreshRows({ keepRows: true }) reloads without blanking. See:

What getRows receives

NameTypeDescription
start / endnumberRows [start, end) of the current sort and filters.
sort{ colId, dir }[]Sort keys in order.
filtersRecord<colId, FilterModel>Column filters, as getFilterModel() returns them.
quickFilterstringThe quick search text; every word must match some column.
→ { rows, total? }PromiseWhat getRows returns: the rows and, when known, the row count. Without total the grid keeps asking until a block comes back short (infinite scroll).

rowsLoaded and rowsLoadFailed report each block, and loadingOverlay sets what shows while the first rows load. Adding or removing rows works only with data in the browser.

Only the loaded blocks are in the browser, so selectAll(), getSelectedRows() and copying a range cover the loaded rows. Copying never requests the missing blocks; it copies what is loaded and warns in the console how many rows of the range were left out. A cell being edited when the rows reload (a sort, a filter or refreshRows()) is saved to its row first, and the editor closes.

For ready-made server code in Java, Node.js, Python, PHP, C# and Go that reads these requests, see Server examples; for groups that are counted on the server, see Server-side grouping.