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

아이콘을 내 모양으로 바꾸기Replace the grid icons with your own

icons로 정렬 화살표, 펼침 표시, 메뉴 버튼, 필터 버튼을 글자나 SVG로 바꿔요. 컬럼의 icons는 그 컬럼만 바꿔요. 허용 목록에 없는 SVG는 만들 때 거절돼요.icons replaces the sort arrows, the expand marks, the menu button and the filter button with text or SVG. A column's icons changes only that column. An SVG outside the allow list is refused at creation.

활용Intermediate
직접 써 보기Live preview아이콘을 내 모양으로 바꾸기Replace the grid icons with your own

코드Code

탭을 골라 코드를 복사하세요. 위 화면은 이 코드를 그대로 실행한 결과예요.Pick a tab and copy the code. The preview above runs exactly this code.

빌드 도구(Vite 등)를 쓰는 프로젝트용 모듈 파일이에요. 설치 없이 바로 돌려 보려면 HTML 탭이나 직접 해보기를 쓰세요.

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

index.html
<div id="app">
  <div class="toolbar">
    <button type="button" id="unsafe">위험한 SVG 넣기</button>
  </div>
  <div id="grid" style="height: 300px"></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('unsafe').addEventListener('click', () => actions.unsafe(api));
example.js
const rows = [
  { id: 1, team: '영업', name: '김서연', amount: 5200 },
  { id: 2, team: '영업', name: '이준호', amount: 4800 },
  { id: 3, team: '개발', name: '박민지', amount: 6100 },
  { id: 4, team: '개발', name: '최하늘', amount: 5900 },
];
const FUNNEL = '<svg viewBox="0 0 16 16"><path d="M2 3h12L9.5 8.5V13l-3-1.5V8.5z" fill="currentColor"/></svg>';
// icons는 이 그리드 전체의 아이콘이에요. 값은 4자 이하의 글자나 SVG 글이고, 주지 않은 것은 기본 모양이에요.
// 컬럼의 icons는 그 컬럼만 바꿔요(여기서는 금액). SVG는 허용 목록(svg·g·path 등)만 쓸 수 있어요.
export const options = {
  columns: [
    { id: 'team', title: '팀', width: 90 },
    { id: 'name', title: '이름', width: 110 },
    {
      id: 'amount',
      title: '금액',
      kind: 'number',
      width: 110,
      icons: { sortAsc: '▲', sortDesc: '▼' },
    },
  ],
  rows,
  rowKey: 'id',
  grouping: { by: ['team'], expandDepth: 1 },
  columnMenu: true,
  filterMenu: true,
  icons: {
    sortAsc: '↑',
    sortDesc: '↓',
    sortNone: '↕',
    expand: '+',
    collapse: '-',
    menu: '≡',
    filter: FUNNEL,
  },
};
let say = () => undefined;
export function setup(_api, log) {
  say = log;
  log({
    ko: '머리글을 눌러 정렬하면 아이콘이 바뀌어요.',
    en: 'Sort by clicking a header and watch the icons change.',
  });
}
export const actions = {
  // 허용 목록 밖(script, onload)이라 거절돼요. 그리드는 그대로이고 오류 메시지만 받아요.
  unsafe: (api) => {
    try {
      const open = '<' + 'script>';
      api.setOptions({ icons: { menu: `<svg onload="alert(1)">${open}x</svg>` } });
      say('accepted?!');
    }
    catch (error) {
      say(`${error.name}: ${error.message}`);
    }
  },
};

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="unsafe">Try an unsafe SVG</button>
  </div>
  <div id="grid" style="height: 300px"></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('unsafe').addEventListener('click', () => actions.unsafe(api));
example.js
const rows = [
  { id: 1, team: 'Sales', name: 'Emma Stone', amount: 5200 },
  { id: 2, team: 'Sales', name: 'Liam Carter', amount: 4800 },
  { id: 3, team: 'Dev', name: 'Olivia Hayes', amount: 6100 },
  { id: 4, team: 'Dev', name: 'Noah Reed', amount: 5900 },
];
const FUNNEL = '<svg viewBox="0 0 16 16"><path d="M2 3h12L9.5 8.5V13l-3-1.5V8.5z" fill="currentColor"/></svg>';
// icons are the icons for this whole grid. A value is text of up to 4 characters or SVG text, and anything not given keeps the default look.
// A column's icons change only that column (the amount here). SVG may use only the allow list (svg, g, path and so on).
export const options = {
  columns: [
    { id: 'team', title: 'Team', width: 90 },
    { id: 'name', title: 'Name', width: 120 },
    {
      id: 'amount',
      title: 'Amount',
      kind: 'number',
      width: 110,
      icons: { sortAsc: '▲', sortDesc: '▼' },
    },
  ],
  rows,
  rowKey: 'id',
  grouping: { by: ['team'], expandDepth: 1 },
  columnMenu: true,
  filterMenu: true,
  icons: {
    sortAsc: '↑',
    sortDesc: '↓',
    sortNone: '↕',
    expand: '+',
    collapse: '-',
    menu: '≡',
    filter: FUNNEL,
  },
};
let say = () => undefined;
export function setup(_api, log) {
  say = log;
  log('Sort by clicking a header and watch the icons change.');
}
export const actions = {
  // It is outside the allow list (script, onload), so it is rejected. The grid stays as it was and you only get an error message.
  unsafe: (api) => {
    try {
      const open = '<' + 'script>';
      api.setOptions({ icons: { menu: `<svg onload="alert(1)">${open}x</svg>` } });
      say('accepted?!');
    }
    catch (error) {
      say(`${error.name}: ${error.message}`);
    }
  },
};