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

그룹 안에서 비율·순위로 보기Show numbers as shares and ranks inside groups

컬럼 showAs로 숫자를 그룹 합계 대비 %, 전체 합계 대비 %, 그룹 안 순위로 보여 줘요. 표시만 바뀌고 값·정렬·편집은 그대로예요. 내보내기는 exportShowAs를 켜야 보이는 값을 써요.A column's showAs shows a number as a percentage of its group, a percentage of the grand total or a rank inside the group. Only the display changes; the value, sorting and editing stay. Exports use the displayed value only with exportShowAs.

입문Beginner
직접 써 보기Live preview그룹 안에서 비율·순위로 보기Show numbers as shares and ranks inside groups

코드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 탭이나 직접 해보기를 쓰세요.

이 예제는 추가 패키지가 필요해요. 내 컴퓨터에서 돌리려면 npm i maxgrid @maxgrid/export로 설치하세요.

버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.

index.html
<div id="app">
  <div class="toolbar">
    <select aria-label="보기 방식" id="showAs">
      <option value="value">값 그대로</option>
      <option value="percentOfGroup" selected>그룹 합계 대비</option>
      <option value="percentOfTotal">전체 합계 대비</option>
      <option value="rankInGroup">그룹 안 순위</option>
    </select>
    <select aria-label="내보낼 때" id="exportShown">
      <option value="no" selected>원래 값</option>
      <option value="yes">보이는 값</option>
    </select>
    <button type="button" id="csv">CSV</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('showAs').addEventListener('change', (event) => actions.showAs(api, event.target.value));
document.getElementById('exportShown').addEventListener('change', (event) => actions.exportShown(api, event.target.value));
document.getElementById('csv').addEventListener('click', () => actions.csv(api));
example.js
import { withExport } from '@maxgrid/export';
const regions = ['서울', '부산', '대구'];
const products = ['무선 이어폰', '노트북', '스마트워치', '태블릿', '보조배터리', '스피커'];
const rows = Array.from({ length: 18 }, (_, i) => ({
  id: i + 1,
  region: regions[Math.floor(i / 6)],
  product: products[i % 6],
  amount: 120 + ((i * 53) % 400),
}));
// showAs는 그룹 안에서 숫자 컬럼이 보이는 방식만 바꿔요(값·정렬·편집 값은 그대로). 퍼센트는 소수 1자리예요.
export const options = {
  columns: [
    { id: 'product', title: '상품', width: 160 },
    { id: 'region', title: '지역', width: 100 },
    {
      id: 'amount',
      title: '금액 ($)',
      kind: 'number',
      width: 140,
      align: 'end',
      aggregate: 'sum',
      showAs: 'percentOfGroup',
      exportShowAs: false,
    },
  ],
  rows,
  rowKey: 'id',
  grouping: { by: ['region'], expandDepth: 'all' },
  summary: true,
};
export function setup(_api, log) {
  log({
    ko: '"보기 방식"을 바꿔 금액이 그룹 안 비율·전체 비율·순위로 보이게 해 보세요. 값은 그대로예요.',
    en: 'Change "Show as" to see the amount as a share of its group, of the total, or as a rank. The value itself does not change.',
  });
  return () => undefined;
}
export const actions = {
  showAs: (api, value) => api.updateColumn('amount', {
    showAs: (value ?? 'value'),
  }),
  exportShown: (api, value) => api.updateColumn('amount', { exportShowAs: value === 'yes' }),
  csv: (api) => void withExport(api).downloadCsv('group-show-as.csv', { hierarchy: true, bom: true }),
};

Module files for a project with a bundler such as Vite. To run it with no setup, use the HTML tab or Try it.

This example needs an extra package. To run it locally, install with npm i maxgrid @maxgrid/export.

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="Show as" id="showAs">
      <option value="value">Value</option>
      <option value="percentOfGroup" selected>Share of group</option>
      <option value="percentOfTotal">Share of total</option>
      <option value="rankInGroup">Rank in group</option>
    </select>
    <select aria-label="On export" id="exportShown">
      <option value="no" selected>Real value</option>
      <option value="yes">Displayed value</option>
    </select>
    <button type="button" id="csv">CSV</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('showAs').addEventListener('change', (event) => actions.showAs(api, event.target.value));
document.getElementById('exportShown').addEventListener('change', (event) => actions.exportShown(api, event.target.value));
document.getElementById('csv').addEventListener('click', () => actions.csv(api));
example.js
import { withExport } from '@maxgrid/export';
const regions = ['New York', 'Boston', 'Austin'];
const products = ['Earbuds', 'Laptop', 'Smartwatch', 'Tablet', 'Power bank', 'Speaker'];
const rows = Array.from({ length: 18 }, (_, i) => ({
  id: i + 1,
  region: regions[Math.floor(i / 6)],
  product: products[i % 6],
  amount: 120 + ((i * 53) % 400),
}));
// showAs only changes how a number column looks inside groups (the value, sorting and the edited value stay the same). Percentages show one decimal.
export const options = {
  columns: [
    { id: 'product', title: 'Product', width: 160 },
    { id: 'region', title: 'Region', width: 100 },
    {
      id: 'amount',
      title: 'Amount ($)',
      kind: 'number',
      width: 140,
      align: 'end',
      aggregate: 'sum',
      showAs: 'percentOfGroup',
      exportShowAs: false,
    },
  ],
  rows,
  rowKey: 'id',
  grouping: { by: ['region'], expandDepth: 'all' },
  summary: true,
};
export function setup(_api, log) {
  log('Change "Show as" to see the amount as a share of its group, of the total, or as a rank. The value itself does not change.');
  return () => undefined;
}
export const actions = {
  showAs: (api, value) => api.updateColumn('amount', {
    showAs: (value ?? 'value'),
  }),
  exportShown: (api, value) => api.updateColumn('amount', { exportShowAs: value === 'yes' }),
  csv: (api) => void withExport(api).downloadCsv('group-show-as.csv', { hierarchy: true, bom: true }),
};