채우기 규칙을 직접 정하기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.
코드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)
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<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>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));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' });
},
};<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>채우기 규칙을 직접 정하기</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="select">요일·근무 2×2 선택</button>
</div>
<div id="grid" style="height: 330px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
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]));
}
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',
};
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());
}
const actions = {
select: (api) => {
api.selectCells({ rowKey: 1, columnId: 'day' }, { rowKey: 2, columnId: 'shift' });
},
};
options.ariaLabel = "채우기 규칙을 직접 정하기";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
document.getElementById('select').addEventListener('click', () => actions.select(api));
</script>
</body>
</html>버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<button
type="button"
disabled={!api}
onClick={() => api && actions.select(api)}
>요일·근무 2×2 선택</button>
</div>
<Grid style={{ height: 330 }} ref={gridRef} options={options} />
</>
);
}import type { FillContext, GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
day: string;
shift: string;
}
const DAYS = ['월', '화', '수', '목', '금', '토', '일'];
const rows: Row[] = [
{ 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: FillContext): unknown[][] | undefined {
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: GridOptions<Row> = {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => {
api.selectCells({ rowKey: 1, columnId: 'day' }, { rowKey: 2, columnId: 'shift' });
},
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<button type="button" :disabled="!api" @click="api && actions.select(api)">요일·근무 2×2 선택</button>
</div>
<Grid style="height: 330px" :options="options" @ready="onReady" />
</template>import type { FillContext, GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
day: string;
shift: string;
}
const DAYS = ['월', '화', '수', '목', '금', '토', '일'];
const rows: Row[] = [
{ 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: FillContext): unknown[][] | undefined {
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: GridOptions<Row> = {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => {
api.selectCells({ rowKey: 1, columnId: 'day' }, { rowKey: 2, columnId: 'shift' });
},
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<button
type="button"
[disabled]="!api"
(click)="api && actions.select(api)"
>요일·근무 2×2 선택</button>
</div>
<mx-grid style="height: 330px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { FillContext, GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
day: string;
shift: string;
}
const DAYS = ['월', '화', '수', '목', '금', '토', '일'];
const rows: Row[] = [
{ 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: FillContext): unknown[][] | undefined {
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: GridOptions<Row> = {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => {
api.selectCells({ rowKey: 1, columnId: 'day' }, { rowKey: 2, columnId: 'shift' });
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<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>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));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' });
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Define your own fill rule</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="select">Select weekdays and shifts</button>
</div>
<div id="grid" style="height: 330px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
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]));
}
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',
};
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());
}
const actions = {
select: (api) => {
api.selectCells({ rowKey: 1, columnId: 'day' }, { rowKey: 2, columnId: 'shift' });
},
};
options.ariaLabel = "Define your own fill rule";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
document.getElementById('select').addEventListener('click', () => actions.select(api));
</script>
</body>
</html>When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<button
type="button"
disabled={!api}
onClick={() => api && actions.select(api)}
>Select weekdays and shifts</button>
</div>
<Grid style={{ height: 330 }} ref={gridRef} options={options} />
</>
);
}import type { FillContext, GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
day: string;
shift: string;
}
const DAYS = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
const rows: Row[] = [
{ 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: FillContext): unknown[][] | undefined {
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: GridOptions<Row> = {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => {
api.selectCells({ rowKey: 1, columnId: 'day' }, { rowKey: 2, columnId: 'shift' });
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<button type="button" :disabled="!api" @click="api && actions.select(api)">Select weekdays and shifts</button>
</div>
<Grid style="height: 330px" :options="options" @ready="onReady" />
</template>import type { FillContext, GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
day: string;
shift: string;
}
const DAYS = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
const rows: Row[] = [
{ 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: FillContext): unknown[][] | undefined {
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: GridOptions<Row> = {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => {
api.selectCells({ rowKey: 1, columnId: 'day' }, { rowKey: 2, columnId: 'shift' });
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<button
type="button"
[disabled]="!api"
(click)="api && actions.select(api)"
>Select weekdays and shifts</button>
</div>
<mx-grid style="height: 330px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { FillContext, GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
day: string;
shift: string;
}
const DAYS = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
const rows: Row[] = [
{ 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: FillContext): unknown[][] | undefined {
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: GridOptions<Row> = {
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: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>) => {
api.selectCells({ rowKey: 1, columnId: 'day' }, { rowKey: 2, columnId: 'shift' });
},
};