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

페이지를 나눠도 같은 값은 칸이 합쳐져요Merged cells work inside pages too

대분류, 구분처럼 같은 값이 이어지면 칸을 합쳐 보여요. 페이지를 나눠도 그 페이지에 보이는 줄 안에서 합쳐지고, '구분'은 같은 대분류 안에서만 합치도록 기준을 정할 수 있어요.Equal neighbouring values merge into one cell, and merging works within each page. The Group column can be told to merge only inside the same major category.

활용Intermediate
직접 써 보기Live preview페이지를 나눠도 같은 값은 칸이 합쳐져요Merged cells work inside pages too

코드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" selected>켜기</option>
      <option value="off">끄기</option>
    </select>
    <select aria-label="구분 합치는 기준" id="within">
      <option value="major" selected>같은 대분류 안에서만</option>
      <option value="value">값이 같으면</option>
    </select>
  </div>
  <div id="grid" style="height: 420px"></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);
document.getElementById('merge').addEventListener('change', (event) => actions.merge(api, event.target.value));
document.getElementById('within').addEventListener('change', (event) => actions.within(api, event.target.value));
example.js
// 식품 영양표: 대분류 → 구분 → 품목. 같은 값이 이어지면 칸을 합쳐 보여요.
// '구분'에는 대분류마다 똑같은 이름('기타')이 있어서, 값만 보면 서로 다른 대분류의 칸까지 합쳐질 수 있어요.
const DATA = [
  ['식물성식품', '곡류', '쌀밥', 310],
  ['식물성식품', '곡류', '현미밥', 330],
  ['식물성식품', '곡류', '보리밥', 300],
  ['식물성식품', '채소류', '시금치', 23],
  ['식물성식품', '채소류', '당근', 41],
  ['식물성식품', '기타', '두부', 84],
  ['식물성식품', '기타', '김', 35],
  ['동물성식품', '기타', '달걀', 143],
  ['동물성식품', '기타', '우유', 61],
  ['동물성식품', '육류', '닭가슴살', 165],
  ['동물성식품', '육류', '돼지목살', 240],
  ['동물성식품', '육류', '소등심', 250],
  ['동물성식품', '수산물', '고등어', 205],
  ['동물성식품', '수산물', '연어', 208],
  ['가공식품', '면류', '라면', 480],
  ['가공식품', '면류', '우동', 330],
  ['가공식품', '기타', '참치캔', 190],
  ['가공식품', '기타', '식빵', 265],
];
const rows = DATA.map(([major, group, item, kcal], i) => ({
  id: i + 1,
  major,
  group,
  item,
  kcal,
}));
// mergeWithin을 쓰면 '구분'은 같은 대분류 안에서만 합쳐져요(예: 식물성식품의 기타와 동물성식품의 기타는 따로).
const columns = (merge, within) => [
  { id: 'major', title: '대분류', width: 120, merge },
  { id: 'group', title: '구분', width: 110, merge, mergeWithin: within ? 'major' : undefined },
  { id: 'item', title: '품목', width: 130 },
  { id: 'kcal', title: '열량(kcal)', kind: 'number', width: 110, align: 'end' },
];
export const options = {
  columns: columns(true, true),
  rows,
  rowKey: 'id',
  showRowNumbers: true,
  pagination: { mode: 'client', pageSize: 9 },
};
let merge = true;
let within = true;
const apply = (api) => api.setColumns(columns(merge, within));
export function setup() {
  merge = true;
  within = true;
  return () => { };
}
export const actions = {
  merge: (api, value) => {
    merge = value !== 'off';
    apply(api);
  },
  within: (api, value) => {
    within = value !== 'value';
    apply(api);
  },
};

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 cells" id="merge">
      <option value="on" selected>On</option>
      <option value="off">Off</option>
    </select>
    <select aria-label="Group merge basis" id="within">
      <option value="major" selected>Inside the same major category</option>
      <option value="value">Same value</option>
    </select>
  </div>
  <div id="grid" style="height: 420px"></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);
document.getElementById('merge').addEventListener('change', (event) => actions.merge(api, event.target.value));
document.getElementById('within').addEventListener('change', (event) => actions.within(api, event.target.value));
example.js
// Food nutrition table: major category -> group -> item. Cells with the same value in a row are merged.
// "Group" has the same name ('Other') under every major category, so going by value alone could merge cells across different major categories.
const DATA = [
  ['Plant foods', 'Grains', 'White rice', 310],
  ['Plant foods', 'Grains', 'Brown rice', 330],
  ['Plant foods', 'Grains', 'Barley rice', 300],
  ['Plant foods', 'Vegetables', 'Spinach', 23],
  ['Plant foods', 'Vegetables', 'Carrot', 41],
  ['Plant foods', 'Other', 'Tofu', 84],
  ['Plant foods', 'Other', 'Seaweed', 35],
  ['Animal foods', 'Other', 'Egg', 143],
  ['Animal foods', 'Other', 'Milk', 61],
  ['Animal foods', 'Meat', 'Chicken breast', 165],
  ['Animal foods', 'Meat', 'Pork shoulder', 240],
  ['Animal foods', 'Meat', 'Beef sirloin', 250],
  ['Animal foods', 'Seafood', 'Mackerel', 205],
  ['Animal foods', 'Seafood', 'Salmon', 208],
  ['Processed foods', 'Noodles', 'Instant ramen', 480],
  ['Processed foods', 'Noodles', 'Udon', 330],
  ['Processed foods', 'Other', 'Canned tuna', 190],
  ['Processed foods', 'Other', 'Sandwich bread', 265],
];
const rows = DATA.map(([major, group, item, kcal], i) => ({
  id: i + 1,
  major,
  group,
  item,
  kcal,
}));
// With mergeWithin, "Group" merges only inside the same major category (e.g. Other under Plant foods and Other under Animal foods stay separate).
const columns = (merge, within) => [
  { id: 'major', title: 'Major category', width: 170, merge },
  { id: 'group', title: 'Group', width: 110, merge, mergeWithin: within ? 'major' : undefined },
  { id: 'item', title: 'Item', width: 140 },
  { id: 'kcal', title: 'Energy (kcal)', kind: 'number', width: 160, align: 'end' },
];
export const options = {
  columns: columns(true, true),
  rows,
  rowKey: 'id',
  showRowNumbers: true,
  pagination: { mode: 'client', pageSize: 9 },
};
let merge = true;
let within = true;
const apply = (api) => api.setColumns(columns(merge, within));
export function setup() {
  merge = true;
  within = true;
  return () => { };
}
export const actions = {
  merge: (api, value) => {
    merge = value !== 'off';
    apply(api);
  },
  within: (api, value) => {
    within = value !== 'value';
    apply(api);
  },
};