칸 하나에 스타일 얹기Style a single cell
setCellStyle(rowKey, columnId, { className, style })는 칸 하나에 클래스와 스타일을 얹고 null로 지워요. 행 키와 컬럼 id로 보관해서 정렬·필터·쪽 나누기를 거쳐도 같은 칸을 따라가요.setCellStyle(rowKey, columnId, { className, style }) layers a class and styles on one cell, and null removes it. It is kept by row key and column id, so it follows the cell through sort, filter and paging.
코드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="mark">스타일 넣기</button>
<button type="button" id="unmark">스타일 지우기</button>
<button type="button" id="sort">점수로 정렬</button>
<button type="button" id="clear">모두 지우기</button>
</div>
<div id="grid" style="height: 380px"></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('mark').addEventListener('click', () => actions.mark(api));
document.getElementById('unmark').addEventListener('click', () => actions.unmark(api));
document.getElementById('sort').addEventListener('click', () => actions.sort(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));const en = document.documentElement.dataset.lang === 'en';
const names = en
? ['Alex', 'Blair', 'Casey', 'Drew', 'Emery', 'Finn', 'Gray', 'Harper']
: ['김서연', '이준호', '박민지', '최도윤', '정하은', '한지우', '오세훈', '윤아름'];
const teams = en ? ['Dev', 'Design', 'Ops'] : ['개발', '디자인', '운영'];
export const options = {
columns: [
{ id: 'name', title: en ? 'Name' : '이름', width: 120, sortable: true },
{ id: 'team', title: en ? 'Team' : '팀', width: 100, sortable: true },
{ id: 'score', title: en ? 'Score' : '점수', kind: 'number', width: 90, sortable: true },
],
rows: names.map((name, i) => ({
id: i + 1,
name,
team: teams[i % 3],
score: 55 + ((i * 11) % 45),
})),
rowKey: 'id',
};
let say = () => { };
export function setup(_api, log) {
say = log;
}
const MARK = { style: { backgroundColor: '#fff3bf', color: '#1f2933', fontWeight: 700 } };
export const actions = {
// 3번 행의 점수 칸 하나에만 스타일을 얹어요. 정렬해도 같은 행의 칸을 따라가요.
mark: (api) => {
api.setCellStyle(3, 'score', MARK);
say({ ko: '3번 행 점수 칸에 스타일을 얹었어요', en: 'Styled the score cell of row 3' });
},
unmark: (api) => {
api.setCellStyle(3, 'score', null);
say({ ko: '스타일을 지웠어요', en: 'Removed the style' });
},
sort: (api) => api.setSortModel([{ columnId: 'score', direction: 'desc' }]),
clear: (api) => {
api.clearCellStyles();
say({ ko: '모든 칸 스타일을 지웠어요', en: 'Cleared every cell style' });
},
};<!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="mark">스타일 넣기</button>
<button type="button" id="unmark">스타일 지우기</button>
<button type="button" id="sort">점수로 정렬</button>
<button type="button" id="clear">모두 지우기</button>
</div>
<div id="grid" style="height: 380px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const en = document.documentElement.dataset.lang === 'en';
const names = en
? ['Alex', 'Blair', 'Casey', 'Drew', 'Emery', 'Finn', 'Gray', 'Harper']
: ['김서연', '이준호', '박민지', '최도윤', '정하은', '한지우', '오세훈', '윤아름'];
const teams = en ? ['Dev', 'Design', 'Ops'] : ['개발', '디자인', '운영'];
const options = {
columns: [
{ id: 'name', title: en ? 'Name' : '이름', width: 120, sortable: true },
{ id: 'team', title: en ? 'Team' : '팀', width: 100, sortable: true },
{ id: 'score', title: en ? 'Score' : '점수', kind: 'number', width: 90, sortable: true },
],
rows: names.map((name, i) => ({
id: i + 1,
name,
team: teams[i % 3],
score: 55 + ((i * 11) % 45),
})),
rowKey: 'id',
};
let say = () => { };
function setup(_api, log) {
say = log;
}
const MARK = { style: { backgroundColor: '#fff3bf', color: '#1f2933', fontWeight: 700 } };
const actions = {
// 3번 행의 점수 칸 하나에만 스타일을 얹어요. 정렬해도 같은 행의 칸을 따라가요.
mark: (api) => {
api.setCellStyle(3, 'score', MARK);
say({ ko: '3번 행 점수 칸에 스타일을 얹었어요', en: 'Styled the score cell of row 3' });
},
unmark: (api) => {
api.setCellStyle(3, 'score', null);
say({ ko: '스타일을 지웠어요', en: 'Removed the style' });
},
sort: (api) => api.setSortModel([{ columnId: 'score', direction: 'desc' }]),
clear: (api) => {
api.clearCellStyles();
say({ ko: '모든 칸 스타일을 지웠어요', en: 'Cleared every cell style' });
},
};
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('mark').addEventListener('click', () => actions.mark(api));
document.getElementById('unmark').addEventListener('click', () => actions.unmark(api));
document.getElementById('sort').addEventListener('click', () => actions.sort(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));
</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">
<button
type="button"
disabled={!api}
onClick={() => api && actions.mark(api)}
>스타일 넣기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.unmark(api)}
>스타일 지우기</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.sort(api)}
>점수로 정렬</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.clear(api)}
>모두 지우기</button>
</div>
<Grid style={{ height: 380 }} 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;
score: number;
}
const en = document.documentElement.dataset.lang === 'en';
const names = en
? ['Alex', 'Blair', 'Casey', 'Drew', 'Emery', 'Finn', 'Gray', 'Harper']
: ['김서연', '이준호', '박민지', '최도윤', '정하은', '한지우', '오세훈', '윤아름'];
const teams = en ? ['Dev', 'Design', 'Ops'] : ['개발', '디자인', '운영'];
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: en ? 'Name' : '이름', width: 120, sortable: true },
{ id: 'team', title: en ? 'Team' : '팀', width: 100, sortable: true },
{ id: 'score', title: en ? 'Score' : '점수', kind: 'number', width: 90, sortable: true },
],
rows: names.map((name, i) => ({
id: i + 1,
name,
team: teams[i % 3] as string,
score: 55 + ((i * 11) % 45),
})),
rowKey: 'id',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
}
const MARK = { style: { backgroundColor: '#fff3bf', color: '#1f2933', fontWeight: 700 } };
export const actions = {
// 3번 행의 점수 칸 하나에만 스타일을 얹어요. 정렬해도 같은 행의 칸을 따라가요.
mark: (api: GridApi<Row>) => {
api.setCellStyle(3, 'score', MARK);
say({ ko: '3번 행 점수 칸에 스타일을 얹었어요', en: 'Styled the score cell of row 3' });
},
unmark: (api: GridApi<Row>) => {
api.setCellStyle(3, 'score', null);
say({ ko: '스타일을 지웠어요', en: 'Removed the style' });
},
sort: (api: GridApi<Row>) => api.setSortModel([{ columnId: 'score', direction: 'desc' }]),
clear: (api: GridApi<Row>) => {
api.clearCellStyles();
say({ ko: '모든 칸 스타일을 지웠어요', en: 'Cleared every cell style' });
},
};버튼을 누르면 이 예제의 코드가 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.mark(api)">스타일 넣기</button>
<button type="button" :disabled="!api" @click="api && actions.unmark(api)">스타일 지우기</button>
<button type="button" :disabled="!api" @click="api && actions.sort(api)">점수로 정렬</button>
<button type="button" :disabled="!api" @click="api && actions.clear(api)">모두 지우기</button>
</div>
<Grid style="height: 380px" :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;
score: number;
}
const en = document.documentElement.dataset.lang === 'en';
const names = en
? ['Alex', 'Blair', 'Casey', 'Drew', 'Emery', 'Finn', 'Gray', 'Harper']
: ['김서연', '이준호', '박민지', '최도윤', '정하은', '한지우', '오세훈', '윤아름'];
const teams = en ? ['Dev', 'Design', 'Ops'] : ['개발', '디자인', '운영'];
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: en ? 'Name' : '이름', width: 120, sortable: true },
{ id: 'team', title: en ? 'Team' : '팀', width: 100, sortable: true },
{ id: 'score', title: en ? 'Score' : '점수', kind: 'number', width: 90, sortable: true },
],
rows: names.map((name, i) => ({
id: i + 1,
name,
team: teams[i % 3] as string,
score: 55 + ((i * 11) % 45),
})),
rowKey: 'id',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
}
const MARK = { style: { backgroundColor: '#fff3bf', color: '#1f2933', fontWeight: 700 } };
export const actions = {
// 3번 행의 점수 칸 하나에만 스타일을 얹어요. 정렬해도 같은 행의 칸을 따라가요.
mark: (api: GridApi<Row>) => {
api.setCellStyle(3, 'score', MARK);
say({ ko: '3번 행 점수 칸에 스타일을 얹었어요', en: 'Styled the score cell of row 3' });
},
unmark: (api: GridApi<Row>) => {
api.setCellStyle(3, 'score', null);
say({ ko: '스타일을 지웠어요', en: 'Removed the style' });
},
sort: (api: GridApi<Row>) => api.setSortModel([{ columnId: 'score', direction: 'desc' }]),
clear: (api: GridApi<Row>) => {
api.clearCellStyles();
say({ ko: '모든 칸 스타일을 지웠어요', en: 'Cleared every cell style' });
},
};버튼을 누르면 이 예제의 코드가 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.mark(api)"
>스타일 넣기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.unmark(api)"
>스타일 지우기</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.sort(api)"
>점수로 정렬</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.clear(api)"
>모두 지우기</button>
</div>
<mx-grid style="height: 380px" [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;
score: number;
}
const en = document.documentElement.dataset.lang === 'en';
const names = en
? ['Alex', 'Blair', 'Casey', 'Drew', 'Emery', 'Finn', 'Gray', 'Harper']
: ['김서연', '이준호', '박민지', '최도윤', '정하은', '한지우', '오세훈', '윤아름'];
const teams = en ? ['Dev', 'Design', 'Ops'] : ['개발', '디자인', '운영'];
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: en ? 'Name' : '이름', width: 120, sortable: true },
{ id: 'team', title: en ? 'Team' : '팀', width: 100, sortable: true },
{ id: 'score', title: en ? 'Score' : '점수', kind: 'number', width: 90, sortable: true },
],
rows: names.map((name, i) => ({
id: i + 1,
name,
team: teams[i % 3] as string,
score: 55 + ((i * 11) % 45),
})),
rowKey: 'id',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
}
const MARK = { style: { backgroundColor: '#fff3bf', color: '#1f2933', fontWeight: 700 } };
export const actions = {
// 3번 행의 점수 칸 하나에만 스타일을 얹어요. 정렬해도 같은 행의 칸을 따라가요.
mark: (api: GridApi<Row>) => {
api.setCellStyle(3, 'score', MARK);
say({ ko: '3번 행 점수 칸에 스타일을 얹었어요', en: 'Styled the score cell of row 3' });
},
unmark: (api: GridApi<Row>) => {
api.setCellStyle(3, 'score', null);
say({ ko: '스타일을 지웠어요', en: 'Removed the style' });
},
sort: (api: GridApi<Row>) => api.setSortModel([{ columnId: 'score', direction: 'desc' }]),
clear: (api: GridApi<Row>) => {
api.clearCellStyles();
say({ ko: '모든 칸 스타일을 지웠어요', en: 'Cleared every cell style' });
},
};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="mark">Add style</button>
<button type="button" id="unmark">Remove style</button>
<button type="button" id="sort">Sort by score</button>
<button type="button" id="clear">Clear all</button>
</div>
<div id="grid" style="height: 380px"></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('mark').addEventListener('click', () => actions.mark(api));
document.getElementById('unmark').addEventListener('click', () => actions.unmark(api));
document.getElementById('sort').addEventListener('click', () => actions.sort(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));const names = ['Alex', 'Blair', 'Casey', 'Drew', 'Emery', 'Finn', 'Gray', 'Harper'];
const teams = ['Dev', 'Design', 'Ops'];
export const options = {
columns: [
{ id: 'name', title: 'Name', width: 120, sortable: true },
{ id: 'team', title: 'Team', width: 100, sortable: true },
{ id: 'score', title: 'Score', kind: 'number', width: 90, sortable: true },
],
rows: names.map((name, i) => ({
id: i + 1,
name,
team: teams[i % 3],
score: 55 + ((i * 11) % 45),
})),
rowKey: 'id',
};
let say = () => { };
export function setup(_api, log) {
say = log;
}
const MARK = { style: { backgroundColor: '#fff3bf', color: '#1f2933', fontWeight: 700 } };
export const actions = {
// Style only the score cell of row 3. It follows the same row's cell when you sort.
mark: (api) => {
api.setCellStyle(3, 'score', MARK);
say('Styled the score cell of row 3');
},
unmark: (api) => {
api.setCellStyle(3, 'score', null);
say('Removed the style');
},
sort: (api) => api.setSortModel([{ columnId: 'score', direction: 'desc' }]),
clear: (api) => {
api.clearCellStyles();
say('Cleared every cell style');
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Style a single cell</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="mark">Add style</button>
<button type="button" id="unmark">Remove style</button>
<button type="button" id="sort">Sort by score</button>
<button type="button" id="clear">Clear all</button>
</div>
<div id="grid" style="height: 380px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const names = ['Alex', 'Blair', 'Casey', 'Drew', 'Emery', 'Finn', 'Gray', 'Harper'];
const teams = ['Dev', 'Design', 'Ops'];
const options = {
columns: [
{ id: 'name', title: 'Name', width: 120, sortable: true },
{ id: 'team', title: 'Team', width: 100, sortable: true },
{ id: 'score', title: 'Score', kind: 'number', width: 90, sortable: true },
],
rows: names.map((name, i) => ({
id: i + 1,
name,
team: teams[i % 3],
score: 55 + ((i * 11) % 45),
})),
rowKey: 'id',
};
let say = () => { };
function setup(_api, log) {
say = log;
}
const MARK = { style: { backgroundColor: '#fff3bf', color: '#1f2933', fontWeight: 700 } };
const actions = {
// Style only the score cell of row 3. It follows the same row's cell when you sort.
mark: (api) => {
api.setCellStyle(3, 'score', MARK);
say('Styled the score cell of row 3');
},
unmark: (api) => {
api.setCellStyle(3, 'score', null);
say('Removed the style');
},
sort: (api) => api.setSortModel([{ columnId: 'score', direction: 'desc' }]),
clear: (api) => {
api.clearCellStyles();
say('Cleared every cell style');
},
};
options.ariaLabel = "Style a single cell";
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('mark').addEventListener('click', () => actions.mark(api));
document.getElementById('unmark').addEventListener('click', () => actions.unmark(api));
document.getElementById('sort').addEventListener('click', () => actions.sort(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));
</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">
<button
type="button"
disabled={!api}
onClick={() => api && actions.mark(api)}
>Add style</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.unmark(api)}
>Remove style</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.sort(api)}
>Sort by score</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.clear(api)}
>Clear all</button>
</div>
<Grid style={{ height: 380 }} 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;
score: number;
}
const names = ['Alex', 'Blair', 'Casey', 'Drew', 'Emery', 'Finn', 'Gray', 'Harper'];
const teams = ['Dev', 'Design', 'Ops'];
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: 'Name', width: 120, sortable: true },
{ id: 'team', title: 'Team', width: 100, sortable: true },
{ id: 'score', title: 'Score', kind: 'number', width: 90, sortable: true },
],
rows: names.map((name, i) => ({
id: i + 1,
name,
team: teams[i % 3] as string,
score: 55 + ((i * 11) % 45),
})),
rowKey: 'id',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
}
const MARK = { style: { backgroundColor: '#fff3bf', color: '#1f2933', fontWeight: 700 } };
export const actions = {
// Style only the score cell of row 3. It follows the same row's cell when you sort.
mark: (api: GridApi<Row>) => {
api.setCellStyle(3, 'score', MARK);
say('Styled the score cell of row 3');
},
unmark: (api: GridApi<Row>) => {
api.setCellStyle(3, 'score', null);
say('Removed the style');
},
sort: (api: GridApi<Row>) => api.setSortModel([{ columnId: 'score', direction: 'desc' }]),
clear: (api: GridApi<Row>) => {
api.clearCellStyles();
say('Cleared every cell style');
},
};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.mark(api)">Add style</button>
<button type="button" :disabled="!api" @click="api && actions.unmark(api)">Remove style</button>
<button type="button" :disabled="!api" @click="api && actions.sort(api)">Sort by score</button>
<button type="button" :disabled="!api" @click="api && actions.clear(api)">Clear all</button>
</div>
<Grid style="height: 380px" :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;
score: number;
}
const names = ['Alex', 'Blair', 'Casey', 'Drew', 'Emery', 'Finn', 'Gray', 'Harper'];
const teams = ['Dev', 'Design', 'Ops'];
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: 'Name', width: 120, sortable: true },
{ id: 'team', title: 'Team', width: 100, sortable: true },
{ id: 'score', title: 'Score', kind: 'number', width: 90, sortable: true },
],
rows: names.map((name, i) => ({
id: i + 1,
name,
team: teams[i % 3] as string,
score: 55 + ((i * 11) % 45),
})),
rowKey: 'id',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
}
const MARK = { style: { backgroundColor: '#fff3bf', color: '#1f2933', fontWeight: 700 } };
export const actions = {
// Style only the score cell of row 3. It follows the same row's cell when you sort.
mark: (api: GridApi<Row>) => {
api.setCellStyle(3, 'score', MARK);
say('Styled the score cell of row 3');
},
unmark: (api: GridApi<Row>) => {
api.setCellStyle(3, 'score', null);
say('Removed the style');
},
sort: (api: GridApi<Row>) => api.setSortModel([{ columnId: 'score', direction: 'desc' }]),
clear: (api: GridApi<Row>) => {
api.clearCellStyles();
say('Cleared every cell style');
},
};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.mark(api)"
>Add style</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.unmark(api)"
>Remove style</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.sort(api)"
>Sort by score</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.clear(api)"
>Clear all</button>
</div>
<mx-grid style="height: 380px" [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;
score: number;
}
const names = ['Alex', 'Blair', 'Casey', 'Drew', 'Emery', 'Finn', 'Gray', 'Harper'];
const teams = ['Dev', 'Design', 'Ops'];
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: 'Name', width: 120, sortable: true },
{ id: 'team', title: 'Team', width: 100, sortable: true },
{ id: 'score', title: 'Score', kind: 'number', width: 90, sortable: true },
],
rows: names.map((name, i) => ({
id: i + 1,
name,
team: teams[i % 3] as string,
score: 55 + ((i * 11) % 45),
})),
rowKey: 'id',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
}
const MARK = { style: { backgroundColor: '#fff3bf', color: '#1f2933', fontWeight: 700 } };
export const actions = {
// Style only the score cell of row 3. It follows the same row's cell when you sort.
mark: (api: GridApi<Row>) => {
api.setCellStyle(3, 'score', MARK);
say('Styled the score cell of row 3');
},
unmark: (api: GridApi<Row>) => {
api.setCellStyle(3, 'score', null);
say('Removed the style');
},
sort: (api: GridApi<Row>) => api.setSortModel([{ columnId: 'score', direction: 'desc' }]),
clear: (api: GridApi<Row>) => {
api.clearCellStyles();
say('Cleared every cell style');
},
};