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

컬럼 폭을 코드로 재고 맞추기Measure and fit column widths from code

measureColumns로 내용에 맞는 폭을 재고, autoSizeColumns로 한 번에 적용하고, fitColumnsToWidth로 남는 폭을 flex 비율대로 나눠요. 컬럼마다 fitWidth·autoSize·flex로 동작을 정해요.measureColumns reads the content width, autoSizeColumns applies it in one go and fitColumnsToWidth shares the spare width by flex weight. fitWidth, autoSize and flex tune each column.

활용Intermediate
직접 써 보기Live preview컬럼 폭을 코드로 재고 맞추기Measure and fit column widths from code

코드Code

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

이 화면은 새 모양 옵션을 켠 모양이에요: density: 'spacious', iconStyle: 'soft', motion: true (코드에 없으면 기본 모양으로 보여요)This preview has the new-look options on: density: 'spacious', iconStyle: 'soft', motion: true (without them in your code you get the default look)

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

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

index.html
<div id="app">
  <div class="toolbar">
    <button type="button" id="measure">폭 재기</button>
    <button type="button" id="autoSize">내용에 맞추기</button>
    <button type="button" id="fit">남는 폭 나누기</button>
    <button type="button" id="reset">처음 폭으로</button>
  </div>
  <div id="grid" style="height: 260px"></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('measure').addEventListener('click', () => actions.measure(api));
document.getElementById('autoSize').addEventListener('click', () => actions.autoSize(api));
document.getElementById('fit').addEventListener('click', () => actions.fit(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));
example.js
// fitWidth: 숫자면 헤더 가장자리를 더블클릭했을 때 그 폭이 돼요(기본 'content'는 내용에 맞춤).
// autoSize: false인 컬럼은 더블클릭·메뉴·autoSizeColumns가 건너뛰어요. flex는 fitColumnsToWidth가 남는 폭을 나누는 가중치예요.
export const options = {
  columns: [
    { id: 'id', title: '번호', width: 70, align: 'end', autoSize: false },
    { id: 'title', title: '작업', width: 120, flex: 3 },
    { id: 'owner', title: '담당', width: 100, flex: 1, fitWidth: 160 },
    { id: 'status', title: '상태', width: 100, flex: 1 },
  ],
  columnFit: 'none',
  columnMenu: true,
  autoSize: { padding: 12, sampleRows: 200 },
  rows: [
    {
      id: 101,
      title: '물류 센터 스캐너 펌웨어를 새 버전으로 교체하기',
      owner: '김민준',
      status: '진행',
    },
    { id: 102, title: '분기 공급사 점검 회의', owner: '이서연', status: '완료' },
    { id: 103, title: '반품 정책 안내 페이지 개편', owner: '박지호', status: '진행' },
    { id: 104, title: '신규 입점 상품 가격표 정리', owner: '최유나', status: '예정' },
  ],
  rowKey: 'id',
};
let say = () => { };
export function setup(_api, log) {
  say = log;
}
export const actions = {
  measure: (api) => {
    const sizes = api.measureColumns();
    say({
      ko: `내용에 맞춘 폭(px): ${JSON.stringify(sizes)}`,
      en: `Content widths (px): ${JSON.stringify(sizes)}`,
    });
  },
  autoSize: (api) => {
    api.autoSizeColumns();
    say({
      ko: '모든 컬럼을 내용에 맞췄어요. 번호는 autoSize: false라 그대로예요.',
      en: 'Every column fitted to its content. Id stays because autoSize is false.',
    });
  },
  fit: (api) => {
    api.fitColumnsToWidth();
    say({
      ko: '남는 폭을 flex 비율(3 : 1 : 1)로 나눠 줬어요.',
      en: 'Spare width shared by flex weight (3 : 1 : 1).',
    });
  },
  reset: (api) => {
    api.resetColumnState();
    say({ ko: '처음 폭으로 돌렸어요.', en: 'Back to the initial widths.' });
  },
};

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="measure">Measure</button>
    <button type="button" id="autoSize">Fit to content</button>
    <button type="button" id="fit">Share spare width</button>
    <button type="button" id="reset">Reset widths</button>
  </div>
  <div id="grid" style="height: 260px"></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('measure').addEventListener('click', () => actions.measure(api));
document.getElementById('autoSize').addEventListener('click', () => actions.autoSize(api));
document.getElementById('fit').addEventListener('click', () => actions.fit(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));
example.js
// fitWidth: a number is the width the column takes when its header edge is double-clicked (the default 'content' fits the content).
// A column with autoSize: false is skipped by double-click, the menu and autoSizeColumns. flex is the weight fitColumnsToWidth uses to share the spare width.
export const options = {
  columns: [
    { id: 'id', title: 'No', width: 70, align: 'end', autoSize: false },
    { id: 'title', title: 'Task', width: 120, flex: 3 },
    { id: 'owner', title: 'Owner', width: 100, flex: 1, fitWidth: 160 },
    { id: 'status', title: 'Status', width: 100, flex: 1 },
  ],
  columnFit: 'none',
  columnMenu: true,
  autoSize: { padding: 12, sampleRows: 200 },
  rows: [
    {
      id: 101,
      title: 'Replace the distribution center scanner firmware with the new version',
      owner: 'Liam Carter',
      status: 'Active',
    },
    { id: 102, title: 'Quarterly supplier review meeting', owner: 'Emma Stone', status: 'Done' },
    { id: 103, title: 'Revamp the returns policy page', owner: 'Noah Hayes', status: 'Active' },
    {
      id: 104,
      title: 'Tidy the price list for new listings',
      owner: 'Olivia Reed',
      status: 'Planned',
    },
  ],
  rowKey: 'id',
};
let say = () => { };
export function setup(_api, log) {
  say = log;
}
export const actions = {
  measure: (api) => {
    const sizes = api.measureColumns();
    say(`Content widths (px): ${JSON.stringify(sizes)}`);
  },
  autoSize: (api) => {
    api.autoSizeColumns();
    say('Every column fitted to its content. No stays because autoSize is false.');
  },
  fit: (api) => {
    api.fitColumnsToWidth();
    say('Spare width shared by flex weight (3 : 1 : 1).');
  },
  reset: (api) => {
    api.resetColumnState();
    say('Back to the initial widths.');
  },
};