FreeGridv1.15.1한국어

Formatting, renderers and sparklines

Numbers and dates formatted for the grid language, custom cell renderers, and sparklines that draw an array of numbers as a small chart in a cell.

Live demoFormatting & renderers

Locale-aware number and date formats, custom cell renderers and classes.

Code for this demo
import { createGrid, type ColumnDef } from '@freegridjs/core';
import de from '@freegridjs/core/locales/de';
import ko from '@freegridjs/core/locales/ko';
import { makeRows } from '../data';
import { select, tr, type DemoContext } from '../ui';

export default function mount(ctx: DemoContext) {
  const { lang } = ctx;
  const h = (en: string, k: string) => tr(lang, { en, ko: k });
  const columns: ColumnDef[] = [
    { field: 'name', header: h('Name', '이름'), width: 170, pinned: 'left' },
    // number patterns: '#,##0', '#,##0.00', '0.0%', 'currency:USD', or Intl.NumberFormat options
    { field: 'total', header: h('Total (USD)', '합계 (USD)'), type: 'number', format: 'currency:USD', width: 140 },
    { field: 'q1', header: h('Q1 (2 decimals)', '1분기 (소수 2자리)'), type: 'number', format: '#,##0.00', width: 150 },
    { field: 'growth', header: h('Growth', '성장률'), type: 'number', format: '0.0%', width: 110,
      cellClass: p => (typeof p.value === 'number' && p.value < 0 ? 'neg' : 'pos') },
    // date presets: 'date', 'datetime', 'time', 'short', 'medium', 'long'
    { field: 'joined', header: h('Joined (long)', '가입일 (long)'), type: 'date', format: 'long', width: 190 },
    { field: 'active', header: h('Active', '활성'), type: 'boolean', width: 90 },
    // A renderer can return an element…
    { id: 'share', header: h('Share of max', '최대 대비 비율'), width: 170, valueGetter: r => r.total / 400000,
      renderer: p => {
        const bar = document.createElement('div');
        bar.className = 'bar';
        const fill = document.createElement('i');
        fill.style.width = `${Math.min(100, Number(p.value) * 100)}%`;
        bar.append(fill);
        return bar;
      } },
    // Return elements for anything built from data; a returned string is inserted as HTML.
    { field: 'email', header: h('Email', '이메일'), width: 280, renderer: p => {
        const a = document.createElement('a');
        a.href = `mailto:${encodeURIComponent(String(p.value))}`;
        a.textContent = String(p.value); // textContent never interprets HTML
        return a;
      } },
  ];
  columns.unshift(
    { id: 'open', header: '', width: 80, pinned: 'left', button: { label: h('Open', '열기'), onClick: p => ctx.log(`open #${p.row.id}`) } },
    { id: 'profile', header: h('Profile', '프로필'), width: 90, valueGetter: r => r.id, link: { href: '#/people/{id}', label: () => h('View', '보기'), onClick: p => ctx.log(`link #${p.row.id}`) } },
  );
  const grid = createGrid(ctx.host, {
    columns, data: makeRows(2000), locale: lang === 'ko' ? ko : 'en',
    // whole-row colours from the values; they go into Excel and PDF too
    rowStyle: ({ row }) => (row.growth < -0.1 ? { background: 'rgba(220, 38, 38, .12)', color: '#b91c1c' } : row.growth > 0.3 ? { background: 'rgba(22, 163, 74, .12)' } : null),
  });

  select(ctx, tr(lang, { en: 'Format locale', ko: '서식 로케일' }), [['en', 'English (en)'], ['ko', '한국어 (ko)'], ['de', 'Deutsch (de)']],
    lang, v => grid.setLocale(v === 'ko' ? ko : v === 'de' ? de : 'en'));
  return grid;
}

Numbers and dates are formatted with the browser's Intl APIs in the grid language, so 1234.5 becomes 1,234.5 in English and 1.234,5 in German. Use renderer for anything else; it gets value, formatted, row, rowIndex and column.

{ field: 'total', type: 'number', format: '#,##0' }       // 1,234
{ field: 'rate', type: 'number', format: '0.0%' }          // 12.5%
{ field: 'price', type: 'number', format: 'currency:KRW' } // ₩12,000
{ field: 'price', type: 'number', format: { maximumSignificantDigits: 3 } } // Intl options
{ field: 'joined', type: 'date', format: 'long' }          // 2026년 3월 5일 (ko)

// element renderer
{ field: 'status', renderer: p => {
    const b = document.createElement('span');
    b.className = `badge badge-${p.value}`;
    b.textContent = p.formatted;
    return b;
} }

A renderer that returns a string is inserted as HTML. Escape anything that comes from users, or return an element.

Sparklines

A column with sparkline draws an array of numbers as a small chart inside the cell: a line by default, or bars or an area. Use a valueGetter to build the array from other fields, such as four quarters.

{ id: 'trend', header: 'Trend', valueGetter: r => [r.q1, r.q2, r.q3, r.q4], sparkline: true }
{ field: 'monthly', sparkline: { type: 'bar', color: '#2563eb' } }   // 'line' (default) | 'bar' | 'area'

Row colours

rowClass and rowStyle colour a whole row from its values. See:

Buttons, links and images

button, link and image put actions and pictures in cells. See: