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

컬럼 기본값을 한 번에 정하기Set column defaults once

columnDefaults로 모든 컬럼에 같은 기본 정의를 깔고, 컬럼이 직접 적은 값만 따로 정해요. defaultColumnWidth로 폭을 안 적은 컬럼의 폭도 한 번에 정해요.columnDefaults lays one default definition under every column, and a column only states what differs. defaultColumnWidth sets the width of columns that give none.

입문Beginner
직접 써 보기Live preview컬럼 기본값을 한 번에 정하기Set column defaults once

코드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 id="grid" style="height: 260px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { options } from './example';
const api = createGrid(document.getElementById('grid'), options);
example.js
const names = ['김민준', '이서연', '박도윤', '최하은', '정시우', '강지민'];
const teams = ['개발팀', '디자인팀', '영업팀'];
const cities = ['서울', '부산', '대전'];
// columnDefaults는 모든 컬럼에 먼저 깔리는 기본 정의예요. 컬럼이 직접 적은 값이 항상 이겨요(여기서는 '메모'만 폭을 직접 정했어요).
// defaultColumnWidth는 폭을 정하지 않은 컬럼의 폭이에요.
export const options = {
  columnDefaults: {
    align: 'center',
    editable: true,
    headerTooltip: '기본 정의로 가운데 정렬·편집 가능',
  },
  defaultColumnWidth: 110,
  columns: [
    { id: 'name', title: '이름' },
    { id: 'team', title: '팀' },
    { id: 'city', title: '근무지' },
    { id: 'note', title: '메모', width: 220, align: 'start' },
  ],
  rows: names.map((name, i) => ({
    id: i + 1,
    name,
    team: teams[i % 3],
    city: cities[i % 3],
    note: i % 2 === 0 ? '' : '확인 필요',
  })),
  rowKey: 'id',
};

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 id="grid" style="height: 260px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { options } from './example';
const api = createGrid(document.getElementById('grid'), options);
example.js
const names = ['Liam Carter', 'Emma Stone', 'Noah Hayes', 'Olivia Reed', 'Ethan Cole', 'Ava Lane'];
const teams = ['Dev', 'Design', 'Sales'];
const cities = ['Seattle', 'Boston', 'Denver'];
// columnDefaults is the default definition laid under every column. A value a column states itself always wins (here only 'Note' sets its own width).
// defaultColumnWidth is the width of columns that do not set one.
export const options = {
  columnDefaults: {
    align: 'center',
    editable: true,
    headerTooltip: 'Centered and editable by the default definition',
  },
  defaultColumnWidth: 110,
  columns: [
    { id: 'name', title: 'Name' },
    { id: 'team', title: 'Team' },
    { id: 'city', title: 'City' },
    { id: 'note', title: 'Note', width: 220, align: 'start' },
  ],
  rows: names.map((name, i) => ({
    id: i + 1,
    name,
    team: teams[i % 3],
    city: cities[i % 3],
    note: i % 2 === 0 ? '' : 'Needs a check',
  })),
  rowKey: 'id',
};