버튼으로 표에 직접 명령하기 (이동·추가·선택·삭제)Command the grid from buttons (move, add, select, remove)
화면의 버튼으로 표에 직접 명령을 내려요. 맨 아래 행으로 이동하고 포커스를 옮기고, 행을 넣고, 조건에 맞는 행을 고르고, 고른 행을 지워요. 표에 명령을 내리는 통로를 React, Vue, Angular가 각각 어떤 방식으로 받는지 비교할 수 있어요. 각 탭에는 같은 예제가 그 프레임워크의 방식으로 들어 있어요.Buttons give the grid direct commands: move to the last row and focus it, add a row, select rows by a rule, and remove the selected rows. Compare how React, Vue and Angular each obtain the handle used to command the grid. Each tab holds the same example written the way that framework does it.
실행 화면은 JavaScript 버전이에요. 아래 탭의 React·Vue·Angular 코드는 같은 예제를 각 프레임워크로 직접 쓴 코드예요.The preview runs the JavaScript version. The React, Vue, Angular tabs below hold the same example written for each framework.
코드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">
<button type="button" id="focusLast">맨 아래로 이동</button>
<button type="button" id="addRow">행 추가</button>
<button type="button" id="selectDev">개발팀 고르기</button>
<button type="button" id="removeSelected">고른 행 지우기</button>
</div>
<div id="grid" style="height: 440px"></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('focusLast').addEventListener('click', () => actions.focusLast(api));
document.getElementById('addRow').addEventListener('click', () => actions.addRow(api));
document.getElementById('selectDev').addEventListener('click', () => actions.selectDev(api));
document.getElementById('removeSelected').addEventListener('click', () => actions.removeSelected(api));const DEPTS = ['개발팀', '디자인팀', '영업팀'];
export const options = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 80, align: 'end' },
{ id: 'name', title: '이름', width: 140 },
{ id: 'dept', title: '부서', width: 120 },
],
rows: Array.from({ length: 30 }, (_, i) => ({
id: i + 1,
name: `직원 ${i + 1}`,
dept: DEPTS[i % 3],
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
};
/** 새 행을 만들 때 쓰는 번호예요. 지워도 번호가 겹치지 않게 가장 큰 번호에 1을 더해요. */
export function nextRow(rows) {
const id = rows.reduce((max, row) => Math.max(max, row.id), 0) + 1;
return { id, name: `직원 ${id}`, dept: DEPTS[id % 3] };
}
let say = () => { };
export function setup(_api, log) {
say = log;
}
export const actions = {
// 맨 아래 행으로 스크롤하고, 그 행의 이름 칸에 포커스를 옮겨요.
focusLast: (api) => {
const rows = api.getVisibleRows();
const last = rows[rows.length - 1];
if (!last)
return say({ ko: '보이는 행이 없어요', en: 'No visible rows' });
api.scrollToIndex(rows.length - 1);
api.focusCell(last.id, 'name');
say({ ko: `'${last.name}' 행으로 이동했어요`, en: `Moved to row '${last.name}'` });
},
addRow: (api) => {
const row = nextRow(api.getRows());
api.addRows([row]);
say({
ko: `'${row.name}' 행을 맨 아래에 넣었어요`,
en: `Added row '${row.name}' at the bottom`,
});
},
selectDev: (api) => {
const keys = api
.getRows()
.filter((row) => row.dept === '개발팀')
.map((row) => row.id);
api.setSelectedKeys(keys);
say({
ko: `개발팀 ${keys.length}행을 골랐어요`,
en: `Selected ${keys.length} rows of the development team`,
});
},
removeSelected: (api) => {
const removed = api.removeRows(api.getSelectedKeys());
say({
ko: removed ? `${removed}행을 지웠어요` : '먼저 지울 행을 골라 주세요',
en: removed ? `Deleted ${removed} rows` : 'Select the rows to delete first',
});
},
};<!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">
<button type="button" id="focusLast">맨 아래로 이동</button>
<button type="button" id="addRow">행 추가</button>
<button type="button" id="selectDev">개발팀 고르기</button>
<button type="button" id="removeSelected">고른 행 지우기</button>
</div>
<div id="grid" style="height: 440px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const DEPTS = ['개발팀', '디자인팀', '영업팀'];
const options = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 80, align: 'end' },
{ id: 'name', title: '이름', width: 140 },
{ id: 'dept', title: '부서', width: 120 },
],
rows: Array.from({ length: 30 }, (_, i) => ({
id: i + 1,
name: `직원 ${i + 1}`,
dept: DEPTS[i % 3],
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
};
/** 새 행을 만들 때 쓰는 번호예요. 지워도 번호가 겹치지 않게 가장 큰 번호에 1을 더해요. */
function nextRow(rows) {
const id = rows.reduce((max, row) => Math.max(max, row.id), 0) + 1;
return { id, name: `직원 ${id}`, dept: DEPTS[id % 3] };
}
let say = () => { };
function setup(_api, log) {
say = log;
}
const actions = {
// 맨 아래 행으로 스크롤하고, 그 행의 이름 칸에 포커스를 옮겨요.
focusLast: (api) => {
const rows = api.getVisibleRows();
const last = rows[rows.length - 1];
if (!last)
return say({ ko: '보이는 행이 없어요', en: 'No visible rows' });
api.scrollToIndex(rows.length - 1);
api.focusCell(last.id, 'name');
say({ ko: `'${last.name}' 행으로 이동했어요`, en: `Moved to row '${last.name}'` });
},
addRow: (api) => {
const row = nextRow(api.getRows());
api.addRows([row]);
say({
ko: `'${row.name}' 행을 맨 아래에 넣었어요`,
en: `Added row '${row.name}' at the bottom`,
});
},
selectDev: (api) => {
const keys = api
.getRows()
.filter((row) => row.dept === '개발팀')
.map((row) => row.id);
api.setSelectedKeys(keys);
say({
ko: `개발팀 ${keys.length}행을 골랐어요`,
en: `Selected ${keys.length} rows of the development team`,
});
},
removeSelected: (api) => {
const removed = api.removeRows(api.getSelectedKeys());
say({
ko: removed ? `${removed}행을 지웠어요` : '먼저 지울 행을 골라 주세요',
en: removed ? `Deleted ${removed} rows` : 'Select the rows to delete first',
});
},
};
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('focusLast').addEventListener('click', () => actions.focusLast(api));
document.getElementById('addRow').addEventListener('click', () => actions.addRow(api));
document.getElementById('selectDev').addEventListener('click', () => actions.selectDev(api));
document.getElementById('removeSelected').addEventListener('click', () => actions.removeSelected(api));
</script>
</body>
</html>버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Grid, useGridApi } from '@maxgrid/react';
import { nextRow, options, type Row } from './example';
// 표를 직접 부르는 명령(api)은 ref로 받아요. 표가 만들어지기 전에는 null이라 버튼도 잠가 둬요.
// StrictMode처럼 표가 다시 만들어지면 새 api가 들어오므로, api를 직접 저장해 두지 말고 이 값을 써요.
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
const focusLast = () => {
const rows = api?.getVisibleRows() ?? [];
const last = rows[rows.length - 1];
if (!api || !last) return;
api.scrollToIndex(rows.length - 1);
api.focusCell(last.id, 'name');
};
return (
<>
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 8, marginBottom: 8 }}>
<button type="button" disabled={!api} onClick={focusLast}>
맨 아래로 이동
</button>
<button
type="button"
disabled={!api}
onClick={() => api?.addRows([nextRow(api.getRows())])}
>
행 추가
</button>
<button
type="button"
disabled={!api}
onClick={() =>
api?.setSelectedKeys(
api
.getRows()
.filter((r) => r.dept === '개발팀')
.map((r) => r.id),
)
}
>
개발팀 고르기
</button>
<button
type="button"
disabled={!api}
onClick={() => api?.removeRows(api.getSelectedKeys())}
>
고른 행 지우기
</button>
</div>
<div style={{ height: 280 }}>
<Grid<Row> ref={gridRef} options={options} />
</div>
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
dept: string;
}
const DEPTS = ['개발팀', '디자인팀', '영업팀'];
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 80, align: 'end' },
{ id: 'name', title: '이름', width: 140 },
{ id: 'dept', title: '부서', width: 120 },
],
rows: Array.from({ length: 30 }, (_, i) => ({
id: i + 1,
name: `직원 ${i + 1}`,
dept: DEPTS[i % 3] as string,
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
};
/** 새 행을 만들 때 쓰는 번호예요. 지워도 번호가 겹치지 않게 가장 큰 번호에 1을 더해요. */
export function nextRow(rows: readonly Row[]): Row {
const id = rows.reduce((max, row) => Math.max(max, row.id), 0) + 1;
return { id, name: `직원 ${id}`, dept: DEPTS[id % 3] as string };
}
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
}
export const actions = {
// 맨 아래 행으로 스크롤하고, 그 행의 이름 칸에 포커스를 옮겨요.
focusLast: (api: GridApi<Row>) => {
const rows = api.getVisibleRows();
const last = rows[rows.length - 1];
if (!last) return say({ ko: '보이는 행이 없어요', en: 'No visible rows' });
api.scrollToIndex(rows.length - 1);
api.focusCell(last.id, 'name');
say({ ko: `'${last.name}' 행으로 이동했어요`, en: `Moved to row '${last.name}'` });
},
addRow: (api: GridApi<Row>) => {
const row = nextRow(api.getRows());
api.addRows([row]);
say({
ko: `'${row.name}' 행을 맨 아래에 넣었어요`,
en: `Added row '${row.name}' at the bottom`,
});
},
selectDev: (api: GridApi<Row>) => {
const keys = api
.getRows()
.filter((row) => row.dept === '개발팀')
.map((row) => row.id);
api.setSelectedKeys(keys);
say({
ko: `개발팀 ${keys.length}행을 골랐어요`,
en: `Selected ${keys.length} rows of the development team`,
});
},
removeSelected: (api: GridApi<Row>) => {
const removed = api.removeRows(api.getSelectedKeys());
say({
ko: removed ? `${removed}행을 지웠어요` : '먼저 지울 행을 골라 주세요',
en: removed ? `Deleted ${removed} rows` : 'Select the rows to delete first',
});
},
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<script setup lang="ts">
import { Grid, type GridInstance } from '@maxgrid/vue';
import { ref } from 'vue';
import { nextRow, options, type Row } from './example';
// 표를 직접 부르는 명령(api)은 템플릿 ref의 getApi()로 받아요. 표가 만들어지기 전에는 null이에요.
const grid = ref<GridInstance<Row> | null>(null);
function focusLast() {
const api = grid.value?.getApi();
const rows = api?.getVisibleRows() ?? [];
const last = rows[rows.length - 1];
if (!api || !last) return;
api.scrollToIndex(rows.length - 1);
api.focusCell(last.id, 'name');
}
function addRow() {
const api = grid.value?.getApi();
api?.addRows([nextRow(api.getRows())]);
}
function selectDev() {
const api = grid.value?.getApi();
api?.setSelectedKeys(
api
.getRows()
.filter((r) => r.dept === '개발팀')
.map((r) => r.id),
);
}
function removeSelected() {
const api = grid.value?.getApi();
api?.removeRows(api.getSelectedKeys());
}
</script>
<template>
<div class="bar">
<button type="button" @click="focusLast">맨 아래로 이동</button>
<button type="button" @click="addRow">행 추가</button>
<button type="button" @click="selectDev">개발팀 고르기</button>
<button type="button" @click="removeSelected">고른 행 지우기</button>
</div>
<Grid ref="grid" style="height: 280px" :options="options" />
</template>
<style scoped>
.bar {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-bottom: 8px;
}
</style>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
dept: string;
}
const DEPTS = ['개발팀', '디자인팀', '영업팀'];
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 80, align: 'end' },
{ id: 'name', title: '이름', width: 140 },
{ id: 'dept', title: '부서', width: 120 },
],
rows: Array.from({ length: 30 }, (_, i) => ({
id: i + 1,
name: `직원 ${i + 1}`,
dept: DEPTS[i % 3] as string,
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
};
/** 새 행을 만들 때 쓰는 번호예요. 지워도 번호가 겹치지 않게 가장 큰 번호에 1을 더해요. */
export function nextRow(rows: readonly Row[]): Row {
const id = rows.reduce((max, row) => Math.max(max, row.id), 0) + 1;
return { id, name: `직원 ${id}`, dept: DEPTS[id % 3] as string };
}
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
}
export const actions = {
// 맨 아래 행으로 스크롤하고, 그 행의 이름 칸에 포커스를 옮겨요.
focusLast: (api: GridApi<Row>) => {
const rows = api.getVisibleRows();
const last = rows[rows.length - 1];
if (!last) return say({ ko: '보이는 행이 없어요', en: 'No visible rows' });
api.scrollToIndex(rows.length - 1);
api.focusCell(last.id, 'name');
say({ ko: `'${last.name}' 행으로 이동했어요`, en: `Moved to row '${last.name}'` });
},
addRow: (api: GridApi<Row>) => {
const row = nextRow(api.getRows());
api.addRows([row]);
say({
ko: `'${row.name}' 행을 맨 아래에 넣었어요`,
en: `Added row '${row.name}' at the bottom`,
});
},
selectDev: (api: GridApi<Row>) => {
const keys = api
.getRows()
.filter((row) => row.dept === '개발팀')
.map((row) => row.id);
api.setSelectedKeys(keys);
say({
ko: `개발팀 ${keys.length}행을 골랐어요`,
en: `Selected ${keys.length} rows of the development team`,
});
},
removeSelected: (api: GridApi<Row>) => {
const removed = api.removeRows(api.getSelectedKeys());
say({
ko: removed ? `${removed}행을 지웠어요` : '먼저 지울 행을 골라 주세요',
en: removed ? `Deleted ${removed} rows` : 'Select the rows to delete first',
});
},
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Component, ViewChild } from '@angular/core';
import { GridComponent } from '@maxgrid/angular';
import { nextRow, options, type Row } from './example';
// 표를 직접 부르는 명령(api)은 @ViewChild로 잡은 GridComponent의 getApi()로 받아요.
// 표가 만들어지기 전(또는 서버에서 그릴 때)에는 null이에요.
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div style="display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px">
<button type="button" (click)="focusLast()">맨 아래로 이동</button>
<button type="button" (click)="addRow()">행 추가</button>
<button type="button" (click)="selectDev()">개발팀 고르기</button>
<button type="button" (click)="removeSelected()">고른 행 지우기</button>
</div>
<mx-grid style="height: 280px" [options]="options" />
`,
})
export class ExampleGridComponent {
readonly options = options;
@ViewChild(GridComponent) private readonly gridRef?: GridComponent<Row>;
focusLast(): void {
const api = this.gridRef?.getApi();
const rows = api?.getVisibleRows() ?? [];
const last = rows[rows.length - 1];
if (!api || !last) return;
api.scrollToIndex(rows.length - 1);
api.focusCell(last.id, 'name');
}
addRow(): void {
const api = this.gridRef?.getApi();
api?.addRows([nextRow(api.getRows())]);
}
selectDev(): void {
const api = this.gridRef?.getApi();
api?.setSelectedKeys(
api
.getRows()
.filter((r) => r.dept === '개발팀')
.map((r) => r.id),
);
}
removeSelected(): void {
const api = this.gridRef?.getApi();
api?.removeRows(api.getSelectedKeys());
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
dept: string;
}
const DEPTS = ['개발팀', '디자인팀', '영업팀'];
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 80, align: 'end' },
{ id: 'name', title: '이름', width: 140 },
{ id: 'dept', title: '부서', width: 120 },
],
rows: Array.from({ length: 30 }, (_, i) => ({
id: i + 1,
name: `직원 ${i + 1}`,
dept: DEPTS[i % 3] as string,
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
};
/** 새 행을 만들 때 쓰는 번호예요. 지워도 번호가 겹치지 않게 가장 큰 번호에 1을 더해요. */
export function nextRow(rows: readonly Row[]): Row {
const id = rows.reduce((max, row) => Math.max(max, row.id), 0) + 1;
return { id, name: `직원 ${id}`, dept: DEPTS[id % 3] as string };
}
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
}
export const actions = {
// 맨 아래 행으로 스크롤하고, 그 행의 이름 칸에 포커스를 옮겨요.
focusLast: (api: GridApi<Row>) => {
const rows = api.getVisibleRows();
const last = rows[rows.length - 1];
if (!last) return say({ ko: '보이는 행이 없어요', en: 'No visible rows' });
api.scrollToIndex(rows.length - 1);
api.focusCell(last.id, 'name');
say({ ko: `'${last.name}' 행으로 이동했어요`, en: `Moved to row '${last.name}'` });
},
addRow: (api: GridApi<Row>) => {
const row = nextRow(api.getRows());
api.addRows([row]);
say({
ko: `'${row.name}' 행을 맨 아래에 넣었어요`,
en: `Added row '${row.name}' at the bottom`,
});
},
selectDev: (api: GridApi<Row>) => {
const keys = api
.getRows()
.filter((row) => row.dept === '개발팀')
.map((row) => row.id);
api.setSelectedKeys(keys);
say({
ko: `개발팀 ${keys.length}행을 골랐어요`,
en: `Selected ${keys.length} rows of the development team`,
});
},
removeSelected: (api: GridApi<Row>) => {
const removed = api.removeRows(api.getSelectedKeys());
say({
ko: removed ? `${removed}행을 지웠어요` : '먼저 지울 행을 골라 주세요',
en: removed ? `Deleted ${removed} rows` : 'Select the rows to delete first',
});
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<div id="app">
<div class="toolbar">
<button type="button" id="focusLast">Move to the bottom</button>
<button type="button" id="addRow">Add a row</button>
<button type="button" id="selectDev">Select development</button>
<button type="button" id="removeSelected">Remove selected</button>
</div>
<div id="grid" style="height: 440px"></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('focusLast').addEventListener('click', () => actions.focusLast(api));
document.getElementById('addRow').addEventListener('click', () => actions.addRow(api));
document.getElementById('selectDev').addEventListener('click', () => actions.selectDev(api));
document.getElementById('removeSelected').addEventListener('click', () => actions.removeSelected(api));const DEPTS = ['Dev', 'Design', 'Sales'];
export const options = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 80, align: 'end' },
{ id: 'name', title: 'Name', width: 140 },
{ id: 'dept', title: 'Department', width: 120 },
],
rows: Array.from({ length: 30 }, (_, i) => ({
id: i + 1,
name: `Employee ${i + 1}`,
dept: DEPTS[i % 3],
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
};
/** The number used when making a new row. Add 1 to the largest number so numbers do not collide even after deleting. */
export function nextRow(rows) {
const id = rows.reduce((max, row) => Math.max(max, row.id), 0) + 1;
return { id, name: `Employee ${id}`, dept: DEPTS[id % 3] };
}
let say = () => { };
export function setup(_api, log) {
say = log;
}
export const actions = {
// Scroll to the bottom row and move focus to that row's name cell.
focusLast: (api) => {
const rows = api.getVisibleRows();
const last = rows[rows.length - 1];
if (!last)
return say('No visible rows');
api.scrollToIndex(rows.length - 1);
api.focusCell(last.id, 'name');
say(`Moved to row '${last.name}'`);
},
addRow: (api) => {
const row = nextRow(api.getRows());
api.addRows([row]);
say(`Added row '${row.name}' at the bottom`);
},
selectDev: (api) => {
const keys = api
.getRows()
.filter((row) => row.dept === 'Dev')
.map((row) => row.id);
api.setSelectedKeys(keys);
say(`Selected ${keys.length} rows of the development team`);
},
removeSelected: (api) => {
const removed = api.removeRows(api.getSelectedKeys());
say(removed ? `Deleted ${removed} rows` : 'Select the rows to delete first');
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Command the grid from buttons (move, add, select, remove)</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="focusLast">Move to the bottom</button>
<button type="button" id="addRow">Add a row</button>
<button type="button" id="selectDev">Select development</button>
<button type="button" id="removeSelected">Remove selected</button>
</div>
<div id="grid" style="height: 440px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const DEPTS = ['Dev', 'Design', 'Sales'];
const options = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 80, align: 'end' },
{ id: 'name', title: 'Name', width: 140 },
{ id: 'dept', title: 'Department', width: 120 },
],
rows: Array.from({ length: 30 }, (_, i) => ({
id: i + 1,
name: `Employee ${i + 1}`,
dept: DEPTS[i % 3],
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
};
/** The number used when making a new row. Add 1 to the largest number so numbers do not collide even after deleting. */
function nextRow(rows) {
const id = rows.reduce((max, row) => Math.max(max, row.id), 0) + 1;
return { id, name: `Employee ${id}`, dept: DEPTS[id % 3] };
}
let say = () => { };
function setup(_api, log) {
say = log;
}
const actions = {
// Scroll to the bottom row and move focus to that row's name cell.
focusLast: (api) => {
const rows = api.getVisibleRows();
const last = rows[rows.length - 1];
if (!last)
return say('No visible rows');
api.scrollToIndex(rows.length - 1);
api.focusCell(last.id, 'name');
say(`Moved to row '${last.name}'`);
},
addRow: (api) => {
const row = nextRow(api.getRows());
api.addRows([row]);
say(`Added row '${row.name}' at the bottom`);
},
selectDev: (api) => {
const keys = api
.getRows()
.filter((row) => row.dept === 'Dev')
.map((row) => row.id);
api.setSelectedKeys(keys);
say(`Selected ${keys.length} rows of the development team`);
},
removeSelected: (api) => {
const removed = api.removeRows(api.getSelectedKeys());
say(removed ? `Deleted ${removed} rows` : 'Select the rows to delete first');
},
};
options.ariaLabel = "Command the grid from buttons (move, add, select, remove)";
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('focusLast').addEventListener('click', () => actions.focusLast(api));
document.getElementById('addRow').addEventListener('click', () => actions.addRow(api));
document.getElementById('selectDev').addEventListener('click', () => actions.selectDev(api));
document.getElementById('removeSelected').addEventListener('click', () => actions.removeSelected(api));
</script>
</body>
</html>When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Grid, useGridApi } from '@maxgrid/react';
import { nextRow, options, type Row } from './example';
// 표를 직접 부르는 명령(api)은 ref로 받아요. 표가 만들어지기 전에는 null이라 버튼도 잠가 둬요.
// StrictMode처럼 표가 다시 만들어지면 새 api가 들어오므로, api를 직접 저장해 두지 말고 이 값을 써요.
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
const focusLast = () => {
const rows = api?.getVisibleRows() ?? [];
const last = rows[rows.length - 1];
if (!api || !last) return;
api.scrollToIndex(rows.length - 1);
api.focusCell(last.id, 'name');
};
return (
<>
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 8, marginBottom: 8 }}>
<button type="button" disabled={!api} onClick={focusLast}>
맨 아래로 이동
</button>
<button
type="button"
disabled={!api}
onClick={() => api?.addRows([nextRow(api.getRows())])}
>
행 추가
</button>
<button
type="button"
disabled={!api}
onClick={() =>
api?.setSelectedKeys(
api
.getRows()
.filter((r) => r.dept === '개발팀')
.map((r) => r.id),
)
}
>
개발팀 고르기
</button>
<button
type="button"
disabled={!api}
onClick={() => api?.removeRows(api.getSelectedKeys())}
>
고른 행 지우기
</button>
</div>
<div style={{ height: 280 }}>
<Grid<Row> ref={gridRef} options={options} />
</div>
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
name: string;
dept: string;
}
const DEPTS = ['Dev', 'Design', 'Sales'];
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 80, align: 'end' },
{ id: 'name', title: 'Name', width: 140 },
{ id: 'dept', title: 'Department', width: 120 },
],
rows: Array.from({ length: 30 }, (_, i) => ({
id: i + 1,
name: `Employee ${i + 1}`,
dept: DEPTS[i % 3] as string,
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
};
/** The number used when making a new row. Add 1 to the largest number so numbers do not collide even after deleting. */
export function nextRow(rows: readonly Row[]): Row {
const id = rows.reduce((max, row) => Math.max(max, row.id), 0) + 1;
return { id, name: `Employee ${id}`, dept: DEPTS[id % 3] as string };
}
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
}
export const actions = {
// Scroll to the bottom row and move focus to that row's name cell.
focusLast: (api: GridApi<Row>) => {
const rows = api.getVisibleRows();
const last = rows[rows.length - 1];
if (!last) return say('No visible rows');
api.scrollToIndex(rows.length - 1);
api.focusCell(last.id, 'name');
say(`Moved to row '${last.name}'`);
},
addRow: (api: GridApi<Row>) => {
const row = nextRow(api.getRows());
api.addRows([row]);
say(`Added row '${row.name}' at the bottom`);
},
selectDev: (api: GridApi<Row>) => {
const keys = api
.getRows()
.filter((row) => row.dept === 'Dev')
.map((row) => row.id);
api.setSelectedKeys(keys);
say(`Selected ${keys.length} rows of the development team`);
},
removeSelected: (api: GridApi<Row>) => {
const removed = api.removeRows(api.getSelectedKeys());
say(removed ? `Deleted ${removed} rows` : 'Select the rows to delete first');
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<script setup lang="ts">
import { Grid, type GridInstance } from '@maxgrid/vue';
import { ref } from 'vue';
import { nextRow, options, type Row } from './example';
// 표를 직접 부르는 명령(api)은 템플릿 ref의 getApi()로 받아요. 표가 만들어지기 전에는 null이에요.
const grid = ref<GridInstance<Row> | null>(null);
function focusLast() {
const api = grid.value?.getApi();
const rows = api?.getVisibleRows() ?? [];
const last = rows[rows.length - 1];
if (!api || !last) return;
api.scrollToIndex(rows.length - 1);
api.focusCell(last.id, 'name');
}
function addRow() {
const api = grid.value?.getApi();
api?.addRows([nextRow(api.getRows())]);
}
function selectDev() {
const api = grid.value?.getApi();
api?.setSelectedKeys(
api
.getRows()
.filter((r) => r.dept === '개발팀')
.map((r) => r.id),
);
}
function removeSelected() {
const api = grid.value?.getApi();
api?.removeRows(api.getSelectedKeys());
}
</script>
<template>
<div class="bar">
<button type="button" @click="focusLast">맨 아래로 이동</button>
<button type="button" @click="addRow">행 추가</button>
<button type="button" @click="selectDev">개발팀 고르기</button>
<button type="button" @click="removeSelected">고른 행 지우기</button>
</div>
<Grid ref="grid" style="height: 280px" :options="options" />
</template>
<style scoped>
.bar {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-bottom: 8px;
}
</style>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string;
export interface Row {
id: number;
name: string;
dept: string;
}
const DEPTS = ['Dev', 'Design', 'Sales'];
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 80, align: 'end' },
{ id: 'name', title: 'Name', width: 140 },
{ id: 'dept', title: 'Department', width: 120 },
],
rows: Array.from({ length: 30 }, (_, i) => ({
id: i + 1,
name: `Employee ${i + 1}`,
dept: DEPTS[i % 3] as string,
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
};
/** The number used when making a new row. Add 1 to the largest number so numbers do not collide even after deleting. */
export function nextRow(rows: readonly Row[]): Row {
const id = rows.reduce((max, row) => Math.max(max, row.id), 0) + 1;
return { id, name: `Employee ${id}`, dept: DEPTS[id % 3] as string };
}
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
}
export const actions = {
// Scroll to the bottom row and move focus to that row's name cell.
focusLast: (api: GridApi<Row>) => {
const rows = api.getVisibleRows();
const last = rows[rows.length - 1];
if (!last) return say('No visible rows');
api.scrollToIndex(rows.length - 1);
api.focusCell(last.id, 'name');
say(`Moved to row '${last.name}'`);
},
addRow: (api: GridApi<Row>) => {
const row = nextRow(api.getRows());
api.addRows([row]);
say(`Added row '${row.name}' at the bottom`);
},
selectDev: (api: GridApi<Row>) => {
const keys = api
.getRows()
.filter((row) => row.dept === 'Dev')
.map((row) => row.id);
api.setSelectedKeys(keys);
say(`Selected ${keys.length} rows of the development team`);
},
removeSelected: (api: GridApi<Row>) => {
const removed = api.removeRows(api.getSelectedKeys());
say(removed ? `Deleted ${removed} rows` : 'Select the rows to delete first');
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Component, ViewChild } from '@angular/core';
import { GridComponent } from '@maxgrid/angular';
import { nextRow, options, type Row } from './example';
// 표를 직접 부르는 명령(api)은 @ViewChild로 잡은 GridComponent의 getApi()로 받아요.
// 표가 만들어지기 전(또는 서버에서 그릴 때)에는 null이에요.
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div style="display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px">
<button type="button" (click)="focusLast()">맨 아래로 이동</button>
<button type="button" (click)="addRow()">행 추가</button>
<button type="button" (click)="selectDev()">개발팀 고르기</button>
<button type="button" (click)="removeSelected()">고른 행 지우기</button>
</div>
<mx-grid style="height: 280px" [options]="options" />
`,
})
export class ExampleGridComponent {
readonly options = options;
@ViewChild(GridComponent) private readonly gridRef?: GridComponent<Row>;
focusLast(): void {
const api = this.gridRef?.getApi();
const rows = api?.getVisibleRows() ?? [];
const last = rows[rows.length - 1];
if (!api || !last) return;
api.scrollToIndex(rows.length - 1);
api.focusCell(last.id, 'name');
}
addRow(): void {
const api = this.gridRef?.getApi();
api?.addRows([nextRow(api.getRows())]);
}
selectDev(): void {
const api = this.gridRef?.getApi();
api?.setSelectedKeys(
api
.getRows()
.filter((r) => r.dept === '개발팀')
.map((r) => r.id),
);
}
removeSelected(): void {
const api = this.gridRef?.getApi();
api?.removeRows(api.getSelectedKeys());
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string;
export interface Row {
id: number;
name: string;
dept: string;
}
const DEPTS = ['Dev', 'Design', 'Sales'];
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 80, align: 'end' },
{ id: 'name', title: 'Name', width: 140 },
{ id: 'dept', title: 'Department', width: 120 },
],
rows: Array.from({ length: 30 }, (_, i) => ({
id: i + 1,
name: `Employee ${i + 1}`,
dept: DEPTS[i % 3] as string,
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
};
/** The number used when making a new row. Add 1 to the largest number so numbers do not collide even after deleting. */
export function nextRow(rows: readonly Row[]): Row {
const id = rows.reduce((max, row) => Math.max(max, row.id), 0) + 1;
return { id, name: `Employee ${id}`, dept: DEPTS[id % 3] as string };
}
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
}
export const actions = {
// Scroll to the bottom row and move focus to that row's name cell.
focusLast: (api: GridApi<Row>) => {
const rows = api.getVisibleRows();
const last = rows[rows.length - 1];
if (!last) return say('No visible rows');
api.scrollToIndex(rows.length - 1);
api.focusCell(last.id, 'name');
say(`Moved to row '${last.name}'`);
},
addRow: (api: GridApi<Row>) => {
const row = nextRow(api.getRows());
api.addRows([row]);
say(`Added row '${row.name}' at the bottom`);
},
selectDev: (api: GridApi<Row>) => {
const keys = api
.getRows()
.filter((row) => row.dept === 'Dev')
.map((row) => row.id);
api.setSelectedKeys(keys);
say(`Selected ${keys.length} rows of the development team`);
},
removeSelected: (api: GridApi<Row>) => {
const removed = api.removeRows(api.getSelectedKeys());
say(removed ? `Deleted ${removed} rows` : 'Select the rows to delete first');
},
};