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

같은 값은 세로로 합쳐 보여 주기Merge equal values down a column

위아래로 같은 값이 이어지면 한 칸으로 합쳐요. 합치지 않고 첫 줄에만 값을 보이게 할 수도 있고, 어느 쪽이든 값 위치를 첫 줄이나 가운데로 고를 수 있어요.Equal values stacked in a column become one cell, or keep the cells and show the value only on the first row. Either way, put the value on the first row or in the center.

입문Beginner
직접 써 보기Live preview같은 값은 세로로 합쳐 보여 주기Merge equal values down a column

코드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="mode">
      <option value="merge">한 칸으로 합치기</option>
      <option value="top">첫 줄에만 값 보이기</option>
      <option value="off">합치지 않기</option>
    </select>
    <select aria-label="값 위치" id="label">
      <option value="start">첫 줄</option>
      <option value="center" selected>보이는 구간 가운데</option>
    </select>
    <select aria-label="정렬 기준" id="sort">
      <option value="none" selected>정렬 안 함</option>
      <option value="region">지역순</option>
      <option value="category">분류순</option>
      <option value="amount">출하량 큰 순</option>
    </select>
  </div>
  <div id="grid" style="height: 400px"></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('mode').addEventListener('change', (event) => actions.mode(api, event.target.value));
document.getElementById('label').addEventListener('change', (event) => actions.label(api, event.target.value));
document.getElementById('sort').addEventListener('change', (event) => actions.sort(api, event.target.value));
example.js
// 고른 방식을 기억해 두었다가, 바뀔 때마다 같은 방식으로 컬럼을 다시 넣어요.
let mode = 'merge';
let label = 'center';
// merge: true  → 위아래로 같은 값이면 한 칸으로 합쳐요.
// merge: 'top' → 칸은 그대로 두고 구간의 첫 줄에만 값을 보여 줘요.
// mergeLabel   → 두 방식 모두에서 값 위치를 골라요. 'start'는 구간의 첫 줄, 'center'는 보이는 구간의 가운데예요.
const columns = () => {
  const merge = mode === 'off' ? undefined : mode === 'top' ? 'top' : true;
  const on = merge === undefined ? {} : { merge, mergeLabel: label };
  return [
    { id: 'region', title: '지역', width: 110, ...on },
    { id: 'category', title: '분류', width: 110, ...on },
    { id: 'item', title: '품목', width: 130 },
    { id: 'amount', title: '출하량(톤)', kind: 'number', width: 120, align: 'end' },
  ];
};
export const options = {
  columns: columns(),
  headerGroups: [
    { id: 'where', title: '어디서', children: ['region', 'category'] },
    { id: 'what', title: '얼마나', children: ['item', 'amount'] },
  ],
  rows: [
    { id: 1, region: '경기', category: '과일', item: '사과', amount: 120 },
    { id: 2, region: '경기', category: '과일', item: '배', amount: 90 },
    { id: 3, region: '경기', category: '곡물', item: '쌀', amount: 300 },
    { id: 4, region: '전남', category: '과일', item: '감귤', amount: 210 },
    { id: 5, region: '전남', category: '곡물', item: '쌀', amount: 280 },
    { id: 6, region: '전남', category: '곡물', item: '옥수수', amount: 130 },
    { id: 7, region: '강원', category: '채소', item: '배추', amount: 175 },
    { id: 8, region: '강원', category: '채소', item: '감자', amount: 240 },
    { id: 9, region: '제주', category: '과일', item: '한라봉', amount: 85 },
    { id: 10, region: '제주', category: '채소', item: '당근', amount: 140 },
  ],
  rowKey: 'id',
};
export const actions = {
  mode: (api, value) => {
    mode = value === 'top' || value === 'off' ? value : 'merge';
    api.setColumns(columns());
  },
  label: (api, value) => {
    label = value === 'start' ? 'start' : 'center';
    api.setColumns(columns());
  },
  // 합치는 건 "이웃한 줄끼리 같은 값"일 때만이에요. 정렬 기준을 바꾸면 이웃이 바뀌어서 합쳐지는 컬럼도 바뀌어요.
  sort: (api, value) => api.setSortModel(value === 'region' || value === 'category'
    ? [{ columnId: value, direction: 'asc' }]
    : value === 'amount'
      ? [{ columnId: 'amount', direction: 'desc' }]
      : []),
};

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="Mode" id="mode">
      <option value="merge">Merge into one cell</option>
      <option value="top">Show on first row only</option>
      <option value="off">No merging</option>
    </select>
    <select aria-label="Label position" id="label">
      <option value="start">First row</option>
      <option value="center" selected>Center of the visible span</option>
    </select>
    <select aria-label="Sort by" id="sort">
      <option value="none" selected>No sorting</option>
      <option value="region">Region</option>
      <option value="category">Category</option>
      <option value="amount">Amount, high to low</option>
    </select>
  </div>
  <div id="grid" style="height: 400px"></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('mode').addEventListener('change', (event) => actions.mode(api, event.target.value));
document.getElementById('label').addEventListener('change', (event) => actions.label(api, event.target.value));
document.getElementById('sort').addEventListener('change', (event) => actions.sort(api, event.target.value));
example.js
// Remember the chosen mode and set the columns again the same way whenever it changes.
let mode = 'merge';
let label = 'center';
// merge: true  -> equal values in consecutive rows merge into one cell.
// merge: 'top' -> cells stay as they are and only the first row of a run shows the value.
// mergeLabel   -> picks where the value goes in both modes. 'start' is the first row of the run, 'center' is the middle of the visible run.
const columns = () => {
  const merge = mode === 'off' ? undefined : mode === 'top' ? 'top' : true;
  const on = merge === undefined ? {} : { merge, mergeLabel: label };
  return [
    { id: 'region', title: 'Region', width: 110, ...on },
    { id: 'category', title: 'Category', width: 110, ...on },
    { id: 'item', title: 'Item', width: 130 },
    { id: 'amount', title: 'Shipped (tons)', kind: 'number', width: 130, align: 'end' },
  ];
};
export const options = {
  columns: columns(),
  headerGroups: [
    { id: 'where', title: 'Where', children: ['region', 'category'] },
    { id: 'what', title: 'How much', children: ['item', 'amount'] },
  ],
  rows: [
    { id: 1, region: 'Texas', category: 'Fruit', item: 'Apple', amount: 120 },
    { id: 2, region: 'Texas', category: 'Fruit', item: 'Pear', amount: 90 },
    { id: 3, region: 'Texas', category: 'Grain', item: 'Rice', amount: 300 },
    { id: 4, region: 'Florida', category: 'Fruit', item: 'Orange', amount: 210 },
    { id: 5, region: 'Florida', category: 'Grain', item: 'Rice', amount: 280 },
    { id: 6, region: 'Florida', category: 'Grain', item: 'Corn', amount: 130 },
    { id: 7, region: 'Oregon', category: 'Vegetable', item: 'Cabbage', amount: 175 },
    { id: 8, region: 'Oregon', category: 'Vegetable', item: 'Potato', amount: 240 },
    { id: 9, region: 'Hawaii', category: 'Fruit', item: 'Pineapple', amount: 85 },
    { id: 10, region: 'Hawaii', category: 'Vegetable', item: 'Carrot', amount: 140 },
  ],
  rowKey: 'id',
};
export const actions = {
  mode: (api, value) => {
    mode = value === 'top' || value === 'off' ? value : 'merge';
    api.setColumns(columns());
  },
  label: (api, value) => {
    label = value === 'start' ? 'start' : 'center';
    api.setColumns(columns());
  },
  // Cells merge only when "neighboring rows have the same value". Changing the sort changes the neighbors, so which columns merge changes too.
  sort: (api, value) => api.setSortModel(value === 'region' || value === 'category'
    ? [{ columnId: value, direction: 'asc' }]
    : value === 'amount'
      ? [{ columnId: 'amount', direction: 'desc' }]
      : []),
};