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

클릭으로 선택하는 방식 바꾸기Change how clicks select rows

selection의 clickToggle, checkboxOnly, canDeselect로 클릭 선택 방식을 바꾸고, focusable, textSelectable, cellMultiRange 옵션과 거꾸로 끈 셀 구간(reversed)도 확인해요. 모두 옵트인이고 기본은 그대로예요.Change click selection with the clickToggle, checkboxOnly and canDeselect selection options, and try the focusable, textSelectable and cellMultiRange options and the reversed flag of backwards-dragged cell ranges. All are opt-in; defaults are unchanged.

활용Intermediate
직접 써 보기Live preview클릭으로 선택하는 방식 바꾸기Change how clicks select rows

코드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="clickToggle">
      <option value="off" selected>그대로</option>
      <option value="on">해제</option>
    </select>
    <select aria-label="선택 방법" id="checkboxOnly">
      <option value="off" selected>행 클릭 + 체크박스</option>
      <option value="on">체크박스·Space만</option>
    </select>
    <select aria-label="해제" id="canDeselect">
      <option value="allowed" selected>가능</option>
      <option value="blocked">막기</option>
    </select>
    <select aria-label="칸 포커스" id="focusable">
      <option value="on" selected>켜기</option>
      <option value="off">끄기</option>
    </select>
    <select aria-label="본문 글자 선택" id="textSelectable">
      <option value="off" selected>막기</option>
      <option value="on">허용</option>
    </select>
    <select aria-label="셀 구간" id="multiRange">
      <option value="multi" selected>여러 개</option>
      <option value="single">하나만</option>
    </select>
  </div>
  <div id="grid" style="height: 480px"></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('clickToggle').addEventListener('change', (event) => actions.clickToggle(api, event.target.value));
document.getElementById('checkboxOnly').addEventListener('change', (event) => actions.checkboxOnly(api, event.target.value));
document.getElementById('canDeselect').addEventListener('change', (event) => actions.canDeselect(api, event.target.value));
document.getElementById('focusable').addEventListener('change', (event) => actions.focusable(api, event.target.value));
document.getElementById('textSelectable').addEventListener('change', (event) => actions.textSelectable(api, event.target.value));
document.getElementById('multiRange').addEventListener('change', (event) => actions.multiRange(api, event.target.value));
example.js
const names = ['김민준', '이서연', '박지호', '최유나', '정도윤', '한소희', '오세훈', '윤지아'];
const flags = {
  clickToggle: false,
  checkboxOnly: false,
  canDeselect: true,
  focusable: true,
  textSelectable: false,
  cellMultiRange: true,
};
const build = () => ({
  selection: {
    mode: 'multiple',
    checkbox: true,
    clickToggle: flags.clickToggle,
    checkboxOnly: flags.checkboxOnly,
    canDeselect: flags.canDeselect,
  },
  focusable: flags.focusable,
  cellMultiRange: flags.cellMultiRange,
  // false로 두면 글자 선택이 막혀요. 켜면 본문 글자를 끌어서 복사할 수 있어요.
  textSelectable: flags.textSelectable,
});
export const options = {
  columns: [
    { id: 'name', title: '이름', width: 120 },
    { id: 'team', title: '팀', width: 100 },
  ],
  rows: Array.from({ length: 8 }, (_, i) => ({
    id: i + 1,
    name: names[i],
    team: ['개발', '디자인', '영업', '인사'][i % 4],
  })),
  rowKey: 'id',
  cellSelection: 'multiple',
  ...build(),
};
let say = () => { };
export function setup(api, log) {
  say = log;
  Object.assign(flags, {
    clickToggle: false,
    checkboxOnly: false,
    canDeselect: true,
    focusable: true,
    textSelectable: false,
    cellMultiRange: true,
  });
  const off = api.on('selectionChanged', ({ selectedKeys }) => log({
    ko: `선택: ${selectedKeys.join(', ') || '없음'}`,
    en: `Selected: ${selectedKeys.join(', ') || 'none'}`,
  }));
  const offCells = api.on('cellSelectionChanged', ({ ranges }) => log({
    ko: `셀 구간 ${ranges.length}개${ranges.some((r) => r.reversed === true) ? '(거꾸로 끈 구간 있음)' : ''}`,
    en: `${ranges.length} cell ranges${ranges.some((r) => r.reversed === true) ? ' (one was dragged backwards)' : ''}`,
  }));
  return () => {
    off();
    offCells();
  };
}
function toggle(api, name, value, on) {
  flags[name] = name === 'canDeselect' ? value !== 'blocked' : value === 'on';
  api.setOptions(build());
  say(on);
}
export const actions = {
  clickToggle: (api, value) => toggle(api, 'clickToggle', value, {
    ko: value === 'on' ? '선택된 행을 다시 누르면 풀려요.' : '다시 눌러도 선택이 유지돼요.',
    en: value === 'on'
      ? 'Clicking a selected row again deselects it.'
      : 'Clicking again keeps the selection.',
  }),
  checkboxOnly: (api, value) => toggle(api, 'checkboxOnly', value, {
    ko: value === 'on'
      ? '행을 눌러도 선택되지 않아요. 체크박스나 Space를 쓰세요.'
      : '행을 누르면 선택돼요.',
    en: value === 'on'
      ? 'Clicking a row does not select. Use the checkbox or Space.'
      : 'Clicking a row selects it.',
  }),
  canDeselect: (api, value) => toggle(api, 'canDeselect', value, {
    ko: value === 'blocked'
      ? '마우스·키보드로는 선택을 풀 수 없어요(코드로는 풀 수 있어요).'
      : '선택을 풀 수 있어요.',
    en: value === 'blocked'
      ? 'You cannot deselect with mouse or keyboard (code still can).'
      : 'Deselecting works.',
  }),
  focusable: (api, value) => toggle(api, 'focusable', value === 'off' ? 'off' : 'on', {
    ko: value === 'off'
      ? '칸 포커스와 화살표 이동이 꺼졌어요(접근성에 주의하세요).'
      : '칸 포커스가 켜져 있어요.',
    en: value === 'off'
      ? 'Cell focus and arrow keys are off (mind accessibility).'
      : 'Cell focus is on.',
  }),
  textSelectable: (api, value) => toggle(api, 'textSelectable', value, {
    ko: value === 'on'
      ? '본문 글자를 끌어서 고를 수 있어요(셀 끌기 선택은 쉬어요).'
      : '본문 글자 선택이 막혀요.',
    en: value === 'on'
      ? 'Body text can be dragged and selected (cell drag selection rests).'
      : 'Body text selection is blocked.',
  }),
  multiRange: (api, value) => toggle(api, 'cellMultiRange', value === 'single' ? 'off' : 'on', {
    ko: value === 'single'
      ? '새 셀 구간이 이전 구간을 대신해요.'
      : 'Ctrl로 셀 구간을 여러 개 만들 수 있어요.',
    en: value === 'single'
      ? 'A new cell range replaces the old one.'
      : 'Ctrl lets you build several ranges.',
  }),
};

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="Click again" id="clickToggle">
      <option value="off" selected>Keep</option>
      <option value="on">Deselect</option>
    </select>
    <select aria-label="Selects with" id="checkboxOnly">
      <option value="off" selected>Row click + checkbox</option>
      <option value="on">Checkbox and Space only</option>
    </select>
    <select aria-label="Deselect" id="canDeselect">
      <option value="allowed" selected>Allowed</option>
      <option value="blocked">Block</option>
    </select>
    <select aria-label="Cell focus" id="focusable">
      <option value="on" selected>On</option>
      <option value="off">Off</option>
    </select>
    <select aria-label="Body text selection" id="textSelectable">
      <option value="off" selected>Blocked</option>
      <option value="on">Allowed</option>
    </select>
    <select aria-label="Cell ranges" id="multiRange">
      <option value="multi" selected>Several</option>
      <option value="single">One only</option>
    </select>
  </div>
  <div id="grid" style="height: 480px"></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('clickToggle').addEventListener('change', (event) => actions.clickToggle(api, event.target.value));
document.getElementById('checkboxOnly').addEventListener('change', (event) => actions.checkboxOnly(api, event.target.value));
document.getElementById('canDeselect').addEventListener('change', (event) => actions.canDeselect(api, event.target.value));
document.getElementById('focusable').addEventListener('change', (event) => actions.focusable(api, event.target.value));
document.getElementById('textSelectable').addEventListener('change', (event) => actions.textSelectable(api, event.target.value));
document.getElementById('multiRange').addEventListener('change', (event) => actions.multiRange(api, event.target.value));
example.js
const names = [
  'Liam Carter',
  'Emma Stone',
  'Noah Reed',
  'Olivia Hayes',
  'Ethan Brooks',
  'Ava Morgan',
  'Mason Clark',
  'Sophia Bell',
];
const flags = {
  clickToggle: false,
  checkboxOnly: false,
  canDeselect: true,
  focusable: true,
  textSelectable: false,
  cellMultiRange: true,
};
const build = () => ({
  selection: {
    mode: 'multiple',
    checkbox: true,
    clickToggle: flags.clickToggle,
    checkboxOnly: flags.checkboxOnly,
    canDeselect: flags.canDeselect,
  },
  focusable: flags.focusable,
  cellMultiRange: flags.cellMultiRange,
  // false blocks text selection. When on, you can drag across body text to copy it.
  textSelectable: flags.textSelectable,
});
export const options = {
  columns: [
    { id: 'name', title: 'Name', width: 140 },
    { id: 'team', title: 'Team', width: 100 },
  ],
  rows: Array.from({ length: 8 }, (_, i) => ({
    id: i + 1,
    name: names[i],
    team: ['Dev', 'Design', 'Sales', 'HR'][i % 4],
  })),
  rowKey: 'id',
  cellSelection: 'multiple',
  ...build(),
};
let say = () => { };
export function setup(api, log) {
  say = log;
  Object.assign(flags, {
    clickToggle: false,
    checkboxOnly: false,
    canDeselect: true,
    focusable: true,
    textSelectable: false,
    cellMultiRange: true,
  });
  const off = api.on('selectionChanged', ({ selectedKeys }) => log(`Selected: ${selectedKeys.join(', ') || 'none'}`));
  const offCells = api.on('cellSelectionChanged', ({ ranges }) => log(`${ranges.length} cell ranges${ranges.some((r) => r.reversed === true) ? ' (one was dragged backwards)' : ''}`));
  return () => {
    off();
    offCells();
  };
}
function toggle(api, name, value, on) {
  flags[name] = name === 'canDeselect' ? value !== 'blocked' : value === 'on';
  api.setOptions(build());
  say(on);
}
export const actions = {
  clickToggle: (api, value) => toggle(api, 'clickToggle', value, value === 'on'
    ? 'Clicking a selected row again deselects it.'
    : 'Clicking again keeps the selection.'),
  checkboxOnly: (api, value) => toggle(api, 'checkboxOnly', value, value === 'on'
    ? 'Clicking a row does not select. Use the checkbox or Space.'
    : 'Clicking a row selects it.'),
  canDeselect: (api, value) => toggle(api, 'canDeselect', value, value === 'blocked'
    ? 'You cannot deselect with mouse or keyboard (code still can).'
    : 'Deselecting works.'),
  focusable: (api, value) => toggle(api, 'focusable', value === 'off' ? 'off' : 'on', value === 'off'
    ? 'Cell focus and arrow keys are off (mind accessibility).'
    : 'Cell focus is on.'),
  textSelectable: (api, value) => toggle(api, 'textSelectable', value, value === 'on'
    ? 'Body text can be dragged and selected (cell drag selection rests).'
    : 'Body text selection is blocked.'),
  multiRange: (api, value) => toggle(api, 'cellMultiRange', value === 'single' ? 'off' : 'on', value === 'single'
    ? 'A new cell range replaces the old one.'
    : 'Ctrl lets you build several ranges.'),
};