FreeGridv1.15.1한국어

Column spans, wrapping and row height

colSpan lets a cell run across columns, wrapText and autoRowHeight wrap long text and grow the row, getRowHeight sets heights per row, and autoHeight drops the inner scroll.

Live demoColumn spans, wrapping & row height

Note rows across several columns, wrapped text with growing rows, row heights per row and header buttons.

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

export default function mount(ctx: DemoContext) {
  const { lang } = ctx;
  const h = (en: string, ko: string) => tr(lang, { en, ko });
  const regions = [
    { region: h('Seoul', '서울'), items: [[h('Monitor', '모니터'), h('Pending payment for the March order; follow up with the buyer before the end of the week.', '3월 주문 입금 대기 중입니다. 이번 주가 끝나기 전에 구매 담당자에게 다시 연락해야 합니다.')], [h('Keyboard', '키보드'), h('Shipped.', '출고 완료.')], [h('Desk', '책상'), h('Assembly service booked for Friday afternoon, customer asked for a call 30 minutes before arrival.', '금요일 오후 조립 서비스 예약. 도착 30분 전에 전화해 달라는 요청이 있었습니다.')]] },
    { region: h('Busan', '부산'), items: [[h('Chair', '의자'), h('Back-ordered: the supplier expects new stock in two weeks.', '재고 없음: 공급처 입고까지 2주 예상.')], [h('Lamp', '조명'), h('Delivered.', '배송 완료.')]] },
    { region: h('Tokyo', '도쿄'), items: [[h('Monitor arm', '모니터 암'), h('Customs paperwork done.', '통관 서류 처리 완료.')], [h('Cable kit', '케이블 세트'), h('Gift wrap requested.', '선물 포장 요청.')]] },
  ];
  const data: Row[] = [];
  for (const r of regions) {
    r.items.forEach(([item, memo], i) => data.push({
      region: r.region, item, memo,
      qty: 1 + ((data.length * 7) % 9), price: 30 + ((data.length * 37) % 400),
    }));
    data.push({ kind: 'note', item: h(`${r.region}: shipping cut-off is 3 pm`, `${r.region}: 오후 3시 이후 주문은 다음 날 출고`) });
  }

  const columns: ColumnDef[] = [
    { field: 'region', header: h('Region', '지역'), width: 110 },
    // note rows span the item, memo and qty columns
    { field: 'item', header: h('Item', '품목'), width: 150, colSpan: ({ row }) => (row.kind === 'note' ? 3 : 1),
      cellClass: ({ row }) => (row.kind === 'note' ? 'demo-note' : undefined) },
    // long memos wrap; autoRowHeight grows the row
    { field: 'memo', header: h('Memo', '메모'), width: 280, wrapText: true },
    { field: 'qty', header: h('Qty', '수량'), type: 'number', width: 90 },
    {
      field: 'price', header: h('Price', '단가'), type: 'number', format: '#,##0', width: 120, headerClass: 'demo-money',
      // a help button in the header; pressing it does not sort
      headerRenderer: ({ header }) => {
        const span = document.createElement('span');
        const b = document.createElement('button');
        b.type = 'button';
        b.textContent = '?';
        b.className = 'demo-help';
        b.addEventListener('click', () => ctx.log(h('Prices are in USD, before tax.', '단가는 달러, 세전 금액입니다.')));
        span.append(`${header} `, b);
        return span;
      },
    },
  ];

  let grid: GridApi;
  const create = (auto: boolean) => {
    grid?.destroy();
    grid = createGrid(ctx.host, {
      columns, data, locale: lang,
      autoRowHeight: auto, // rows grow to fit the wrapped memos
      // note rows are a little lower than the others
      getRowHeight: row => (row.kind === 'note' ? 30 : undefined),
    });
  };
  create(true);

  text(ctx, h('Note rows span three columns, long memos wrap and the row grows. Press ? in the Price header.',
    '안내 행은 세 칸을 차지하고, 긴 메모는 줄바꿈되며 행 높이가 늘어납니다. 단가 헤더의 ? 버튼도 눌러 보세요.'));
  separator(ctx);
  toggle(ctx, h('Rows grow to fit (autoRowHeight)', '내용에 맞춰 행 높이 (autoRowHeight)'), true, on => { create(on); ctx.log(`autoRowHeight: ${on}`); });
  return grid!;
}

colSpan decides per cell how many columns it covers: a note row across several columns, or a label that runs over the empty cells next to it. A spanned cell stays one cell for focus and copying. wrapText: true wraps long text onto more lines, and autoRowHeight: true grows each row to fit; rowHeight is then the minimum.

getRowHeight gives single rows their own height, such as a lower note row. wrapHeaderText (on a column, or for every column as a grid option) lets a long header wrap instead of being cut off, and headerRenderer puts icons or help buttons in a header; pressing them does not sort. With autoHeight: true the grid has no inner scroll and grows with its rows, which suits short lists of up to a few hundred rows. Its width still follows the container, wide columns scroll sideways, and with no rows it keeps three rows of room for the "no rows" message.

createGrid(el, {
  columns: [
    // one cell spans the row's first three columns when it is a note row
    { field: 'title', colSpan: ({ row }) => (row.kind === 'note' ? 3 : 1) },
    // long text wraps; the row grows to fit
    { field: 'memo', width: 220, wrapText: true },
    // a long header wraps instead of being cut off
    { field: 'q1', header: 'First quarter revenue', wrapHeaderText: true },
    // icons or buttons in the header; sorting still works
    { field: 'amount', type: 'number', headerClass: 'money',
      headerRenderer: ({ header }) => `${header} <button type="button" data-fg-no-sort>?</button>` },
  ],
  data,
  autoRowHeight: true,                                // rowHeight becomes the minimum
  getRowHeight: row => (row.kind === 'note' ? 28 : undefined),
  // autoHeight: true,                                // no inner scroll: the grid grows with its rows
});

To join cells over several rows, see Merged cells. Right-to-left layout is on Languages and right-to-left, and cards for phones on Card view.