FreeGridv1.15.1한국어

Column customization and saved views

People reorder, resize, hide, pin and autosize columns from the header and the column chooser; getColumnState and getState save each person's layout and view.

Live demoColumn customization

Show, hide, reorder, resize, autosize and reset columns; save the layout.

Code for this demo
import { createGrid } from '@freegridjs/core';
import { demoColumns, makeRows } from '../data';
import { button, separator, tr, type DemoContext } from '../ui';

const KEY = 'freegrid-demo-columns';

export default function mount(ctx: DemoContext) {
  const { lang } = ctx;
  const columns = demoColumns(lang).map(c => (c.field === 'id' ? { ...c, lockVisible: true } : c));
  const grid = createGrid(ctx.host, {
    columns,
    data: makeRows(3000),
    locale: lang,
    // widths, order, hidden and pinned columns and the sort come back after a reload; drag a header up to hide it
    stateKey: 'docs-column-customization',
  });

  grid.on('columnVisible', e => ctx.log(`columnVisible → ${e.colId}: ${e.visible}`));
  grid.on('columnMove', e => ctx.log(`columnMove → ${e.colId} to ${e.toIndex}`));
  grid.on('columnResize', e => ctx.log(`columnResize → ${e.colId}: ${e.width}px`));

  button(ctx, tr(lang, { en: 'Choose columns…', ko: '컬럼 선택…' }), () => grid.openColumnChooser());
  button(ctx, tr(lang, { en: 'Autosize all', ko: '모두 자동 맞춤' }), () => grid.autoSizeColumns());
  separator(ctx);
  button(ctx, tr(lang, { en: 'Save layout', ko: '레이아웃 저장' }), () => {
    localStorage.setItem(KEY, JSON.stringify(grid.getColumnState()));
    ctx.log(tr(lang, { en: 'saved to localStorage', ko: 'localStorage에 저장됨' }));
  });
  button(ctx, tr(lang, { en: 'Restore layout', ko: '레이아웃 복원' }), () => {
    const saved = localStorage.getItem(KEY);
    if (saved) grid.applyColumnState(JSON.parse(saved));
    ctx.log(saved ? tr(lang, { en: 'restored', ko: '복원됨' }) : tr(lang, { en: 'nothing saved yet', ko: '저장된 레이아웃 없음' }));
  });
  button(ctx, tr(lang, { en: 'Reset columns', ko: '컬럼 초기화' }), () => grid.resetColumnState());
  button(ctx, tr(lang, { en: 'Forget saved layout', ko: '저장된 레이아웃 지우기' }), () => {
    grid.clearSavedState();
    ctx.log(tr(lang, { en: 'saved layout cleared', ko: '저장된 레이아웃을 지웠습니다' }));
  });
  return grid;
}

Live demoRow numbers, filter row & saved views

Row numbers, a filter row, tooltips, collapsible column groups, sparklines, row spacing and saved views per person.

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

const KEY = 'freegrid-demo-view';

export default function mount(ctx: DemoContext) {
  const { lang } = ctx;
  const h = (en: string, ko: string) => tr(lang, { en, ko });
  const grid: GridApi = createGrid(ctx.host, {
    columns: [
      { field: 'name', header: h('Name', '이름'), width: 150, headerTooltip: h('Customer name', '고객 이름'), tooltip: true },
      { field: 'email', header: h('Email', '이메일'), width: 150 }, // cut off: hover to read it all
      { field: 'country', header: h('Country', '국가'), width: 120 },
      {
        header: h('Sales', '매출'), children: [
          { field: 'total', header: h('Year', '연간'), type: 'number', format: '#,##0', width: 110, columnGroupShow: 'closed' },
          { field: 'q1', header: 'Q1', type: 'number', format: '#,##0', width: 90, columnGroupShow: 'open' },
          { field: 'q2', header: 'Q2', type: 'number', format: '#,##0', width: 90, columnGroupShow: 'open' },
          { field: 'q3', header: 'Q3', type: 'number', format: '#,##0', width: 90, columnGroupShow: 'open' },
          { field: 'q4', header: 'Q4', type: 'number', format: '#,##0', width: 90, columnGroupShow: 'open' },
        ],
      },
      // four quarters as a small line chart
      { id: 'trend', header: h('Trend', '추이'), width: 110, valueGetter: r => [r.q1, r.q2, r.q3, r.q4], sparkline: true, sortable: false, filter: false },
      { field: 'joined', header: h('Joined', '가입일'), type: 'date', width: 120 },
    ],
    data: makeRows(500),
    locale: lang,
    rowNumbers: true,
    filterRow: true,
    rowSelection: 'multiple',
    getRowId: r => String(r.id),
  });

  text(ctx, h('Type in the boxes under the header: Kim, >50000, 10000~20000, >2025-01-01. Press + on Sales to see the quarters. Hover a cut-off email.',
    '헤더 아래 칸에 입력해 보세요: Kim, >50000, 10000~20000, >2025-01-01. 매출 옆 + 버튼으로 분기를 펼치고, 잘린 이메일 위에 마우스도 올려 보세요.'));
  separator(ctx);
  select(ctx, h('Spacing', '행 간격'), [['compact', h('Compact', '좁게')], ['normal', h('Normal', '보통')], ['comfortable', h('Comfortable', '넓게')]], 'normal',
    d => grid.setDensity(d));
  select(ctx, h('Cell padding', '셀 여백'), [['4', '4px'], ['8', '8px'], ['12', '12px'], ['20', '20px']], '12',
    p => grid.setCellPadding(Number(p)));
  button(ctx, h('Save view', '화면 저장'), () => {
    localStorage.setItem(KEY, JSON.stringify(grid.getState()));
    ctx.log(h('saved: columns, sort, filters, groups, selection', '저장: 컬럼, 정렬, 필터, 그룹, 선택'));
  });
  button(ctx, h('Restore view', '화면 복원'), () => {
    const saved = localStorage.getItem(KEY);
    if (!saved) return ctx.log(h('nothing saved yet', '저장한 화면이 없습니다'));
    grid.setState(JSON.parse(saved));
    ctx.log(h('restored', '복원했습니다'));
  });
  button(ctx, h('Reset', '초기화'), () => {
    grid.setState({ sort: [], filters: {}, quickFilter: '', columnGroups: {}, selected: [] });
    grid.resetColumnState();
  });
  return grid;
}

Users can drag headers to reorder, drag header edges to resize, and use the column menu to autosize, hide, pin or reset columns. Choose columns opens a chooser with search, select all, the group tree and drag-to-reorder. lockVisible keeps a column from being hidden and chooser: false leaves it out of the list. Store getColumnState() to remember each user's layout.

const grid = createGrid(el, {
  columns: [
    { field: 'id', lockVisible: true },       // cannot be hidden
    { field: 'internalCode', chooser: false }, // not listed in the chooser
    ...rest,
  ],
  data,
});

grid.openColumnChooser();
grid.setColumnVisible('email', false);
grid.autoSizeColumns();             // all columns
grid.autoSizeColumns(['name']);     // one column

// save / restore the user's layout
localStorage.setItem('layout', JSON.stringify(grid.getColumnState()));
grid.applyColumnState(JSON.parse(localStorage.getItem('layout')));
grid.resetColumnState();

Each change fires an event: columnMove, columnResize and columnVisible. To keep the same panel docked on the right of the grid, see Side bar.

Saved views

getColumnState() covers the columns only. getState() returns the whole view as plain JSON: column order, widths, visibility and pinning, sort, filters and the quick search, open column groups, the page and the checked rows. Store it per person and give it back with setState(); parts limits what is restored, and columns that no longer exist are skipped.

// save each person's view: columns, sort, filters, grouping, page, selection …
localStorage.setItem('orders-view', JSON.stringify(grid.getState()));

// restore it next time; unknown columns are skipped
grid.setState(JSON.parse(localStorage.getItem('orders-view') ?? '{}'));

Keeping the layout after a reload

stateKey keeps the column layout in the browser across reloads, and resetColumnState() puts it back. See:

Tall headers and selectable text

Long headers can wrap and grow, a header dragged up hides its column, and cell text can be selected with the mouse. See: