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

다른 컬럼으로 계산한 합계 컬럼Computed total columns

다른 값을 더하거나 곱해서 만든 컬럼은 자기 데이터가 따로 없어요. 그래도 일반 컬럼처럼 정렬·필터가 되고, 원본 값이 바뀌면 같이 바뀌어요.A column built from other values has no data of its own, yet it sorts and filters like any column and follows when the source values change.

활용Intermediate
직접 써 보기Live preview다른 컬럼으로 계산한 합계 컬럼Computed total columns

코드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="bonusUp">상여 50만 원 올리기</button>
    <button type="button" id="bonusReset">처음 값으로</button>
  </div>
  <div id="grid" style="height: 200px"></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('bonusUp').addEventListener('click', () => actions.bonusUp(api));
document.getElementById('bonusReset').addEventListener('click', () => actions.bonusReset(api));
example.js
const won = (value) => Number(value).toLocaleString('ko-KR');
// 계산 컬럼은 자기 값을 따로 갖지 않아요. getValue가 행에서 값을 만들어서, 다른 컬럼처럼 정렬·필터가 되고 직접 고칠 수는 없어요.
// 세금은 400만 원까지는 15%, 넘는 부분만 24%로 계산해요(누진). 그래서 많이 받을수록 실수령도 항상 늘어요.
const net = (gross) => Math.round(gross - Math.min(gross, 4000000) * 0.15 - Math.max(0, gross - 4000000) * 0.24);
export const options = {
  columns: [
    { id: 'name', title: '이름', width: 110 },
    { id: 'salary', title: '월급(원)', kind: 'number', width: 120, align: 'end', formatValue: won },
    { id: 'bonus', title: '상여(원)', kind: 'number', width: 110, align: 'end', formatValue: won },
    {
      id: 'gross',
      title: '지급 합계',
      kind: 'number',
      width: 130,
      align: 'end',
      getValue: (row) => row.salary + row.bonus,
      formatValue: won,
    },
    {
      id: 'net',
      title: '세후 실수령',
      kind: 'number',
      width: 140,
      align: 'end',
      getValue: (row) => net(row.salary + row.bonus),
      formatValue: won,
    },
  ],
  rows: [
    { id: 1, name: '김민준', salary: 4200000, bonus: 800000 },
    { id: 2, name: '이서연', salary: 3800000, bonus: 600000 },
    { id: 3, name: '박지호', salary: 4500000, bonus: 700000 },
    { id: 4, name: '최유나', salary: 3300000, bonus: 400000 },
  ],
  rowKey: 'id',
};
let say = () => { };
export function setup(api, log) {
  say = log;
  api.setSortModel([{ columnId: 'net', direction: 'desc' }]);
  return () => { };
}
export const actions = {
  // 상여를 바꾸면 지급 합계와 세후 실수령이 따라 바뀌어요.
  bonusUp: (api) => {
    api.updateRows(api.getRows().map((row) => ({ ...row, bonus: row.bonus + 500000 })));
    say({ ko: '모두의 상여를 50만 원씩 올렸어요', en: "Raised everyone's bonus by 500,000 KRW" });
  },
  bonusReset: (api) => {
    api.setRows(options.rows ?? []);
    say({ ko: '상여를 처음 값으로 되돌렸어요', en: 'Bonus back to the starting values' });
  },
};

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="bonusUp">Raise bonus by $500</button>
    <button type="button" id="bonusReset">Reset</button>
  </div>
  <div id="grid" style="height: 200px"></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('bonusUp').addEventListener('click', () => actions.bonusUp(api));
document.getElementById('bonusReset').addEventListener('click', () => actions.bonusReset(api));
example.js
const money = (value) => '$' + Number(value).toLocaleString('en-US');
// A computed column has no value of its own. getValue builds the value from the row, so it sorts and filters like any other column but cannot be edited directly.
// Tax is 15% up to 4,000 and 24% only on the part above it (progressive), so more pay always means more net.
const net = (gross) => Math.round(gross - Math.min(gross, 4000) * 0.15 - Math.max(0, gross - 4000) * 0.24);
export const options = {
  columns: [
    { id: 'name', title: 'Name', width: 110 },
    {
      id: 'salary',
      title: 'Salary ($)',
      kind: 'number',
      width: 120,
      align: 'end',
      formatValue: money,
    },
    {
      id: 'bonus',
      title: 'Bonus ($)',
      kind: 'number',
      width: 110,
      align: 'end',
      formatValue: money,
    },
    {
      id: 'gross',
      title: 'Total pay',
      kind: 'number',
      width: 130,
      align: 'end',
      getValue: (row) => row.salary + row.bonus,
      formatValue: money,
    },
    {
      id: 'net',
      title: 'Net after tax',
      kind: 'number',
      width: 140,
      align: 'end',
      getValue: (row) => net(row.salary + row.bonus),
      formatValue: money,
    },
  ],
  rows: [
    { id: 1, name: 'Liam Carter', salary: 4200, bonus: 800 },
    { id: 2, name: 'Emma Stone', salary: 3800, bonus: 600 },
    { id: 3, name: 'Noah Hayes', salary: 4500, bonus: 700 },
    { id: 4, name: 'Olivia Reed', salary: 3300, bonus: 400 },
  ],
  rowKey: 'id',
};
let say = () => { };
export function setup(api, log) {
  say = log;
  api.setSortModel([{ columnId: 'net', direction: 'desc' }]);
  return () => { };
}
export const actions = {
  // Changing the bonus changes Total pay and Net after tax with it.
  bonusUp: (api) => {
    api.updateRows(api.getRows().map((row) => ({ ...row, bonus: row.bonus + 500 })));
    say("Raised everyone's bonus by $500");
  },
  bonusReset: (api) => {
    api.setRows(options.rows ?? []);
    say('Bonus back to the starting values');
  },
};