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

옆 칸이 같으면 가로로 합치기Merge equal neighbours across a row

근무표처럼 같은 줄에서 이웃한 값이 같으면 한 칸으로 합쳐요. 합칠 줄을 조건으로 정할 수도 있어서, 휴가 중인 줄은 그대로 둘 수 있어요.In a shift table, equal neighbours on a row become one cell. A condition decides which rows merge, so vacation rows can stay as they are.

활용Intermediate
직접 써 보기Live preview옆 칸이 같으면 가로로 합치기Merge equal neighbours across a row

코드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">
    <select aria-label="가로 병합" id="merge">
      <option value="on">켜기</option>
      <option value="off">끄기</option>
    </select>
    <select aria-label="휴가 중인 줄" id="vacation">
      <option value="merge">똑같이 합치기</option>
      <option value="skip">합치지 않기</option>
    </select>
  </div>
  <div id="grid" style="height: 250px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options } from './example';
const api = createGrid(document.getElementById('grid'), options);
document.getElementById('merge').addEventListener('change', (event) => actions.merge(api, event.target.value));
document.getElementById('vacation').addEventListener('change', (event) => actions.vacation(api, event.target.value));
example.js
// 시작 컬럼(월)에 mergeCols: 5를 주면, 같은 줄에서 이웃한 같은 값이 한 칸으로 합쳐져요.
// mergeColsWhen은 줄마다 합칠지 정해요. 휴가 중인 줄만 합치지 않게 해 볼 수 있어요.
let merged = true;
let skipVacation = false;
const columns = () => [
  { id: 'name', title: '이름', width: 110 },
  {
    id: 'mon',
    title: '월',
    width: 90,
    align: 'center',
    ...(merged
      ? { mergeCols: 5, ...(skipVacation ? { mergeColsWhen: (row) => !row.vacation } : {}) }
      : {}),
  },
  { id: 'tue', title: '화', width: 90, align: 'center' },
  { id: 'wed', title: '수', width: 90, align: 'center' },
  { id: 'thu', title: '목', width: 90, align: 'center' },
  { id: 'fri', title: '금', width: 90, align: 'center' },
];
const row = (id, name, vacation, days) => ({
  id,
  name,
  vacation,
  mon: days[0],
  tue: days[1],
  wed: days[2],
  thu: days[3],
  fri: days[4],
});
export const options = {
  columns: columns(),
  rows: [
    row(1, '김서연', false, ['주간', '주간', '야간', '야간', '야간']),
    row(2, '이준호', true, ['휴무', '휴무', '휴무', '휴무', '휴무']),
    row(3, '박민지', false, ['주간', '야간', '주간', '야간', '주간']),
    row(4, '최도윤', false, ['야간', '야간', '야간', '주간', '주간']),
    row(5, '정하은', true, ['주간', '주간', '휴무', '휴무', '휴무']),
    row(6, '강지후', false, ['주간', '주간', '주간', '주간', '주간']),
  ],
  rowKey: 'id',
};
export const actions = {
  merge: (api, value) => {
    merged = value !== 'off';
    api.setColumns(columns());
  },
  vacation: (api, value) => {
    skipVacation = value === 'skip';
    api.setColumns(columns());
  },
};

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">
    <select aria-label="Merge across" id="merge">
      <option value="on">On</option>
      <option value="off">Off</option>
    </select>
    <select aria-label="Vacation rows" id="vacation">
      <option value="merge">Merge as usual</option>
      <option value="skip">Do not merge</option>
    </select>
  </div>
  <div id="grid" style="height: 250px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options } from './example';
const api = createGrid(document.getElementById('grid'), options);
document.getElementById('merge').addEventListener('change', (event) => actions.merge(api, event.target.value));
document.getElementById('vacation').addEventListener('change', (event) => actions.vacation(api, event.target.value));
example.js
// With mergeCols: 5 on the start column (Mon), equal neighboring values in the same row merge into one cell.
// mergeColsWhen decides per row whether to merge. You can skip merging for rows on vacation.
let merged = true;
let skipVacation = false;
const columns = () => [
  { id: 'name', title: 'Name', width: 130 },
  {
    id: 'mon',
    title: 'Mon',
    width: 90,
    align: 'center',
    ...(merged
      ? { mergeCols: 5, ...(skipVacation ? { mergeColsWhen: (row) => !row.vacation } : {}) }
      : {}),
  },
  { id: 'tue', title: 'Tue', width: 90, align: 'center' },
  { id: 'wed', title: 'Wed', width: 90, align: 'center' },
  { id: 'thu', title: 'Thu', width: 90, align: 'center' },
  { id: 'fri', title: 'Fri', width: 90, align: 'center' },
];
const row = (id, name, vacation, days) => ({
  id,
  name,
  vacation,
  mon: days[0],
  tue: days[1],
  wed: days[2],
  thu: days[3],
  fri: days[4],
});
export const options = {
  columns: columns(),
  rows: [
    row(1, 'Emma Stone', false, ['Day', 'Day', 'Night', 'Night', 'Night']),
    row(2, 'Liam Carter', true, ['Off', 'Off', 'Off', 'Off', 'Off']),
    row(3, 'Olivia Reed', false, ['Day', 'Night', 'Day', 'Night', 'Day']),
    row(4, 'Noah Parker', false, ['Night', 'Night', 'Night', 'Day', 'Day']),
    row(5, 'Sophia Hayes', true, ['Day', 'Day', 'Off', 'Off', 'Off']),
    row(6, 'Ethan Brooks', false, ['Day', 'Day', 'Day', 'Day', 'Day']),
  ],
  rowKey: 'id',
};
export const actions = {
  merge: (api, value) => {
    merged = value !== 'off';
    api.setColumns(columns());
  },
  vacation: (api, value) => {
    skipVacation = value === 'skip';
    api.setColumns(columns());
  },
};