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

같은 저장소, 서로 다른 필터One store, different filters

두 그리드가 같은 행을 보지만 필터는 따로예요. 값이 바뀌면 각 그리드의 필터가 다시 판정해요.Two grids see the same rows but keep their own filters. When a value changes, each grid's filter judges the row again.

심화Advanced
직접 써 보기Live preview같은 저장소, 서로 다른 필터One store, different filters

코드Code

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

이 화면은 새 모양 옵션을 켠 모양이에요: iconStyle: 'soft', motion: true (코드에 없으면 기본 모양으로 보여요)This preview has the new-look options on: 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="moveTeam">개발로 옮기기</button>
    <button type="button" id="raise">수량 올리기</button>
    <button type="button" id="lower">수량 내리기</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('moveTeam').addEventListener('click', () => actions.moveTeam(api));
document.getElementById('raise').addEventListener('click', () => actions.raise(api));
document.getElementById('lower').addEventListener('click', () => actions.lower(api));
example.js
const store = MaxGrid.createRowsStore([
  { id: 1, name: '김하늘', team: '개발', qty: 4 },
  { id: 2, name: '이도윤', team: '운영', qty: 9 },
  { id: 3, name: '박서연', team: '디자인', qty: 2 },
  { id: 4, name: '최지우', team: '개발', qty: 12 },
  { id: 5, name: '정민준', team: '운영', qty: 6 },
], { rowKey: 'id' });
// 같은 저장소를 보는 두 그리드가 서로 다른 필터를 가져요. 왼쪽 위는 개발 팀만, 아래는 수량이 5 이상인 행만 보여요.
// 한쪽에서 행을 고치면 다른 쪽의 필터가 그 행을 다시 판정해서 나타나거나 사라져요.
const columns = () => [
  { id: 'name', title: '이름', editable: true, width: 120 },
  { id: 'team', title: '팀', editable: true, width: 90 },
  { id: 'qty', title: '수량', kind: 'number', editable: true, width: 80 },
];
export const options = {
  density: 'normal', // 틀이 높이를 키우지 않게 고정(두 그리드 높이를 직접 정한다)
  columns: columns(),
  rowsStore: store,
  ariaLabel: '개발 팀',
};
let second = null;
let host = null;
export function setup(api, log) {
  api.setFilterModel({
    match: 'all',
    items: [{ columnId: 'team', operator: 'equals', value: '개발' }],
  });
  const first = document.getElementById('grid');
  host = document.createElement('div');
  host.style.cssText = 'height:260px;margin-top:10px';
  if (first !== null)
    first.after(host);
  else
    document.body.append(host);
  second = MaxGrid.createGrid(host, {
    locale: 'ko',
    columns: columns(),
    rowsStore: store,
    ariaLabel: '수량 5 이상',
  });
  second.setFilterModel({ match: 'all', items: [{ columnId: 'qty', operator: 'gte', value: 5 }] });
  log({
    ko: '위는 팀이 개발인 행, 아래는 수량 5 이상인 행만 보여요. 버튼으로 값을 바꿔 두 필터가 다시 판정하는 걸 보세요.',
    en: 'The top grid shows the Dev team, the bottom grid shows quantity 5 or more. Use the buttons to change values and watch both filters re-evaluate.',
  });
  return () => {
    second?.destroy();
    host?.remove();
    second = host = null;
  };
}
export const actions = {
  moveTeam: (api) => {
    const row = api.getRows().find((r) => r.team !== '개발');
    if (row)
      api.setCellValue(row.id, 'team', '개발');
  },
  raise: () => {
    const low = store.getRows().find((r) => r.qty < 5);
    if (low)
      store.updateRow(low.id, { qty: 20 });
  },
  lower: () => {
    const high = store.getRows().find((r) => r.qty >= 5);
    if (high)
      store.updateRow(high.id, { qty: 1 });
  },
};

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="moveTeam">Move to Dev</button>
    <button type="button" id="raise">Raise qty</button>
    <button type="button" id="lower">Lower qty</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('moveTeam').addEventListener('click', () => actions.moveTeam(api));
document.getElementById('raise').addEventListener('click', () => actions.raise(api));
document.getElementById('lower').addEventListener('click', () => actions.lower(api));
example.js
const store = MaxGrid.createRowsStore([
  { id: 1, name: 'Liam Carter', team: 'Dev', qty: 4 },
  { id: 2, name: 'Emma Stone', team: 'Ops', qty: 9 },
  { id: 3, name: 'Noah Reed', team: 'Design', qty: 2 },
  { id: 4, name: 'Ava Brooks', team: 'Dev', qty: 12 },
  { id: 5, name: 'Mia Lopez', team: 'Ops', qty: 6 },
], { rowKey: 'id' });
// Two grids on one store keep different filters. The top one shows only the Dev team, the bottom one only rows with quantity 5 or more.
// When a row changes in one grid, the other grid's filter judges it again and the row appears or disappears.
const columns = () => [
  { id: 'name', title: 'Name', editable: true, width: 120 },
  { id: 'team', title: 'Team', editable: true, width: 90 },
  { id: 'qty', title: 'Qty', kind: 'number', editable: true, width: 80 },
];
export const options = {
  density: 'normal', // keeps the frame from scaling heights (both grid heights are set here)
  columns: columns(),
  rowsStore: store,
  ariaLabel: 'Dev team',
};
let second = null;
let host = null;
export function setup(api, log) {
  api.setFilterModel({
    match: 'all',
    items: [{ columnId: 'team', operator: 'equals', value: 'Dev' }],
  });
  const first = document.getElementById('grid');
  host = document.createElement('div');
  host.style.cssText = 'height:260px;margin-top:10px';
  if (first !== null)
    first.after(host);
  else
    document.body.append(host);
  second = MaxGrid.createGrid(host, {
    locale: 'en',
    columns: columns(),
    rowsStore: store,
    ariaLabel: 'Quantity 5 or more',
  });
  second.setFilterModel({ match: 'all', items: [{ columnId: 'qty', operator: 'gte', value: 5 }] });
  log('The top grid shows the Dev team, the bottom grid shows quantity 5 or more. Use the buttons to change values and watch both filters re-evaluate.');
  return () => {
    second?.destroy();
    host?.remove();
    second = host = null;
  };
}
export const actions = {
  moveTeam: (api) => {
    const row = api.getRows().find((r) => r.team !== 'Dev');
    if (row)
      api.setCellValue(row.id, 'team', 'Dev');
  },
  raise: () => {
    const low = store.getRows().find((r) => r.qty < 5);
    if (low)
      store.updateRow(low.id, { qty: 20 });
  },
  lower: () => {
    const high = store.getRows().find((r) => r.qty >= 5);
    if (high)
      store.updateRow(high.id, { qty: 1 });
  },
};