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

그룹 행에서 팀 전체 값 한 번에 바꾸기Change a whole group's value from its group row

groupRows.editable에 컬럼을 적으면 그룹 행의 그 칸을 두 번 눌러 그 그룹 모든 행의 값을 한 번에 바꿔요. 되돌리기는 한 단계이고, 자식 행 하나라도 검증·훅에 걸리면 아무것도 바뀌지 않아요.List columns in groupRows.editable and a double-click on that cell of a group row sets the value on every row of the group at once. Undo is one step, and if any child fails validation or a hook nothing changes.

입문Beginner
직접 써 보기Live preview그룹 행에서 팀 전체 값 한 번에 바꾸기Change a whole group's value from its group 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">
    <button type="button" id="allSenior">전원 시니어(코드)</button>
    <button type="button" id="undo">되돌리기</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('allSenior').addEventListener('click', () => actions.allSenior(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(api));
example.js
const names = ['김서연', '이준호', '박민지', '최도윤', '정하은', '강지후', '윤서아', '임태민'];
const teams = ['개발', '디자인', '영업'];
const levels = ['주니어', '미드', '시니어'];
const rows = Array.from({ length: 24 }, (_, i) => ({
  id: i + 1,
  name: `${names[i % names.length]} ${Math.floor(i / names.length) + 1}`,
  team: teams[Math.floor(i / 8)],
  level: levels[i % 3],
  note: '',
  hours: 30 + ((i * 5) % 14),
}));
// groupRows.editable에 컬럼 id를 적으면 그룹 행의 그 칸을 두 번 눌러 고칠 수 있어요. 값은 그 그룹의 모든 자식 행에 한 번에 들어가고(되돌리기 한 단계),
// 자식마다 편집 가능·beforeEdit·검증을 먼저 보고 하나라도 걸리면 아무것도 바뀌지 않아요. 집계가 있는 칸(주간 시간)은 고칠 수 없어요.
export const options = {
  columns: [
    { id: 'name', title: '이름', width: 150 },
    { id: 'team', title: '팀', width: 90 },
    {
      id: 'level',
      title: '등급',
      kind: 'select',
      width: 110,
      editable: true,
      editorOptions: levels.map((value) => ({ value })),
    },
    {
      id: 'note',
      title: '메모',
      width: 160,
      editable: true,
      validation: { maxLength: 12 },
    },
    {
      id: 'hours',
      title: '주간 시간',
      kind: 'number',
      width: 130,
      align: 'end',
      editable: true,
      aggregate: 'sum',
    },
  ],
  rows,
  rowKey: 'id',
  grouping: { by: ['team'], expandDepth: 1 },
  groupRows: { editable: ['level', 'note'] },
};
export function setup(api, log) {
  log({
    ko: '그룹 행의 "등급"·"메모" 칸을 두 번 누르면 그 팀 전원의 값이 한 번에 바뀌어요.',
    en: 'Double-click the Level or Note cell of a group row to change it for the whole team at once.',
  });
  const offs = [
    api.on('groupRowEdited', (e) => log({
      ko: `✓ ${e.keys.length}명의 ${e.columnId}을(를) "${String(e.value)}"(으)로 바꿨어요`,
      en: `✓ Set ${e.columnId} to "${String(e.value)}" for ${e.keys.length} people`,
    })),
    api.on('validationFailed', (e) => log({ ko: `✗ ${e.rowKey}번: ${e.message}`, en: `✗ ${e.rowKey}: ${e.message}` })),
  ];
  return () => offs.forEach((off) => off());
}
export const actions = {
  allSenior: (api) => {
    const first = api.getDisplayRow(0);
    if (first?.kind === 'group')
      api.editGroupRow(first.key, 'level', '시니어');
  },
  undo: (api) => api.undo(),
};

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="allSenior">Everyone Senior (code)</button>
    <button type="button" id="undo">Undo</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('allSenior').addEventListener('click', () => actions.allSenior(api));
document.getElementById('undo').addEventListener('click', () => actions.undo(api));
example.js
const names = [
  'Liam Carter',
  'Emma Stone',
  'Noah Kim',
  'Olivia Reed',
  'Ava Brooks',
  'Mason Lee',
  'Sophia Hall',
  'Lucas Wood',
];
const teams = ['Dev', 'Design', 'Sales'];
const levels = ['Junior', 'Mid', 'Senior'];
const rows = Array.from({ length: 24 }, (_, i) => ({
  id: i + 1,
  name: `${names[i % names.length]} ${Math.floor(i / names.length) + 1}`,
  team: teams[Math.floor(i / 8)],
  level: levels[i % 3],
  note: '',
  hours: 30 + ((i * 5) % 14),
}));
// Listing column ids in groupRows.editable lets users double-click that cell on a group row. The value goes to every child row at once (one undo step);
// each child is checked for editability, beforeEdit and validation first and a single failure changes nothing. Cells with an aggregate (weekly hours) cannot be edited.
export const options = {
  columns: [
    { id: 'name', title: 'Name', width: 150 },
    { id: 'team', title: 'Team', width: 90 },
    {
      id: 'level',
      title: 'Level',
      kind: 'select',
      width: 110,
      editable: true,
      editorOptions: levels.map((value) => ({ value })),
    },
    {
      id: 'note',
      title: 'Note',
      width: 160,
      editable: true,
      validation: { maxLength: 12 },
    },
    {
      id: 'hours',
      title: 'Weekly hours',
      kind: 'number',
      width: 130,
      align: 'end',
      editable: true,
      aggregate: 'sum',
    },
  ],
  rows,
  rowKey: 'id',
  grouping: { by: ['team'], expandDepth: 1 },
  groupRows: { editable: ['level', 'note'] },
};
export function setup(api, log) {
  log('Double-click the Level or Note cell of a group row to change it for the whole team at once.');
  const offs = [
    api.on('groupRowEdited', (e) => log(`✓ Set ${e.columnId} to "${String(e.value)}" for ${e.keys.length} people`)),
    api.on('validationFailed', (e) => log(`✗ ${e.rowKey}: ${e.message}`)),
  ];
  return () => offs.forEach((off) => off());
}
export const actions = {
  allSenior: (api) => {
    const first = api.getDisplayRow(0);
    if (first?.kind === 'group')
      api.editGroupRow(first.key, 'level', 'Senior');
  },
  undo: (api) => api.undo(),
};