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

코드 대신 보이는 이름으로 고르기Pick filter values by what the user sees

데이터에는 D01, 1 같은 코드가 들어 있어도 화면에는 영업팀, 재직처럼 읽기 좋은 이름이 보여요. 필터 값 목록도 보이는 이름으로 나오게 하면 사용자가 코드를 몰라도 고를 수 있어요.Data may hold codes such as D01 or 1 while the grid shows Sales or Active. Show the displayed names in the filter list so users never need to know the codes.

활용Intermediate
직접 써 보기Live preview코드 대신 보이는 이름으로 고르기Pick filter values by what the user sees

코드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="list">
      <option value="shown" selected>보이는 이름 (기본)</option>
      <option value="raw">원래 코드</option>
    </select>
    <button type="button" id="salesOnly">영업팀(D01)만</button>
    <button type="button" id="clear">필터 지우기</button>
  </div>
  <div id="grid" style="height: 540px"></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('list').addEventListener('change', (event) => actions.list(api, event.target.value));
document.getElementById('salesOnly').addEventListener('click', () => actions.salesOnly(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));
example.js
// 데이터에는 코드(D01, 1…)가 들어 있고, 화면에는 formatValue로 읽기 좋은 이름을 보여 주는 표예요.
const DEPT = {
  D01: '영업팀',
  D02: '개발팀',
  D03: '인사팀',
  D04: '디자인팀',
};
const STATUS = { 1: '재직', 2: '휴직', 3: '퇴직 예정' };
const codes = Object.keys(DEPT);
const columns = (shown) => [
  { id: 'name', title: '이름', width: 110 },
  {
    id: 'dept',
    title: '부서',
    kind: 'select',
    width: 150,
    formatValue: (value) => DEPT[String(value)] ?? String(value),
    // 목록에 코드(D01)를 보일지, 화면에 보이는 이름(영업팀)을 보일지 정해요.
    filter: shown ? undefined : { options: codes.map((code) => ({ value: code, label: code })) },
  },
  {
    id: 'status',
    title: '상태',
    kind: 'select',
    width: 130,
    formatValue: (value) => STATUS[Number(value)] ?? String(value),
    filter: shown
      ? undefined
      : { options: [1, 2, 3].map((code) => ({ value: code, label: String(code) })) },
  },
];
export const options = {
  filterRow: true,
  columns: columns(true),
  rows: Array.from({ length: 12 }, (_, i) => ({
    id: i + 1,
    name: ['김민준', '이서연', '박지호', '최유나', '정도윤', '한소희'][i % 6],
    dept: codes[(i * 3) % 4],
    status: 1 + ((i * 5) % 3),
  })),
  rowKey: 'id',
};
export const actions = {
  // 부서·상태 칸의 값 목록에 무엇을 보일지 바꿔요. 걸려 있던 필터는 지워요.
  list: (api, value) => {
    api.setFilterModel(null);
    api.setColumns(columns(value !== 'raw'));
  },
  // 표시 이름으로 고른 것과 같은 결과를 코드로: 영업팀(D01)만
  salesOnly: (api) => api.setFilterModel({
    match: 'all',
    items: [{ columnId: 'dept', operator: 'in', value: ['D01'] }],
  }),
  clear: (api) => api.setFilterModel(null),
};

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="Filter list" id="list">
      <option value="shown" selected>Displayed names (default)</option>
      <option value="raw">Raw codes</option>
    </select>
    <button type="button" id="salesOnly">Sales (D01) only</button>
    <button type="button" id="clear">Clear filter</button>
  </div>
  <div id="grid" style="height: 540px"></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('list').addEventListener('change', (event) => actions.list(api, event.target.value));
document.getElementById('salesOnly').addEventListener('click', () => actions.salesOnly(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));
example.js
// The data holds codes (D01, 1...) and formatValue shows readable names on screen.
const DEPT = {
  D01: 'Sales',
  D02: 'Dev',
  D03: 'HR',
  D04: 'Design',
};
const STATUS = { 1: 'Active', 2: 'On leave', 3: 'Leaving soon' };
const codes = Object.keys(DEPT);
const columns = (shown) => [
  { id: 'name', title: 'Name', width: 130 },
  {
    id: 'dept',
    title: 'Department',
    kind: 'select',
    width: 150,
    formatValue: (value) => DEPT[String(value)] ?? String(value),
    // Decide whether the list shows the code (D01) or the displayed name (Sales).
    filter: shown ? undefined : { options: codes.map((code) => ({ value: code, label: code })) },
  },
  {
    id: 'status',
    title: 'Status',
    kind: 'select',
    width: 130,
    formatValue: (value) => STATUS[Number(value)] ?? String(value),
    filter: shown
      ? undefined
      : { options: [1, 2, 3].map((code) => ({ value: code, label: String(code) })) },
  },
];
export const options = {
  filterRow: true,
  columns: columns(true),
  rows: Array.from({ length: 12 }, (_, i) => ({
    id: i + 1,
    name: ['Liam Carter', 'Emma Stone', 'Noah Kim', 'Olivia Park', 'Ethan Brooks', 'Sophia Lee'][i % 6],
    dept: codes[(i * 3) % 4],
    status: 1 + ((i * 5) % 3),
  })),
  rowKey: 'id',
};
export const actions = {
  // Change what the Department and Status value lists show. Active filters are cleared.
  list: (api, value) => {
    api.setFilterModel(null);
    api.setColumns(columns(value !== 'raw'));
  },
  // The same result as picking by displayed name, from code: Sales (D01) only
  salesOnly: (api) => api.setFilterModel({
    match: 'all',
    items: [{ columnId: 'dept', operator: 'in', value: ['D01'] }],
  }),
  clear: (api) => api.setFilterModel(null),
};