체크박스에 다른 저장 값 쓰기Store other values with a checkbox
checkedValue와 uncheckedValue로 'Y'/'N' 같은 값을 체크 상태에 짝지어요. 크기와 모양, 라벨도 정해요. 필터와 내보내기는 저장된 값('Y'/'N')을 그대로 봐요.checkedValue and uncheckedValue map values such as 'Y'/'N' to the checked state. Size, shape and label are options too. Filter and export see the stored value ('Y'/'N').
코드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)
이 예제는 추가 패키지가 필요해요. 내 컴퓨터에서 돌리려면 npm i maxgrid @maxgrid/export로 설치하세요.
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<div id="app">
<div class="toolbar">
<button type="button" id="csv">CSV 보기</button>
</div>
<div id="grid" style="height: 230px"></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('csv').addEventListener('click', () => actions.csv(api));import { withExport } from '@maxgrid/export';
const rows = [
{ id: 1, name: '김서연', agreed: 'Y', vip: 1 },
{ id: 2, name: '이준호', agreed: 'N', vip: 0 },
{ id: 3, name: '박민지', agreed: 'Y', vip: 0 },
];
// checkbox에 매개변수를 주면 누르는 컨트롤이 돼요. 값은 'Y'/'N', 1/0처럼 저장된 그대로 쓰이고
// 필터·정렬·내보내기도 그 값을 봐요. size와 shape(checkbox 전용)로 모양을 바꿔요.
export const options = {
columns: [
{ id: 'name', title: '이름', width: 120 },
{
id: 'agreed',
title: '동의',
width: 110,
editable: true,
cellRenderer: 'checkbox',
cellRendererParams: { checkedValue: 'Y', uncheckedValue: 'N', label: '동의', size: 'md' },
},
{
id: 'vip',
title: 'VIP',
kind: 'number',
width: 90,
editable: true,
cellRenderer: 'checkbox',
cellRendererParams: { checkedValue: 1, uncheckedValue: 0, size: 'lg', shape: 'round' },
},
],
rows,
rowKey: 'id',
};
let say = () => undefined;
export function setup(api, log) {
say = log;
withExport(api);
return api.on('editCommitted', (e) => log({
ko: `${e.columnId}에 ${JSON.stringify(e.newValue)}을(를) 저장했어요.`,
en: `Stored ${JSON.stringify(e.newValue)} in ${e.columnId}.`,
}));
}
export const actions = {
csv: (api) => {
const csv = withExport(api).exportCsv({ bom: false, newline: '\n' });
say(csv.split('\n').join(' | '));
},
}; 이 HTML은 추가 번들(위 script 태그)도 함께 불러오고 전역 변수로 써요. 별도 설치는 필요 없어요.
<!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="csv">CSV 보기</button>
</div>
<div id="grid" style="height: 230px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.export.min.js"></script>
<script>
const { withExport } = MaxGridExport;
const rows = [
{ id: 1, name: '김서연', agreed: 'Y', vip: 1 },
{ id: 2, name: '이준호', agreed: 'N', vip: 0 },
{ id: 3, name: '박민지', agreed: 'Y', vip: 0 },
];
// checkbox에 매개변수를 주면 누르는 컨트롤이 돼요. 값은 'Y'/'N', 1/0처럼 저장된 그대로 쓰이고
// 필터·정렬·내보내기도 그 값을 봐요. size와 shape(checkbox 전용)로 모양을 바꿔요.
const options = {
columns: [
{ id: 'name', title: '이름', width: 120 },
{
id: 'agreed',
title: '동의',
width: 110,
editable: true,
cellRenderer: 'checkbox',
cellRendererParams: { checkedValue: 'Y', uncheckedValue: 'N', label: '동의', size: 'md' },
},
{
id: 'vip',
title: 'VIP',
kind: 'number',
width: 90,
editable: true,
cellRenderer: 'checkbox',
cellRendererParams: { checkedValue: 1, uncheckedValue: 0, size: 'lg', shape: 'round' },
},
],
rows,
rowKey: 'id',
};
let say = () => undefined;
function setup(api, log) {
say = log;
withExport(api);
return api.on('editCommitted', (e) => log({
ko: `${e.columnId}에 ${JSON.stringify(e.newValue)}을(를) 저장했어요.`,
en: `Stored ${JSON.stringify(e.newValue)} in ${e.columnId}.`,
}));
}
const actions = {
csv: (api) => {
const csv = withExport(api).exportCsv({ bom: false, newline: '\n' });
say(csv.split('\n').join(' | '));
},
};
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('csv').addEventListener('click', () => actions.csv(api));
</script>
</body>
</html> 이 예제는 추가 패키지가 필요해요. 내 컴퓨터에서 돌리려면 npm i @maxgrid/react @maxgrid/export로 설치하세요.
버튼을 누르면 이 예제의 코드가 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">
<button
type="button"
disabled={!api}
onClick={() => api && actions.csv(api)}
>CSV 보기</button>
</div>
<Grid style={{ height: 230 }} ref={gridRef} options={options} />
</>
);
}import { withExport } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
agreed: 'Y' | 'N';
vip: 0 | 1;
}
const rows: Row[] = [
{ id: 1, name: '김서연', agreed: 'Y', vip: 1 },
{ id: 2, name: '이준호', agreed: 'N', vip: 0 },
{ id: 3, name: '박민지', agreed: 'Y', vip: 0 },
];
// checkbox에 매개변수를 주면 누르는 컨트롤이 돼요. 값은 'Y'/'N', 1/0처럼 저장된 그대로 쓰이고
// 필터·정렬·내보내기도 그 값을 봐요. size와 shape(checkbox 전용)로 모양을 바꿔요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: '이름', width: 120 },
{
id: 'agreed',
title: '동의',
width: 110,
editable: true,
cellRenderer: 'checkbox',
cellRendererParams: { checkedValue: 'Y', uncheckedValue: 'N', label: '동의', size: 'md' },
},
{
id: 'vip',
title: 'VIP',
kind: 'number',
width: 90,
editable: true,
cellRenderer: 'checkbox',
cellRendererParams: { checkedValue: 1, uncheckedValue: 0, size: 'lg', shape: 'round' },
},
],
rows,
rowKey: 'id',
};
let say: (message: LogText) => void = () => undefined;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
withExport(api);
return api.on('editCommitted', (e) =>
log({
ko: `${e.columnId}에 ${JSON.stringify(e.newValue)}을(를) 저장했어요.`,
en: `Stored ${JSON.stringify(e.newValue)} in ${e.columnId}.`,
}),
);
}
export const actions = {
csv: (api: GridApi<Row>) => {
const csv = withExport(api).exportCsv({ bom: false, newline: '\n' });
say(csv.split('\n').join(' | '));
},
}; 이 예제는 추가 패키지가 필요해요. 내 컴퓨터에서 돌리려면 npm i @maxgrid/vue @maxgrid/export로 설치하세요.
버튼을 누르면 이 예제의 코드가 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">
<button type="button" :disabled="!api" @click="api && actions.csv(api)">CSV 보기</button>
</div>
<Grid style="height: 230px" :options="options" @ready="onReady" />
</template>import { withExport } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
agreed: 'Y' | 'N';
vip: 0 | 1;
}
const rows: Row[] = [
{ id: 1, name: '김서연', agreed: 'Y', vip: 1 },
{ id: 2, name: '이준호', agreed: 'N', vip: 0 },
{ id: 3, name: '박민지', agreed: 'Y', vip: 0 },
];
// checkbox에 매개변수를 주면 누르는 컨트롤이 돼요. 값은 'Y'/'N', 1/0처럼 저장된 그대로 쓰이고
// 필터·정렬·내보내기도 그 값을 봐요. size와 shape(checkbox 전용)로 모양을 바꿔요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: '이름', width: 120 },
{
id: 'agreed',
title: '동의',
width: 110,
editable: true,
cellRenderer: 'checkbox',
cellRendererParams: { checkedValue: 'Y', uncheckedValue: 'N', label: '동의', size: 'md' },
},
{
id: 'vip',
title: 'VIP',
kind: 'number',
width: 90,
editable: true,
cellRenderer: 'checkbox',
cellRendererParams: { checkedValue: 1, uncheckedValue: 0, size: 'lg', shape: 'round' },
},
],
rows,
rowKey: 'id',
};
let say: (message: LogText) => void = () => undefined;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
withExport(api);
return api.on('editCommitted', (e) =>
log({
ko: `${e.columnId}에 ${JSON.stringify(e.newValue)}을(를) 저장했어요.`,
en: `Stored ${JSON.stringify(e.newValue)} in ${e.columnId}.`,
}),
);
}
export const actions = {
csv: (api: GridApi<Row>) => {
const csv = withExport(api).exportCsv({ bom: false, newline: '\n' });
say(csv.split('\n').join(' | '));
},
}; 이 예제는 추가 패키지가 필요해요. 내 컴퓨터에서 돌리려면 npm i @maxgrid/angular @maxgrid/export로 설치하세요.
버튼을 누르면 이 예제의 코드가 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">
<button
type="button"
[disabled]="!api"
(click)="api && actions.csv(api)"
>CSV 보기</button>
</div>
<mx-grid style="height: 230px" [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 { withExport } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
agreed: 'Y' | 'N';
vip: 0 | 1;
}
const rows: Row[] = [
{ id: 1, name: '김서연', agreed: 'Y', vip: 1 },
{ id: 2, name: '이준호', agreed: 'N', vip: 0 },
{ id: 3, name: '박민지', agreed: 'Y', vip: 0 },
];
// checkbox에 매개변수를 주면 누르는 컨트롤이 돼요. 값은 'Y'/'N', 1/0처럼 저장된 그대로 쓰이고
// 필터·정렬·내보내기도 그 값을 봐요. size와 shape(checkbox 전용)로 모양을 바꿔요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: '이름', width: 120 },
{
id: 'agreed',
title: '동의',
width: 110,
editable: true,
cellRenderer: 'checkbox',
cellRendererParams: { checkedValue: 'Y', uncheckedValue: 'N', label: '동의', size: 'md' },
},
{
id: 'vip',
title: 'VIP',
kind: 'number',
width: 90,
editable: true,
cellRenderer: 'checkbox',
cellRendererParams: { checkedValue: 1, uncheckedValue: 0, size: 'lg', shape: 'round' },
},
],
rows,
rowKey: 'id',
};
let say: (message: LogText) => void = () => undefined;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
withExport(api);
return api.on('editCommitted', (e) =>
log({
ko: `${e.columnId}에 ${JSON.stringify(e.newValue)}을(를) 저장했어요.`,
en: `Stored ${JSON.stringify(e.newValue)} in ${e.columnId}.`,
}),
);
}
export const actions = {
csv: (api: GridApi<Row>) => {
const csv = withExport(api).exportCsv({ bom: false, newline: '\n' });
say(csv.split('\n').join(' | '));
},
}; This example needs an extra package. To run it locally, install with npm i maxgrid @maxgrid/export.
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="csv">Show CSV</button>
</div>
<div id="grid" style="height: 230px"></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('csv').addEventListener('click', () => actions.csv(api));import { withExport } from '@maxgrid/export';
const rows = [
{ id: 1, name: 'Liam Carter', agreed: 'Y', vip: 1 },
{ id: 2, name: 'Emma Stone', agreed: 'N', vip: 0 },
{ id: 3, name: 'Noah Kim', agreed: 'Y', vip: 0 },
];
// Giving checkbox parameters turns it into a clickable control. The value is used as stored
// ('Y'/'N', 1/0), and filter, sort and export see that value too. size and shape (checkbox only) change the look.
export const options = {
columns: [
{ id: 'name', title: 'Name', width: 140 },
{
id: 'agreed',
title: 'Agreed',
width: 120,
editable: true,
cellRenderer: 'checkbox',
cellRendererParams: { checkedValue: 'Y', uncheckedValue: 'N', label: 'Agreed', size: 'md' },
},
{
id: 'vip',
title: 'VIP',
kind: 'number',
width: 90,
editable: true,
cellRenderer: 'checkbox',
cellRendererParams: { checkedValue: 1, uncheckedValue: 0, size: 'lg', shape: 'round' },
},
],
rows,
rowKey: 'id',
};
let say = () => undefined;
export function setup(api, log) {
say = log;
withExport(api);
return api.on('editCommitted', (e) => log(`Stored ${JSON.stringify(e.newValue)} in ${e.columnId}.`));
}
export const actions = {
csv: (api) => {
const csv = withExport(api).exportCsv({ bom: false, newline: '\n' });
say(csv.split('\n').join(' | '));
},
};This HTML also loads an extra bundle (the extra script tag above) and uses it as a global. No install is needed.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Store other values with a checkbox</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="csv">Show CSV</button>
</div>
<div id="grid" style="height: 230px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.export.min.js"></script>
<script>
const { withExport } = MaxGridExport;
const rows = [
{ id: 1, name: 'Liam Carter', agreed: 'Y', vip: 1 },
{ id: 2, name: 'Emma Stone', agreed: 'N', vip: 0 },
{ id: 3, name: 'Noah Kim', agreed: 'Y', vip: 0 },
];
// Giving checkbox parameters turns it into a clickable control. The value is used as stored
// ('Y'/'N', 1/0), and filter, sort and export see that value too. size and shape (checkbox only) change the look.
const options = {
columns: [
{ id: 'name', title: 'Name', width: 140 },
{
id: 'agreed',
title: 'Agreed',
width: 120,
editable: true,
cellRenderer: 'checkbox',
cellRendererParams: { checkedValue: 'Y', uncheckedValue: 'N', label: 'Agreed', size: 'md' },
},
{
id: 'vip',
title: 'VIP',
kind: 'number',
width: 90,
editable: true,
cellRenderer: 'checkbox',
cellRendererParams: { checkedValue: 1, uncheckedValue: 0, size: 'lg', shape: 'round' },
},
],
rows,
rowKey: 'id',
};
let say = () => undefined;
function setup(api, log) {
say = log;
withExport(api);
return api.on('editCommitted', (e) => log(`Stored ${JSON.stringify(e.newValue)} in ${e.columnId}.`));
}
const actions = {
csv: (api) => {
const csv = withExport(api).exportCsv({ bom: false, newline: '\n' });
say(csv.split('\n').join(' | '));
},
};
options.ariaLabel = "Store other values with a checkbox";
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('csv').addEventListener('click', () => actions.csv(api));
</script>
</body>
</html> This example needs an extra package. To run it locally, install with npm i @maxgrid/react @maxgrid/export.
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">
<button
type="button"
disabled={!api}
onClick={() => api && actions.csv(api)}
>Show CSV</button>
</div>
<Grid style={{ height: 230 }} ref={gridRef} options={options} />
</>
);
}import { withExport } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
agreed: 'Y' | 'N';
vip: 0 | 1;
}
const rows: Row[] = [
{ id: 1, name: 'Liam Carter', agreed: 'Y', vip: 1 },
{ id: 2, name: 'Emma Stone', agreed: 'N', vip: 0 },
{ id: 3, name: 'Noah Kim', agreed: 'Y', vip: 0 },
];
// Giving checkbox parameters turns it into a clickable control. The value is used as stored
// ('Y'/'N', 1/0), and filter, sort and export see that value too. size and shape (checkbox only) change the look.
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: 'Name', width: 140 },
{
id: 'agreed',
title: 'Agreed',
width: 120,
editable: true,
cellRenderer: 'checkbox',
cellRendererParams: { checkedValue: 'Y', uncheckedValue: 'N', label: 'Agreed', size: 'md' },
},
{
id: 'vip',
title: 'VIP',
kind: 'number',
width: 90,
editable: true,
cellRenderer: 'checkbox',
cellRendererParams: { checkedValue: 1, uncheckedValue: 0, size: 'lg', shape: 'round' },
},
],
rows,
rowKey: 'id',
};
let say: (message: LogText) => void = () => undefined;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
withExport(api);
return api.on('editCommitted', (e) =>
log(`Stored ${JSON.stringify(e.newValue)} in ${e.columnId}.`),
);
}
export const actions = {
csv: (api: GridApi<Row>) => {
const csv = withExport(api).exportCsv({ bom: false, newline: '\n' });
say(csv.split('\n').join(' | '));
},
}; This example needs an extra package. To run it locally, install with npm i @maxgrid/vue @maxgrid/export.
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">
<button type="button" :disabled="!api" @click="api && actions.csv(api)">Show CSV</button>
</div>
<Grid style="height: 230px" :options="options" @ready="onReady" />
</template>import { withExport } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
agreed: 'Y' | 'N';
vip: 0 | 1;
}
const rows: Row[] = [
{ id: 1, name: 'Liam Carter', agreed: 'Y', vip: 1 },
{ id: 2, name: 'Emma Stone', agreed: 'N', vip: 0 },
{ id: 3, name: 'Noah Kim', agreed: 'Y', vip: 0 },
];
// Giving checkbox parameters turns it into a clickable control. The value is used as stored
// ('Y'/'N', 1/0), and filter, sort and export see that value too. size and shape (checkbox only) change the look.
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: 'Name', width: 140 },
{
id: 'agreed',
title: 'Agreed',
width: 120,
editable: true,
cellRenderer: 'checkbox',
cellRendererParams: { checkedValue: 'Y', uncheckedValue: 'N', label: 'Agreed', size: 'md' },
},
{
id: 'vip',
title: 'VIP',
kind: 'number',
width: 90,
editable: true,
cellRenderer: 'checkbox',
cellRendererParams: { checkedValue: 1, uncheckedValue: 0, size: 'lg', shape: 'round' },
},
],
rows,
rowKey: 'id',
};
let say: (message: LogText) => void = () => undefined;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
withExport(api);
return api.on('editCommitted', (e) =>
log(`Stored ${JSON.stringify(e.newValue)} in ${e.columnId}.`),
);
}
export const actions = {
csv: (api: GridApi<Row>) => {
const csv = withExport(api).exportCsv({ bom: false, newline: '\n' });
say(csv.split('\n').join(' | '));
},
}; This example needs an extra package. To run it locally, install with npm i @maxgrid/angular @maxgrid/export.
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">
<button
type="button"
[disabled]="!api"
(click)="api && actions.csv(api)"
>Show CSV</button>
</div>
<mx-grid style="height: 230px" [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 { withExport } from '@maxgrid/export';
import type { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
agreed: 'Y' | 'N';
vip: 0 | 1;
}
const rows: Row[] = [
{ id: 1, name: 'Liam Carter', agreed: 'Y', vip: 1 },
{ id: 2, name: 'Emma Stone', agreed: 'N', vip: 0 },
{ id: 3, name: 'Noah Kim', agreed: 'Y', vip: 0 },
];
// Giving checkbox parameters turns it into a clickable control. The value is used as stored
// ('Y'/'N', 1/0), and filter, sort and export see that value too. size and shape (checkbox only) change the look.
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: 'Name', width: 140 },
{
id: 'agreed',
title: 'Agreed',
width: 120,
editable: true,
cellRenderer: 'checkbox',
cellRendererParams: { checkedValue: 'Y', uncheckedValue: 'N', label: 'Agreed', size: 'md' },
},
{
id: 'vip',
title: 'VIP',
kind: 'number',
width: 90,
editable: true,
cellRenderer: 'checkbox',
cellRendererParams: { checkedValue: 1, uncheckedValue: 0, size: 'lg', shape: 'round' },
},
],
rows,
rowKey: 'id',
};
let say: (message: LogText) => void = () => undefined;
export function setup(api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
withExport(api);
return api.on('editCommitted', (e) =>
log(`Stored ${JSON.stringify(e.newValue)} in ${e.columnId}.`),
);
}
export const actions = {
csv: (api: GridApi<Row>) => {
const csv = withExport(api).exportCsv({ bom: false, newline: '\n' });
say(csv.split('\n').join(' | '));
},
};