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

범위 선택과 머리글 전체 선택Range selection and select all

Shift+클릭으로 두 행 사이를 한 번에 고르고, 머리글 체크박스로 전체를 켜고 끌 수 있어요. 일부만 골랐을 때는 머리글 체크박스가 '일부' 모양이 돼요. 같은 일을 코드(범위, 전체, 반전)로도 해요.Shift+click selects everything between two rows and the header checkbox toggles all rows. With a partial selection it shows a mixed state. The same actions (range, all, invert) also work from code.

입문Beginner
직접 써 보기Live preview범위 선택과 머리글 전체 선택Range selection and select all

코드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="selectAll">전체 선택</button>
    <button type="button" id="rows2to5">2~5번째 행</button>
    <button type="button" id="invert">선택 반전</button>
    <button type="button" id="clear">선택 지우기</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('selectAll').addEventListener('click', () => actions.selectAll(api));
document.getElementById('rows2to5').addEventListener('click', () => actions.rows2to5(api));
document.getElementById('invert').addEventListener('click', () => actions.invert(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));
example.js
const customers = [
  '한빛물류',
  '푸른하늘식품',
  '북극성무역',
  '다온상사',
  '새벽농원',
  '모아패션',
  '바다유통',
  '해든전자',
];
const statuses = ['결제 완료', '대기', '연체'];
export const options = {
  columns: [
    { id: 'customer', title: '고객사', width: 150 },
    { id: 'status', title: '상태', width: 110 },
    {
      id: 'amount',
      title: '금액(원)',
      kind: 'number',
      width: 120,
      align: 'end',
      formatValue: (value) => Number(value).toLocaleString('ko-KR'),
    },
  ],
  rows: customers.map((customer, i) => ({
    id: i + 1,
    customer,
    status: statuses[(i * 2) % 3],
    amount: 430000 + ((i * 613000) % 2900000),
  })),
  rowKey: 'id',
  selection: { mode: 'multiple', checkbox: true },
};
export function setup(api, log) {
  return api.on('selectionChanged', ({ selectedKeys }) => log({
    ko: `${api.getRowCount()}행 중 ${selectedKeys.length}행 선택`,
    en: `${selectedKeys.length} of ${api.getRowCount()} rows selected`,
  }));
}
// 머리글의 체크박스는 전체 선택을 켜고 끄고, 일부만 골랐을 때는 '일부' 모양(—)이 돼요.
// 아래 버튼은 같은 일을 코드로 해요. 보이는 행(필터·정렬 뒤)을 기준으로 범위를 잡아요.
export const actions = {
  selectAll: (api) => api.setSelectedKeys(api.getRows().map((row) => row.id)),
  rows2to5: (api) => api.setSelectedKeys(api
    .getVisibleRows()
    .slice(1, 5)
    .map((row) => row.id)),
  invert: (api) => {
    const picked = new Set(api.getSelectedKeys());
    api.setSelectedKeys(api
      .getRows()
      .filter((row) => !picked.has(row.id))
      .map((row) => row.id));
  },
  clear: (api) => api.clearSelection(),
};

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="selectAll">Select all</button>
    <button type="button" id="rows2to5">Rows 2 to 5</button>
    <button type="button" id="invert">Invert selection</button>
    <button type="button" id="clear">Clear selection</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('selectAll').addEventListener('click', () => actions.selectAll(api));
document.getElementById('rows2to5').addEventListener('click', () => actions.rows2to5(api));
document.getElementById('invert').addEventListener('click', () => actions.invert(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));
example.js
const customers = [
  'Bright Logistics',
  'Blue Sky Foods',
  'Polaris Trading',
  'Dawn Corp',
  'Sunrise Farm',
  'Moa Fashion',
  'Ocean Retail',
  'Haeden Electronics',
];
const statuses = ['Paid', 'Pending', 'Overdue'];
export const options = {
  columns: [
    { id: 'customer', title: 'Customer', width: 170 },
    { id: 'status', title: 'Status', width: 110 },
    {
      id: 'amount',
      title: 'Amount ($)',
      kind: 'number',
      width: 120,
      align: 'end',
      formatValue: (value) => Number(value).toLocaleString('en-US'),
    },
  ],
  rows: customers.map((customer, i) => ({
    id: i + 1,
    customer,
    status: statuses[(i * 2) % 3],
    amount: 430 + ((i * 613) % 2900),
  })),
  rowKey: 'id',
  selection: { mode: 'multiple', checkbox: true },
};
export function setup(api, log) {
  return api.on('selectionChanged', ({ selectedKeys }) => log(`${selectedKeys.length} of ${api.getRowCount()} rows selected`));
}
// The header checkbox toggles select-all, and shows a partial mark (—) when only some rows are selected.
// The buttons below do the same in code. Ranges use the visible rows (after filter and sort).
export const actions = {
  selectAll: (api) => api.setSelectedKeys(api.getRows().map((row) => row.id)),
  rows2to5: (api) => api.setSelectedKeys(api
    .getVisibleRows()
    .slice(1, 5)
    .map((row) => row.id)),
  invert: (api) => {
    const picked = new Set(api.getSelectedKeys());
    api.setSelectedKeys(api
      .getRows()
      .filter((row) => !picked.has(row.id))
      .map((row) => row.id));
  },
  clear: (api) => api.clearSelection(),
};