본문으로 건너뛰기Skip to content
예제 첫화면Examples home

내용에 맞춰 자라는 줄 높이Row height that follows the content

rowHeight: 'auto'와 컬럼 wrap: true면 긴 글이 줄바꿈되고 줄이 그 높이만큼 자라요. 보이는 줄만 재서 200줄도 가벼워요.With rowHeight: 'auto' and column wrap: true, long text wraps and the row grows to fit. Only visible rows are measured, so 200 rows stay light.

활용Intermediate
직접 써 보기Live preview내용에 맞춰 자라는 줄 높이Row height that follows the content

코드Code

탭을 골라 코드를 복사하세요. 위 화면은 이 코드를 그대로 실행한 결과예요.Pick a tab and copy the code. The preview above runs exactly this code.

빌드 도구(Vite 등)를 쓰는 프로젝트용 모듈 파일이에요. 설치 없이 바로 돌려 보려면 HTML 탭이나 직접 해보기를 쓰세요.

버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.

index.html
<div id="app">
  <div class="toolbar">
    <button type="button" id="narrow">좁게</button>
    <button type="button" id="wide">넓게</button>
    <button type="button" id="edit">2번 줄 고치기</button>
    <button type="button" id="remeasure">다시 재기</button>
  </div>
  <div id="grid" style="height: 360px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('narrow').addEventListener('click', () => actions.narrow(api));
document.getElementById('wide').addEventListener('click', () => actions.wide(api));
document.getElementById('edit').addEventListener('click', () => actions.edit(api));
document.getElementById('remeasure').addEventListener('click', () => actions.remeasure(api));
example.js
const MEMOS = [
  '짧은 메모',
  '조금 긴 메모예요. 폭이 좁으면 두 줄로 나뉘어요.',
  '아주 긴 메모는 컬럼 폭 안에서 단어 단위로 줄바꿈하고, 줄 높이는 내용에 맞춰 자라요. 폭을 바꾸면 다시 재요.',
];
export const options = {
  columns: [
    { id: 'title', title: '제목', width: 120 },
    { id: 'memo', title: '메모', width: 240, wrap: true },
  ],
  rows: Array.from({ length: 200 }, (_, i) => ({
    id: i + 1,
    title: `항목 ${i + 1}`,
    memo: MEMOS[i % 3],
  })),
  rowKey: 'id',
  rowHeight: 'auto',
};
let say = () => { };
export function setup(_api, log) {
  say = log;
  return () => { };
}
export const actions = {
  narrow: (api) => {
    api.resizeColumn('memo', 140);
    say({ ko: '메모 폭 140px: 줄이 더 자라요', en: 'Memo width 140px: rows grow taller' });
  },
  wide: (api) => {
    api.resizeColumn('memo', 420);
    say({ ko: '메모 폭 420px: 줄이 줄어요', en: 'Memo width 420px: rows shrink' });
  },
  edit: (api) => {
    api.updateRows([{ id: 2, title: '항목 2', memo: MEMOS[2] }]);
    say({ ko: '2번 줄만 다시 재요', en: 'Only row 2 is measured again' });
  },
  remeasure: (api) => {
    api.remeasureRows();
    say({
      ko: 'remeasureRows(): 보이는 줄을 다시 재요',
      en: 'remeasureRows(): visible rows are measured again',
    });
  },
};

Module files for a project with a bundler such as Vite. To run it with no setup, use the HTML tab or Try it.

When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.

index.html
<div id="app">
  <div class="toolbar">
    <button type="button" id="narrow">Narrow</button>
    <button type="button" id="wide">Wide</button>
    <button type="button" id="edit">Edit row 2</button>
    <button type="button" id="remeasure">Measure again</button>
  </div>
  <div id="grid" style="height: 360px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('narrow').addEventListener('click', () => actions.narrow(api));
document.getElementById('wide').addEventListener('click', () => actions.wide(api));
document.getElementById('edit').addEventListener('click', () => actions.edit(api));
document.getElementById('remeasure').addEventListener('click', () => actions.remeasure(api));
example.js
const MEMOS = [
  'Short note',
  'A slightly longer note. If the column is narrow, it splits into two lines.',
  'A very long note wraps word by word inside the column width, and the row height grows to fit the content. Change the width and it is measured again.',
];
export const options = {
  columns: [
    { id: 'title', title: 'Title', width: 120 },
    { id: 'memo', title: 'Memo', width: 240, wrap: true },
  ],
  rows: Array.from({ length: 200 }, (_, i) => ({
    id: i + 1,
    title: `Item ${i + 1}`,
    memo: MEMOS[i % 3],
  })),
  rowKey: 'id',
  rowHeight: 'auto',
};
let say = () => { };
export function setup(_api, log) {
  say = log;
  return () => { };
}
export const actions = {
  narrow: (api) => {
    api.resizeColumn('memo', 140);
    say('Memo width 140px: rows grow taller');
  },
  wide: (api) => {
    api.resizeColumn('memo', 420);
    say('Memo width 420px: rows shrink');
  },
  edit: (api) => {
    api.updateRows([{ id: 2, title: 'Item 2', memo: MEMOS[2] }]);
    say('Only row 2 is measured again');
  },
  remeasure: (api) => {
    api.remeasureRows();
    say('remeasureRows(): visible rows are measured again');
  },
};