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

합친 구간을 코드로 묻기Ask for merged spans in code

getMergeSpan(rowKey, columnId)는 칸이 속한 병합 구간 { rows, cols, start }를 돌려주고 합치지 않은 칸은 null이에요. 구간의 모든 칸이 같은 start를 돌려줘요. isMerged는 병합 여부만 돌려주고요. 구간은 보이는 행 전체에서 계산하고 정렬·필터가 바뀌면 다시 계산해요.getMergeSpan(rowKey, columnId) returns the merged span { rows, cols, start } of a cell, or null for an unmerged cell. Every cell of a span returns the same start, and isMerged returns only whether the cell is merged. Spans are computed over all visible rows and recalculated when the sort or filter changes.

입문Beginner
직접 써 보기Live preview합친 구간을 코드로 묻기Ask for merged spans in 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="dept">2행 부서 구간</button>
    <button type="button" id="team">3행 팀 구간</button>
    <button type="button" id="single">4행 부서(한 칸)</button>
    <button type="button" id="sort">이름 내림차순 정렬</button>
    <button type="button" id="reset">정렬 해제</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('dept').addEventListener('click', () => actions.dept(api));
document.getElementById('team').addEventListener('click', () => actions.team(api));
document.getElementById('single').addEventListener('click', () => actions.single(api));
document.getElementById('sort').addEventListener('click', () => actions.sort(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));
example.js
const rows = [
  { id: 1, dept: '개발', team: '웹', name: '김하늘' },
  { id: 2, dept: '개발', team: '웹', name: '이도윤' },
  { id: 3, dept: '개발', team: '앱', name: '박서연' },
  { id: 4, dept: '운영', team: '인프라', name: '최지우' },
  { id: 5, dept: '디자인', team: '브랜드', name: '정민준' },
  { id: 6, dept: '디자인', team: '브랜드', name: '강서윤' },
];
// getMergeSpan(rowKey, columnId)는 칸이 속한 병합 구간 { rows, cols, start }를, isMerged는 병합 여부를 돌려줘요.
// 구간은 화면에 그려진 줄이 아니라 보이는 행 전체에서 계산하고 정렬·필터를 바꾸면 다시 계산돼요.
export const options = {
  columns: [
    { id: 'dept', title: '부서', width: 90, merge: true },
    { id: 'team', title: '팀', width: 90, merge: true, mergeWithin: 'dept' },
    { id: 'name', title: '이름', width: 90, merge: true },
  ],
  rows,
  rowKey: 'id',
};
let say = () => { };
export function setup(_api, log) {
  say = log;
  log({
    ko: '버튼으로 구간을 물어 보세요. 정렬을 바꾸면 구간도 달라져요.',
    en: 'Ask for spans with the buttons. A different sort changes the spans.',
  });
}
function show(api, rowKey, columnId) {
  const span = api.getMergeSpan(rowKey, columnId);
  say(span === null
    ? {
      ko: `${rowKey}행 ${columnId}: 병합 아님(isMerged ${api.isMerged(rowKey, columnId)})`,
      en: `Row ${rowKey} ${columnId}: not merged (isMerged ${api.isMerged(rowKey, columnId)})`,
    }
    : {
      ko: `${rowKey}행 ${columnId}: ${span.rows}줄, 시작 ${span.start.rowKey}행`,
      en: `Row ${rowKey} ${columnId}: ${span.rows} rows, starts at row ${span.start.rowKey}`,
    });
}
export const actions = {
  dept: (api) => show(api, 2, 'dept'),
  team: (api) => show(api, 3, 'team'),
  single: (api) => show(api, 4, 'dept'),
  sort: (api) => {
    api.setSortModel([{ columnId: 'name', direction: 'desc' }]);
    show(api, 2, 'dept');
  },
  reset: (api) => {
    api.setSortModel([]);
    show(api, 2, 'dept');
  },
};

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="dept">Row 2 Dept span</button>
    <button type="button" id="team">Row 3 Team span</button>
    <button type="button" id="single">Row 4 Dept (single)</button>
    <button type="button" id="sort">Sort by name desc</button>
    <button type="button" id="reset">Clear sort</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('dept').addEventListener('click', () => actions.dept(api));
document.getElementById('team').addEventListener('click', () => actions.team(api));
document.getElementById('single').addEventListener('click', () => actions.single(api));
document.getElementById('sort').addEventListener('click', () => actions.sort(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));
example.js
const rows = [
  { id: 1, dept: 'Dev', team: 'Web', name: 'Chloe Adams' },
  { id: 2, dept: 'Dev', team: 'Web', name: 'Emma Stone' },
  { id: 3, dept: 'Dev', team: 'App', name: 'Daniel Parker' },
  { id: 4, dept: 'Ops', team: 'Infra', name: 'Olivia Reed' },
  { id: 5, dept: 'Design', team: 'Brand', name: 'Liam Turner' },
  { id: 6, dept: 'Design', team: 'Brand', name: 'Ava Brooks' },
];
// getMergeSpan(rowKey, columnId) returns the merge span { rows, cols, start } the cell belongs to, and isMerged tells whether it is merged.
// Spans are computed over all visible rows, not only the rows drawn on screen, and are recomputed when the sort or filter changes.
export const options = {
  columns: [
    { id: 'dept', title: 'Dept', width: 90, merge: true },
    { id: 'team', title: 'Team', width: 90, merge: true, mergeWithin: 'dept' },
    { id: 'name', title: 'Name', width: 110, merge: true },
  ],
  rows,
  rowKey: 'id',
};
let say = () => { };
export function setup(_api, log) {
  say = log;
  log('Ask for spans with the buttons. A different sort changes the spans.');
}
function show(api, rowKey, columnId) {
  const span = api.getMergeSpan(rowKey, columnId);
  say(span === null
    ? `Row ${rowKey} ${columnId}: not merged (isMerged ${api.isMerged(rowKey, columnId)})`
    : `Row ${rowKey} ${columnId}: ${span.rows} rows, starts at row ${span.start.rowKey}`);
}
export const actions = {
  dept: (api) => show(api, 2, 'dept'),
  team: (api) => show(api, 3, 'team'),
  single: (api) => show(api, 4, 'dept'),
  sort: (api) => {
    api.setSortModel([{ columnId: 'name', direction: 'desc' }]);
    show(api, 2, 'dept');
  },
  reset: (api) => {
    api.setSortModel([]);
    show(api, 2, 'dept');
  },
};