클릭으로 선택하는 방식 바꾸기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.
코드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)
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<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>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));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.',
}),
};<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>클릭으로 선택하는 방식 바꾸기</title>
</head>
<body>
<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>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
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,
});
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 = () => { };
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);
}
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.',
}),
};
options.ariaLabel = "클릭으로 선택하는 방식 바꾸기";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
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));
</script>
</body>
</html>버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<select
aria-label="다시 누르면"
disabled={!api}
onChange={(event) => api && actions.clickToggle(api, event.target.value)}
>
<option value="off">그대로</option>
<option value="on">해제</option>
</select>
<select defaultValue="off"
aria-label="선택 방법"
disabled={!api}
onChange={(event) => api && actions.checkboxOnly(api, event.target.value)}
>
<option value="off">행 클릭 + 체크박스</option>
<option value="on">체크박스·Space만</option>
</select>
<select defaultValue="off"
aria-label="해제"
disabled={!api}
onChange={(event) => api && actions.canDeselect(api, event.target.value)}
>
<option value="allowed">가능</option>
<option value="blocked">막기</option>
</select>
<select defaultValue="allowed"
aria-label="칸 포커스"
disabled={!api}
onChange={(event) => api && actions.focusable(api, event.target.value)}
>
<option value="on">켜기</option>
<option value="off">끄기</option>
</select>
<select defaultValue="on"
aria-label="본문 글자 선택"
disabled={!api}
onChange={(event) => api && actions.textSelectable(api, event.target.value)}
>
<option value="off">막기</option>
<option value="on">허용</option>
</select>
<select defaultValue="off"
aria-label="셀 구간"
disabled={!api}
onChange={(event) => api && actions.multiRange(api, event.target.value)}
>
<option value="multi">여러 개</option>
<option value="single">하나만</option>
</select>
</div>
<Grid style={{ height: 480 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
team: string;
}
const names = ['김민준', '이서연', '박지호', '최유나', '정도윤', '한소희', '오세훈', '윤지아'];
// 클릭으로 선택하는 방식을 셋으로 바꿔요: clickToggle(선택된 행을 다시 누르면 해제), checkboxOnly(체크박스와 Space로만 선택),
// canDeselect:false(클릭·체크박스·Ctrl+클릭으로는 해제할 수 없음). 그 밖에 focusable, textSelectable, cellMultiRange도 켜 볼 수 있어요.
type Flags = {
clickToggle: boolean;
checkboxOnly: boolean;
canDeselect: boolean;
focusable: boolean;
textSelectable: boolean;
cellMultiRange: boolean;
};
const flags: Flags = {
clickToggle: false,
checkboxOnly: false,
canDeselect: true,
focusable: true,
textSelectable: false,
cellMultiRange: true,
};
const build = (): Partial<GridOptions<Row>> => ({
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: GridOptions<Row> = {
columns: [
{ id: 'name', title: '이름', width: 120 },
{ id: 'team', title: '팀', width: 100 },
],
rows: Array.from({ length: 8 }, (_, i) => ({
id: i + 1,
name: names[i] as string,
team: ['개발', '디자인', '영업', '인사'][i % 4] as string,
})),
rowKey: 'id',
cellSelection: 'multiple',
...build(),
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>,
name: keyof Flags,
value: string | undefined,
on: LogText,
): void {
flags[name] = name === 'canDeselect' ? value !== 'blocked' : value === 'on';
api.setOptions(build());
say(on);
}
export const actions = {
clickToggle: (api: GridApi<Row>, value?: string) =>
toggle(api, 'clickToggle', value, {
ko: value === 'on' ? '선택된 행을 다시 누르면 풀려요.' : '다시 눌러도 선택이 유지돼요.',
en:
value === 'on'
? 'Clicking a selected row again deselects it.'
: 'Clicking again keeps the selection.',
}),
checkboxOnly: (api: GridApi<Row>, value?: string) =>
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: GridApi<Row>, value?: string) =>
toggle(api, 'canDeselect', value, {
ko:
value === 'blocked'
? '마우스·키보드로는 선택을 풀 수 없어요(코드로는 풀 수 있어요).'
: '선택을 풀 수 있어요.',
en:
value === 'blocked'
? 'You cannot deselect with mouse or keyboard (code still can).'
: 'Deselecting works.',
}),
focusable: (api: GridApi<Row>, value?: string) =>
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: GridApi<Row>, value?: string) =>
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: GridApi<Row>, value?: string) =>
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.',
}),
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<select
aria-label="다시 누르면"
:disabled="!api"
@change="api && actions.clickToggle(api, ($event.target as HTMLSelectElement).value)"
>
<option value="off" selected>그대로</option>
<option value="on">해제</option>
</select>
<select
aria-label="선택 방법"
:disabled="!api"
@change="api && actions.checkboxOnly(api, ($event.target as HTMLSelectElement).value)"
>
<option value="off" selected>행 클릭 + 체크박스</option>
<option value="on">체크박스·Space만</option>
</select>
<select
aria-label="해제"
:disabled="!api"
@change="api && actions.canDeselect(api, ($event.target as HTMLSelectElement).value)"
>
<option value="allowed" selected>가능</option>
<option value="blocked">막기</option>
</select>
<select
aria-label="칸 포커스"
:disabled="!api"
@change="api && actions.focusable(api, ($event.target as HTMLSelectElement).value)"
>
<option value="on" selected>켜기</option>
<option value="off">끄기</option>
</select>
<select
aria-label="본문 글자 선택"
:disabled="!api"
@change="api && actions.textSelectable(api, ($event.target as HTMLSelectElement).value)"
>
<option value="off" selected>막기</option>
<option value="on">허용</option>
</select>
<select
aria-label="셀 구간"
:disabled="!api"
@change="api && actions.multiRange(api, ($event.target as HTMLSelectElement).value)"
>
<option value="multi" selected>여러 개</option>
<option value="single">하나만</option>
</select>
</div>
<Grid style="height: 480px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
team: string;
}
const names = ['김민준', '이서연', '박지호', '최유나', '정도윤', '한소희', '오세훈', '윤지아'];
// 클릭으로 선택하는 방식을 셋으로 바꿔요: clickToggle(선택된 행을 다시 누르면 해제), checkboxOnly(체크박스와 Space로만 선택),
// canDeselect:false(클릭·체크박스·Ctrl+클릭으로는 해제할 수 없음). 그 밖에 focusable, textSelectable, cellMultiRange도 켜 볼 수 있어요.
type Flags = {
clickToggle: boolean;
checkboxOnly: boolean;
canDeselect: boolean;
focusable: boolean;
textSelectable: boolean;
cellMultiRange: boolean;
};
const flags: Flags = {
clickToggle: false,
checkboxOnly: false,
canDeselect: true,
focusable: true,
textSelectable: false,
cellMultiRange: true,
};
const build = (): Partial<GridOptions<Row>> => ({
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: GridOptions<Row> = {
columns: [
{ id: 'name', title: '이름', width: 120 },
{ id: 'team', title: '팀', width: 100 },
],
rows: Array.from({ length: 8 }, (_, i) => ({
id: i + 1,
name: names[i] as string,
team: ['개발', '디자인', '영업', '인사'][i % 4] as string,
})),
rowKey: 'id',
cellSelection: 'multiple',
...build(),
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>,
name: keyof Flags,
value: string | undefined,
on: LogText,
): void {
flags[name] = name === 'canDeselect' ? value !== 'blocked' : value === 'on';
api.setOptions(build());
say(on);
}
export const actions = {
clickToggle: (api: GridApi<Row>, value?: string) =>
toggle(api, 'clickToggle', value, {
ko: value === 'on' ? '선택된 행을 다시 누르면 풀려요.' : '다시 눌러도 선택이 유지돼요.',
en:
value === 'on'
? 'Clicking a selected row again deselects it.'
: 'Clicking again keeps the selection.',
}),
checkboxOnly: (api: GridApi<Row>, value?: string) =>
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: GridApi<Row>, value?: string) =>
toggle(api, 'canDeselect', value, {
ko:
value === 'blocked'
? '마우스·키보드로는 선택을 풀 수 없어요(코드로는 풀 수 있어요).'
: '선택을 풀 수 있어요.',
en:
value === 'blocked'
? 'You cannot deselect with mouse or keyboard (code still can).'
: 'Deselecting works.',
}),
focusable: (api: GridApi<Row>, value?: string) =>
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: GridApi<Row>, value?: string) =>
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: GridApi<Row>, value?: string) =>
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.',
}),
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<select
aria-label="다시 누르면"
[disabled]="!api"
(change)="api && actions.clickToggle(api, $any($event.target).value)"
>
<option value="off" selected>그대로</option>
<option value="on">해제</option>
</select>
<select
aria-label="선택 방법"
[disabled]="!api"
(change)="api && actions.checkboxOnly(api, $any($event.target).value)"
>
<option value="off" selected>행 클릭 + 체크박스</option>
<option value="on">체크박스·Space만</option>
</select>
<select
aria-label="해제"
[disabled]="!api"
(change)="api && actions.canDeselect(api, $any($event.target).value)"
>
<option value="allowed" selected>가능</option>
<option value="blocked">막기</option>
</select>
<select
aria-label="칸 포커스"
[disabled]="!api"
(change)="api && actions.focusable(api, $any($event.target).value)"
>
<option value="on" selected>켜기</option>
<option value="off">끄기</option>
</select>
<select
aria-label="본문 글자 선택"
[disabled]="!api"
(change)="api && actions.textSelectable(api, $any($event.target).value)"
>
<option value="off" selected>막기</option>
<option value="on">허용</option>
</select>
<select
aria-label="셀 구간"
[disabled]="!api"
(change)="api && actions.multiRange(api, $any($event.target).value)"
>
<option value="multi" selected>여러 개</option>
<option value="single">하나만</option>
</select>
</div>
<mx-grid style="height: 480px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
team: string;
}
const names = ['김민준', '이서연', '박지호', '최유나', '정도윤', '한소희', '오세훈', '윤지아'];
// 클릭으로 선택하는 방식을 셋으로 바꿔요: clickToggle(선택된 행을 다시 누르면 해제), checkboxOnly(체크박스와 Space로만 선택),
// canDeselect:false(클릭·체크박스·Ctrl+클릭으로는 해제할 수 없음). 그 밖에 focusable, textSelectable, cellMultiRange도 켜 볼 수 있어요.
type Flags = {
clickToggle: boolean;
checkboxOnly: boolean;
canDeselect: boolean;
focusable: boolean;
textSelectable: boolean;
cellMultiRange: boolean;
};
const flags: Flags = {
clickToggle: false,
checkboxOnly: false,
canDeselect: true,
focusable: true,
textSelectable: false,
cellMultiRange: true,
};
const build = (): Partial<GridOptions<Row>> => ({
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: GridOptions<Row> = {
columns: [
{ id: 'name', title: '이름', width: 120 },
{ id: 'team', title: '팀', width: 100 },
],
rows: Array.from({ length: 8 }, (_, i) => ({
id: i + 1,
name: names[i] as string,
team: ['개발', '디자인', '영업', '인사'][i % 4] as string,
})),
rowKey: 'id',
cellSelection: 'multiple',
...build(),
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>,
name: keyof Flags,
value: string | undefined,
on: LogText,
): void {
flags[name] = name === 'canDeselect' ? value !== 'blocked' : value === 'on';
api.setOptions(build());
say(on);
}
export const actions = {
clickToggle: (api: GridApi<Row>, value?: string) =>
toggle(api, 'clickToggle', value, {
ko: value === 'on' ? '선택된 행을 다시 누르면 풀려요.' : '다시 눌러도 선택이 유지돼요.',
en:
value === 'on'
? 'Clicking a selected row again deselects it.'
: 'Clicking again keeps the selection.',
}),
checkboxOnly: (api: GridApi<Row>, value?: string) =>
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: GridApi<Row>, value?: string) =>
toggle(api, 'canDeselect', value, {
ko:
value === 'blocked'
? '마우스·키보드로는 선택을 풀 수 없어요(코드로는 풀 수 있어요).'
: '선택을 풀 수 있어요.',
en:
value === 'blocked'
? 'You cannot deselect with mouse or keyboard (code still can).'
: 'Deselecting works.',
}),
focusable: (api: GridApi<Row>, value?: string) =>
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: GridApi<Row>, value?: string) =>
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: GridApi<Row>, value?: string) =>
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.',
}),
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<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>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));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.'),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Change how clicks select rows</title>
</head>
<body>
<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>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
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,
});
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 = () => { };
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);
}
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.'),
};
options.ariaLabel = "Change how clicks select rows";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
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));
</script>
</body>
</html>When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<select
aria-label="Click again"
disabled={!api}
onChange={(event) => api && actions.clickToggle(api, event.target.value)}
>
<option value="off">Keep</option>
<option value="on">Deselect</option>
</select>
<select defaultValue="off"
aria-label="Selects with"
disabled={!api}
onChange={(event) => api && actions.checkboxOnly(api, event.target.value)}
>
<option value="off">Row click + checkbox</option>
<option value="on">Checkbox and Space only</option>
</select>
<select defaultValue="off"
aria-label="Deselect"
disabled={!api}
onChange={(event) => api && actions.canDeselect(api, event.target.value)}
>
<option value="allowed">Allowed</option>
<option value="blocked">Block</option>
</select>
<select defaultValue="allowed"
aria-label="Cell focus"
disabled={!api}
onChange={(event) => api && actions.focusable(api, event.target.value)}
>
<option value="on">On</option>
<option value="off">Off</option>
</select>
<select defaultValue="on"
aria-label="Body text selection"
disabled={!api}
onChange={(event) => api && actions.textSelectable(api, event.target.value)}
>
<option value="off">Blocked</option>
<option value="on">Allowed</option>
</select>
<select defaultValue="off"
aria-label="Cell ranges"
disabled={!api}
onChange={(event) => api && actions.multiRange(api, event.target.value)}
>
<option value="multi">Several</option>
<option value="single">One only</option>
</select>
</div>
<Grid style={{ height: 480 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
team: string;
}
const names = [
'Liam Carter',
'Emma Stone',
'Noah Reed',
'Olivia Hayes',
'Ethan Brooks',
'Ava Morgan',
'Mason Clark',
'Sophia Bell',
];
// Three ways to select by click: clickToggle (clicking a selected row again deselects it), checkboxOnly (select only with the checkbox and Space),
// canDeselect:false (cannot deselect by click, checkbox or Ctrl+click). focusable, textSelectable and cellMultiRange can be toggled too.
type Flags = {
clickToggle: boolean;
checkboxOnly: boolean;
canDeselect: boolean;
focusable: boolean;
textSelectable: boolean;
cellMultiRange: boolean;
};
const flags: Flags = {
clickToggle: false,
checkboxOnly: false,
canDeselect: true,
focusable: true,
textSelectable: false,
cellMultiRange: true,
};
const build = (): Partial<GridOptions<Row>> => ({
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: GridOptions<Row> = {
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] as string,
team: ['Dev', 'Design', 'Sales', 'HR'][i % 4] as string,
})),
rowKey: 'id',
cellSelection: 'multiple',
...build(),
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>,
name: keyof Flags,
value: string | undefined,
on: LogText,
): void {
flags[name] = name === 'canDeselect' ? value !== 'blocked' : value === 'on';
api.setOptions(build());
say(on);
}
export const actions = {
clickToggle: (api: GridApi<Row>, value?: string) =>
toggle(
api,
'clickToggle',
value,
value === 'on'
? 'Clicking a selected row again deselects it.'
: 'Clicking again keeps the selection.',
),
checkboxOnly: (api: GridApi<Row>, value?: string) =>
toggle(
api,
'checkboxOnly',
value,
value === 'on'
? 'Clicking a row does not select. Use the checkbox or Space.'
: 'Clicking a row selects it.',
),
canDeselect: (api: GridApi<Row>, value?: string) =>
toggle(
api,
'canDeselect',
value,
value === 'blocked'
? 'You cannot deselect with mouse or keyboard (code still can).'
: 'Deselecting works.',
),
focusable: (api: GridApi<Row>, value?: string) =>
toggle(
api,
'focusable',
value === 'off' ? 'off' : 'on',
value === 'off'
? 'Cell focus and arrow keys are off (mind accessibility).'
: 'Cell focus is on.',
),
textSelectable: (api: GridApi<Row>, value?: string) =>
toggle(
api,
'textSelectable',
value,
value === 'on'
? 'Body text can be dragged and selected (cell drag selection rests).'
: 'Body text selection is blocked.',
),
multiRange: (api: GridApi<Row>, value?: string) =>
toggle(
api,
'cellMultiRange',
value === 'single' ? 'off' : 'on',
value === 'single'
? 'A new cell range replaces the old one.'
: 'Ctrl lets you build several ranges.',
),
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<select
aria-label="Click again"
:disabled="!api"
@change="api && actions.clickToggle(api, ($event.target as HTMLSelectElement).value)"
>
<option value="off" selected>Keep</option>
<option value="on">Deselect</option>
</select>
<select
aria-label="Selects with"
:disabled="!api"
@change="api && actions.checkboxOnly(api, ($event.target as HTMLSelectElement).value)"
>
<option value="off" selected>Row click + checkbox</option>
<option value="on">Checkbox and Space only</option>
</select>
<select
aria-label="Deselect"
:disabled="!api"
@change="api && actions.canDeselect(api, ($event.target as HTMLSelectElement).value)"
>
<option value="allowed" selected>Allowed</option>
<option value="blocked">Block</option>
</select>
<select
aria-label="Cell focus"
:disabled="!api"
@change="api && actions.focusable(api, ($event.target as HTMLSelectElement).value)"
>
<option value="on" selected>On</option>
<option value="off">Off</option>
</select>
<select
aria-label="Body text selection"
:disabled="!api"
@change="api && actions.textSelectable(api, ($event.target as HTMLSelectElement).value)"
>
<option value="off" selected>Blocked</option>
<option value="on">Allowed</option>
</select>
<select
aria-label="Cell ranges"
:disabled="!api"
@change="api && actions.multiRange(api, ($event.target as HTMLSelectElement).value)"
>
<option value="multi" selected>Several</option>
<option value="single">One only</option>
</select>
</div>
<Grid style="height: 480px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
team: string;
}
const names = [
'Liam Carter',
'Emma Stone',
'Noah Reed',
'Olivia Hayes',
'Ethan Brooks',
'Ava Morgan',
'Mason Clark',
'Sophia Bell',
];
// Three ways to select by click: clickToggle (clicking a selected row again deselects it), checkboxOnly (select only with the checkbox and Space),
// canDeselect:false (cannot deselect by click, checkbox or Ctrl+click). focusable, textSelectable and cellMultiRange can be toggled too.
type Flags = {
clickToggle: boolean;
checkboxOnly: boolean;
canDeselect: boolean;
focusable: boolean;
textSelectable: boolean;
cellMultiRange: boolean;
};
const flags: Flags = {
clickToggle: false,
checkboxOnly: false,
canDeselect: true,
focusable: true,
textSelectable: false,
cellMultiRange: true,
};
const build = (): Partial<GridOptions<Row>> => ({
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: GridOptions<Row> = {
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] as string,
team: ['Dev', 'Design', 'Sales', 'HR'][i % 4] as string,
})),
rowKey: 'id',
cellSelection: 'multiple',
...build(),
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>,
name: keyof Flags,
value: string | undefined,
on: LogText,
): void {
flags[name] = name === 'canDeselect' ? value !== 'blocked' : value === 'on';
api.setOptions(build());
say(on);
}
export const actions = {
clickToggle: (api: GridApi<Row>, value?: string) =>
toggle(
api,
'clickToggle',
value,
value === 'on'
? 'Clicking a selected row again deselects it.'
: 'Clicking again keeps the selection.',
),
checkboxOnly: (api: GridApi<Row>, value?: string) =>
toggle(
api,
'checkboxOnly',
value,
value === 'on'
? 'Clicking a row does not select. Use the checkbox or Space.'
: 'Clicking a row selects it.',
),
canDeselect: (api: GridApi<Row>, value?: string) =>
toggle(
api,
'canDeselect',
value,
value === 'blocked'
? 'You cannot deselect with mouse or keyboard (code still can).'
: 'Deselecting works.',
),
focusable: (api: GridApi<Row>, value?: string) =>
toggle(
api,
'focusable',
value === 'off' ? 'off' : 'on',
value === 'off'
? 'Cell focus and arrow keys are off (mind accessibility).'
: 'Cell focus is on.',
),
textSelectable: (api: GridApi<Row>, value?: string) =>
toggle(
api,
'textSelectable',
value,
value === 'on'
? 'Body text can be dragged and selected (cell drag selection rests).'
: 'Body text selection is blocked.',
),
multiRange: (api: GridApi<Row>, value?: string) =>
toggle(
api,
'cellMultiRange',
value === 'single' ? 'off' : 'on',
value === 'single'
? 'A new cell range replaces the old one.'
: 'Ctrl lets you build several ranges.',
),
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<select
aria-label="Click again"
[disabled]="!api"
(change)="api && actions.clickToggle(api, $any($event.target).value)"
>
<option value="off" selected>Keep</option>
<option value="on">Deselect</option>
</select>
<select
aria-label="Selects with"
[disabled]="!api"
(change)="api && actions.checkboxOnly(api, $any($event.target).value)"
>
<option value="off" selected>Row click + checkbox</option>
<option value="on">Checkbox and Space only</option>
</select>
<select
aria-label="Deselect"
[disabled]="!api"
(change)="api && actions.canDeselect(api, $any($event.target).value)"
>
<option value="allowed" selected>Allowed</option>
<option value="blocked">Block</option>
</select>
<select
aria-label="Cell focus"
[disabled]="!api"
(change)="api && actions.focusable(api, $any($event.target).value)"
>
<option value="on" selected>On</option>
<option value="off">Off</option>
</select>
<select
aria-label="Body text selection"
[disabled]="!api"
(change)="api && actions.textSelectable(api, $any($event.target).value)"
>
<option value="off" selected>Blocked</option>
<option value="on">Allowed</option>
</select>
<select
aria-label="Cell ranges"
[disabled]="!api"
(change)="api && actions.multiRange(api, $any($event.target).value)"
>
<option value="multi" selected>Several</option>
<option value="single">One only</option>
</select>
</div>
<mx-grid style="height: 480px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
team: string;
}
const names = [
'Liam Carter',
'Emma Stone',
'Noah Reed',
'Olivia Hayes',
'Ethan Brooks',
'Ava Morgan',
'Mason Clark',
'Sophia Bell',
];
// Three ways to select by click: clickToggle (clicking a selected row again deselects it), checkboxOnly (select only with the checkbox and Space),
// canDeselect:false (cannot deselect by click, checkbox or Ctrl+click). focusable, textSelectable and cellMultiRange can be toggled too.
type Flags = {
clickToggle: boolean;
checkboxOnly: boolean;
canDeselect: boolean;
focusable: boolean;
textSelectable: boolean;
cellMultiRange: boolean;
};
const flags: Flags = {
clickToggle: false,
checkboxOnly: false,
canDeselect: true,
focusable: true,
textSelectable: false,
cellMultiRange: true,
};
const build = (): Partial<GridOptions<Row>> => ({
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: GridOptions<Row> = {
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] as string,
team: ['Dev', 'Design', 'Sales', 'HR'][i % 4] as string,
})),
rowKey: 'id',
cellSelection: 'multiple',
...build(),
};
let say: (message: LogText) => void = () => {};
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
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: GridApi<Row>,
name: keyof Flags,
value: string | undefined,
on: LogText,
): void {
flags[name] = name === 'canDeselect' ? value !== 'blocked' : value === 'on';
api.setOptions(build());
say(on);
}
export const actions = {
clickToggle: (api: GridApi<Row>, value?: string) =>
toggle(
api,
'clickToggle',
value,
value === 'on'
? 'Clicking a selected row again deselects it.'
: 'Clicking again keeps the selection.',
),
checkboxOnly: (api: GridApi<Row>, value?: string) =>
toggle(
api,
'checkboxOnly',
value,
value === 'on'
? 'Clicking a row does not select. Use the checkbox or Space.'
: 'Clicking a row selects it.',
),
canDeselect: (api: GridApi<Row>, value?: string) =>
toggle(
api,
'canDeselect',
value,
value === 'blocked'
? 'You cannot deselect with mouse or keyboard (code still can).'
: 'Deselecting works.',
),
focusable: (api: GridApi<Row>, value?: string) =>
toggle(
api,
'focusable',
value === 'off' ? 'off' : 'on',
value === 'off'
? 'Cell focus and arrow keys are off (mind accessibility).'
: 'Cell focus is on.',
),
textSelectable: (api: GridApi<Row>, value?: string) =>
toggle(
api,
'textSelectable',
value,
value === 'on'
? 'Body text can be dragged and selected (cell drag selection rests).'
: 'Body text selection is blocked.',
),
multiRange: (api: GridApi<Row>, value?: string) =>
toggle(
api,
'cellMultiRange',
value === 'single' ? 'off' : 'on',
value === 'single'
? 'A new cell range replaces the old one.'
: 'Ctrl lets you build several ranges.',
),
};