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

머리글 안 드롭다운Dropdown inside a header

headerRenderer: 'dropdown'은 머리글에 목록 버튼을 둬요. 고른 값은 onSelect로 앱에 전달되고 이 예제는 그 값으로 행을 걸러요. applyToTitle이면 버튼에 컬럼 제목과 고른 이름이 함께 나와요.headerRenderer: 'dropdown' puts a list button in the header. The chosen value reaches the app through onSelect, and this example filters the rows with it. With applyToTitle the button shows the column title together with the chosen name.

활용Intermediate
직접 써 보기Live preview머리글 안 드롭다운Dropdown inside a header

코드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="dev">개발로 바꾸기</button>
    <button type="button" id="all">전체로 되돌리기</button>
  </div>
  <div id="grid" style="height: 380px"></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('dev').addEventListener('click', () => actions.dev(api));
document.getElementById('all').addEventListener('click', () => actions.all(api));
example.js
// 영어 페이지는 컬럼 제목·목록·데이터를 모두 영어로 보여요.
const en = document.documentElement.dataset.lang === 'en';
const TEAMS = en ? ['Dev', 'Design', 'Sales'] : ['개발', '디자인', '영업'];
const CITIES = en ? ['Seoul', 'Busan', 'Daegu'] : ['서울', '부산', '대구'];
const ALL = en ? 'All' : '전체';
const ROWS = Array.from({ length: 24 }, (_, i) => ({
  id: i + 1,
  name: en ? `Staff ${i + 1}` : `사원 ${i + 1}`,
  team: TEAMS[i % 3],
  city: CITIES[(i * 2) % 3],
}));
let say = () => { };
let apiRef = null;
/** 고른 팀으로 보이는 행을 거른다(앱이 하는 일: 드롭다운은 값만 알려 줘요). */
function filterTeam(team) {
  apiRef?.setQuickFilter(team === null || team === 'all' ? '' : String(team));
}
export const options = {
  columns: [
    { id: 'name', title: en ? 'Name' : '이름', width: 120 },
    {
      id: 'team',
      title: en ? 'Team' : '팀',
      width: 160,
      headerRenderer: 'dropdown',
      headerRendererParams: {
        options: [{ value: 'all', label: ALL }, ...TEAMS.map((t) => ({ value: t, label: t }))],
        value: 'all',
        applyToTitle: true,
        onSelect: ({ value }) => filterTeam(value),
      },
    },
    { id: 'city', title: en ? 'City' : '도시', width: 120 },
  ],
  rows: ROWS,
  rowKey: 'id',
};
export function setup(api, log) {
  say = log;
  apiRef = api;
  const off = api.on('headerDropdownChanged', (e) => say({
    ko: `${e.columnId} 머리글에서 '${e.label}' 고름`,
    en: `Picked '${e.label}' in the ${e.columnId} header`,
  }));
  return () => {
    off();
    apiRef = null;
  };
}
export const actions = {
  dev: (api) => {
    // 값만 바꾸면 목록과 onSelect는 그대로 남아요.
    api.setHeaderRendererParams('team', { value: TEAMS[0] });
    filterTeam(TEAMS[0]);
    say({
      ko: "setHeaderRendererParams: value '개발'",
      en: "setHeaderRendererParams: value 'dev'",
    });
  },
  all: (api) => {
    api.setHeaderRendererParams('team', { value: 'all' });
    filterTeam('all');
    say({ ko: '전체로 되돌림', en: 'Back to all' });
  },
};

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="dev">Set to Dev</button>
    <button type="button" id="all">Back to All</button>
  </div>
  <div id="grid" style="height: 380px"></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('dev').addEventListener('click', () => actions.dev(api));
document.getElementById('all').addEventListener('click', () => actions.all(api));
example.js
const TEAMS = ['Dev', 'Design', 'Sales'];
const CITIES = ['Seoul', 'Busan', 'Daegu'];
const ALL = 'All';
const ROWS = Array.from({ length: 24 }, (_, i) => ({
  id: i + 1,
  name: `Staff ${i + 1}`,
  team: TEAMS[i % 3],
  city: CITIES[(i * 2) % 3],
}));
let say = () => { };
let apiRef = null;
/** Filter the visible rows by the chosen team (the app's job: the dropdown only reports the value). */
function filterTeam(team) {
  apiRef?.setQuickFilter(team === null || team === 'all' ? '' : String(team));
}
export const options = {
  columns: [
    { id: 'name', title: 'Name', width: 120 },
    {
      id: 'team',
      title: 'Team',
      width: 160,
      headerRenderer: 'dropdown',
      headerRendererParams: {
        options: [{ value: 'all', label: ALL }, ...TEAMS.map((t) => ({ value: t, label: t }))],
        value: 'all',
        applyToTitle: true,
        onSelect: ({ value }) => filterTeam(value),
      },
    },
    { id: 'city', title: 'City', width: 120 },
  ],
  rows: ROWS,
  rowKey: 'id',
};
export function setup(api, log) {
  say = log;
  apiRef = api;
  const off = api.on('headerDropdownChanged', (e) => say(`Picked '${e.label}' in the ${e.columnId} header`));
  return () => {
    off();
    apiRef = null;
  };
}
export const actions = {
  dev: (api) => {
    // Changing only the value keeps the list and onSelect as they are.
    api.setHeaderRendererParams('team', { value: TEAMS[0] });
    filterTeam(TEAMS[0]);
    say("setHeaderRendererParams: value 'dev'");
  },
  all: (api) => {
    api.setHeaderRendererParams('team', { value: 'all' });
    filterTeam('all');
    say('Back to all');
  },
};