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

채우기 규칙을 직접 정하기Define your own fill rule

cellFill의 fill 콜백으로 채울 값을 직접 정해요. 요일 열은 월·화·수 순서를 이어 가고 근무 열은 되풀이해요. direction: "y"로 아래로만 채워요.The cellFill fill callback sets the values to fill. The weekday column continues Mon, Tue, Wed and the shift column repeats. direction: "y" fills downward only.

심화Advanced
직접 써 보기Live preview채우기 규칙을 직접 정하기Define your own fill rule

코드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="select">요일·근무 2×2 선택</button>
  </div>
  <div id="grid" style="height: 330px"></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('select').addEventListener('click', () => actions.select(api));
example.js
const DAYS = ['월', '화', '수', '목', '금', '토', '일'];
const rows = [
  { id: 1, day: '월', shift: '오전' },
  { id: 2, day: '화', shift: '오후' },
  { id: 3, day: '', shift: '' },
  { id: 4, day: '', shift: '' },
  { id: 5, day: '', shift: '' },
  { id: 6, day: '', shift: '' },
  { id: 7, day: '', shift: '' },
  { id: 8, day: '', shift: '' },
];
// 기본 채우기는 글자를 되풀이해요. fill 콜백을 주면 원본 밖(새로 채울) 줄의 값을 직접 정해요(줄 수 × 목표 컬럼 수의 배열).
// 여기서는 요일 열은 요일 순서를 이어 가고, 근무 열은 원본을 되풀이해요. rowKey가 1부터 이어지는 숫자라서 줄 수를 키로 셀 수 있어요.
function fill(ctx) {
  const dayAt = ctx.columns.indexOf('day');
  if (ctx.direction !== 'y' || dayAt < 0)
    return undefined; // undefined면 기본 규칙
  const last = DAYS.indexOf(String(ctx.source[ctx.source.length - 1]?.[dayAt] ?? ''));
  const added = Number(ctx.targetRange.to.rowKey) - Number(ctx.sourceRange.to.rowKey);
  return Array.from({ length: added }, (_, i) => ctx.columns.map((_id, c) => c === dayAt
    ? (DAYS[(last + 1 + i) % DAYS.length] ?? '')
    : ctx.source[i % ctx.source.length]?.[c]));
}
export const options = {
  columns: [
    { id: 'day', title: '요일', width: 100, editable: true },
    { id: 'shift', title: '근무', width: 110, editable: true },
  ],
  cellSelection: 'multiple',
  cellFill: { direction: 'y', fill },
  rows,
  rowKey: 'id',
};
export function setup(api, log) {
  log({
    ko: '두 줄을 고른 뒤 핸들을 아래로 끌어 보세요. 요일은 월·화·수·목…으로 이어 가고 근무는 되풀이돼요.',
    en: 'Select two rows and drag the handle down: weekdays continue Mon, Tue, Wed… and shifts repeat.',
  });
  const offs = [
    api.on('fillEnded', (e) => log({
      ko: `채웠어요: ${e.count}칸`,
      en: `Filled: ${e.count} ${e.count === 1 ? 'cell' : 'cells'}`,
    })),
  ];
  return () => offs.forEach((off) => off());
}
export const actions = {
  select: (api) => {
    api.selectCells({ rowKey: 1, columnId: 'day' }, { rowKey: 2, columnId: 'shift' });
  },
};

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="select">Select weekdays and shifts</button>
  </div>
  <div id="grid" style="height: 330px"></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('select').addEventListener('click', () => actions.select(api));
example.js
const DAYS = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
const rows = [
  { id: 1, day: 'Mon', shift: 'AM' },
  { id: 2, day: 'Tue', shift: 'PM' },
  { id: 3, day: '', shift: '' },
  { id: 4, day: '', shift: '' },
  { id: 5, day: '', shift: '' },
  { id: 6, day: '', shift: '' },
  { id: 7, day: '', shift: '' },
  { id: 8, day: '', shift: '' },
];
// The default fill repeats the text. With a fill callback you set the values of the rows outside the source (the ones to be filled) yourself (an array of rows x target columns).
// Here the weekday column continues the weekday order, and the shift column repeats the source. The rowKey is a number running from 1, so the row count can be counted by key.
function fill(ctx) {
  const dayAt = ctx.columns.indexOf('day');
  if (ctx.direction !== 'y' || dayAt < 0)
    return undefined; // undefined means the default rule
  const last = DAYS.indexOf(String(ctx.source[ctx.source.length - 1]?.[dayAt] ?? ''));
  const added = Number(ctx.targetRange.to.rowKey) - Number(ctx.sourceRange.to.rowKey);
  return Array.from({ length: added }, (_, i) => ctx.columns.map((_id, c) => c === dayAt
    ? (DAYS[(last + 1 + i) % DAYS.length] ?? '')
    : ctx.source[i % ctx.source.length]?.[c]));
}
export const options = {
  columns: [
    { id: 'day', title: 'Weekday', width: 100, editable: true },
    { id: 'shift', title: 'Shift', width: 110, editable: true },
  ],
  cellSelection: 'multiple',
  cellFill: { direction: 'y', fill },
  rows,
  rowKey: 'id',
};
export function setup(api, log) {
  log('Select two rows and drag the handle down: weekdays continue Mon, Tue, Wed… and shifts repeat.');
  const offs = [
    api.on('fillEnded', (e) => log(`Filled: ${e.count} ${e.count === 1 ? 'cell' : 'cells'}`)),
  ];
  return () => offs.forEach((off) => off());
}
export const actions = {
  select: (api) => {
    api.selectCells({ rowKey: 1, columnId: 'day' }, { rowKey: 2, columnId: 'shift' });
  },
};