행·칸 선택 색 바꾸기Change the row and cell selection colors
선택한 행, 선택한 칸 블록, 처음 누른 칸의 색은 그리드의 CSS 변수라서 몇 줄로 바꿀 수 있어요. 글자색을 섞는 방식(color-mix)으로 정하면 밝은 테마와 어두운 테마 모두에서 잘 읽혀요.The colors of selected rows, the selected cell block and the anchor cell are CSS variables of the grid, so a few lines restyle them. Mixing with the theme color (color-mix) keeps them readable in light and dark themes.
코드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="colors">
<option value="default" selected>처음 색 (기본)</option>
<option value="green">초록</option>
<option value="violet">보라</option>
</select>
<button type="button" id="pick">행·칸 함께 고르기</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);
document.getElementById('colors').addEventListener('change', (event) => actions.colors(api, event.target.value));
document.getElementById('pick').addEventListener('click', () => actions.pick(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));const names = ['김민준', '이서연', '박지호', '최유나', '정도윤', '한소희'];
const countries = ['일본', '한국', '중국', '아일랜드', '이탈리아', '영국'];
// 행 선택(체크박스)과 칸 선택(끌기)을 함께 켰어요. 두 선택은 서로 다른 색으로 보여요.
export const options = {
columns: [
{ id: 'name', title: '이름', width: 110 },
{ id: 'country', title: '국가', width: 110 },
{ id: 'product', title: '상품', width: 130 },
{ id: 'price', title: '가격(만원)', kind: 'number', width: 110, align: 'end' },
],
rows: Array.from({ length: 10 }, (_, i) => ({
id: i + 1,
name: names[i % 6],
country: countries[(i * 5) % 6],
product: `상품 ${i + 1}`,
price: 80 + i * 37,
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
cellSelection: 'multiple',
};
// 선택 색은 그리드의 CSS 변수예요. --mxg-bg-selected는 선택한 행, --mxg-bg-cell-selected는 선택한 칸,
// --mxg-bg-cell-anchor는 처음 누른 칸, --mxg-cell-sel-line은 칸 블록의 테두리 색이에요.
// 테마 규칙보다 우선하도록 선택자를 조금 더 구체적으로 쓰고(:root:root .mxg.mxg), 섞는 색(color-mix)으로 정해서, 밝은 테마와 어두운 테마 어디서나 읽기 편해요.
const PALETTES = {
green: `:root:root .mxg.mxg {
--mxg-bg-selected: color-mix(in srgb, #3fa534 28%, var(--mxg-bg));
--mxg-fg-selected: var(--mxg-fg);
--mxg-bg-cell-selected: color-mix(in srgb, #3fa534 14%, var(--mxg-bg));
--mxg-bg-cell-anchor: var(--mxg-bg);
--mxg-cell-sel-line: #3fa534;
--mxg-focus-ring: #3fa534;
}`,
violet: `:root:root .mxg.mxg {
--mxg-bg-selected: color-mix(in srgb, #a855f7 26%, var(--mxg-bg));
--mxg-fg-selected: var(--mxg-fg);
--mxg-bg-cell-selected: color-mix(in srgb, #a855f7 14%, var(--mxg-bg));
--mxg-bg-cell-anchor: var(--mxg-bg);
--mxg-cell-sel-line: #c026d3;
--mxg-focus-ring: #c026d3;
}`,
default: '',
};
let style;
export function setup(_api) {
style = document.createElement('style');
document.head.append(style);
return () => {
style?.remove();
style = undefined;
};
}
export const actions = {
colors: (_api, value) => {
if (style)
style.textContent = PALETTES[value ?? 'default'] ?? '';
},
// 행 두 개와 칸 블록 하나를 한꺼번에 골라 두 색을 나란히 보여 줘요.
pick: (api) => {
api.setSelectedKeys([2, 3]);
api.selectCells({ rowKey: 5, columnId: 'country' }, { rowKey: 8, columnId: 'price' });
},
clear: (api) => {
api.clearSelection();
api.clearCellSelection();
},
};<!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="colors">
<option value="default" selected>처음 색 (기본)</option>
<option value="green">초록</option>
<option value="violet">보라</option>
</select>
<button type="button" id="pick">행·칸 함께 고르기</button>
<button type="button" id="clear">선택 지우기</button>
</div>
<div id="grid" style="height: 380px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const names = ['김민준', '이서연', '박지호', '최유나', '정도윤', '한소희'];
const countries = ['일본', '한국', '중국', '아일랜드', '이탈리아', '영국'];
// 행 선택(체크박스)과 칸 선택(끌기)을 함께 켰어요. 두 선택은 서로 다른 색으로 보여요.
const options = {
columns: [
{ id: 'name', title: '이름', width: 110 },
{ id: 'country', title: '국가', width: 110 },
{ id: 'product', title: '상품', width: 130 },
{ id: 'price', title: '가격(만원)', kind: 'number', width: 110, align: 'end' },
],
rows: Array.from({ length: 10 }, (_, i) => ({
id: i + 1,
name: names[i % 6],
country: countries[(i * 5) % 6],
product: `상품 ${i + 1}`,
price: 80 + i * 37,
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
cellSelection: 'multiple',
};
// 선택 색은 그리드의 CSS 변수예요. --mxg-bg-selected는 선택한 행, --mxg-bg-cell-selected는 선택한 칸,
// --mxg-bg-cell-anchor는 처음 누른 칸, --mxg-cell-sel-line은 칸 블록의 테두리 색이에요.
// 테마 규칙보다 우선하도록 선택자를 조금 더 구체적으로 쓰고(:root:root .mxg.mxg), 섞는 색(color-mix)으로 정해서, 밝은 테마와 어두운 테마 어디서나 읽기 편해요.
const PALETTES = {
green: `:root:root .mxg.mxg {
--mxg-bg-selected: color-mix(in srgb, #3fa534 28%, var(--mxg-bg));
--mxg-fg-selected: var(--mxg-fg);
--mxg-bg-cell-selected: color-mix(in srgb, #3fa534 14%, var(--mxg-bg));
--mxg-bg-cell-anchor: var(--mxg-bg);
--mxg-cell-sel-line: #3fa534;
--mxg-focus-ring: #3fa534;
}`,
violet: `:root:root .mxg.mxg {
--mxg-bg-selected: color-mix(in srgb, #a855f7 26%, var(--mxg-bg));
--mxg-fg-selected: var(--mxg-fg);
--mxg-bg-cell-selected: color-mix(in srgb, #a855f7 14%, var(--mxg-bg));
--mxg-bg-cell-anchor: var(--mxg-bg);
--mxg-cell-sel-line: #c026d3;
--mxg-focus-ring: #c026d3;
}`,
default: '',
};
let style;
function setup(_api) {
style = document.createElement('style');
document.head.append(style);
return () => {
style?.remove();
style = undefined;
};
}
const actions = {
colors: (_api, value) => {
if (style)
style.textContent = PALETTES[value ?? 'default'] ?? '';
},
// 행 두 개와 칸 블록 하나를 한꺼번에 골라 두 색을 나란히 보여 줘요.
pick: (api) => {
api.setSelectedKeys([2, 3]);
api.selectCells({ rowKey: 5, columnId: 'country' }, { rowKey: 8, columnId: 'price' });
},
clear: (api) => {
api.clearSelection();
api.clearCellSelection();
},
};
options.ariaLabel = "행·칸 선택 색 바꾸기";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api);
document.getElementById('colors').addEventListener('change', (event) => actions.colors(api, event.target.value));
document.getElementById('pick').addEventListener('click', () => actions.pick(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) : undefined), [api]);
return (
<>
<div className="toolbar">
<select
aria-label="선택 색"
disabled={!api}
onChange={(event) => api && actions.colors(api, event.target.value)}
>
<option value="default">처음 색 (기본)</option>
<option value="green">초록</option>
<option value="violet">보라</option>
</select>
<button
type="button"
disabled={!api}
onClick={() => api && actions.pick(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 interface Row {
id: number;
name: string;
country: string;
product: string;
price: number;
}
const names = ['김민준', '이서연', '박지호', '최유나', '정도윤', '한소희'];
const countries = ['일본', '한국', '중국', '아일랜드', '이탈리아', '영국'];
// 행 선택(체크박스)과 칸 선택(끌기)을 함께 켰어요. 두 선택은 서로 다른 색으로 보여요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: '이름', width: 110 },
{ id: 'country', title: '국가', width: 110 },
{ id: 'product', title: '상품', width: 130 },
{ id: 'price', title: '가격(만원)', kind: 'number', width: 110, align: 'end' },
],
rows: Array.from({ length: 10 }, (_, i) => ({
id: i + 1,
name: names[i % 6] as string,
country: countries[(i * 5) % 6] as string,
product: `상품 ${i + 1}`,
price: 80 + i * 37,
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
cellSelection: 'multiple',
};
// 선택 색은 그리드의 CSS 변수예요. --mxg-bg-selected는 선택한 행, --mxg-bg-cell-selected는 선택한 칸,
// --mxg-bg-cell-anchor는 처음 누른 칸, --mxg-cell-sel-line은 칸 블록의 테두리 색이에요.
// 테마 규칙보다 우선하도록 선택자를 조금 더 구체적으로 쓰고(:root:root .mxg.mxg), 섞는 색(color-mix)으로 정해서, 밝은 테마와 어두운 테마 어디서나 읽기 편해요.
const PALETTES: Record<string, string> = {
green: `:root:root .mxg.mxg {
--mxg-bg-selected: color-mix(in srgb, #3fa534 28%, var(--mxg-bg));
--mxg-fg-selected: var(--mxg-fg);
--mxg-bg-cell-selected: color-mix(in srgb, #3fa534 14%, var(--mxg-bg));
--mxg-bg-cell-anchor: var(--mxg-bg);
--mxg-cell-sel-line: #3fa534;
--mxg-focus-ring: #3fa534;
}`,
violet: `:root:root .mxg.mxg {
--mxg-bg-selected: color-mix(in srgb, #a855f7 26%, var(--mxg-bg));
--mxg-fg-selected: var(--mxg-fg);
--mxg-bg-cell-selected: color-mix(in srgb, #a855f7 14%, var(--mxg-bg));
--mxg-bg-cell-anchor: var(--mxg-bg);
--mxg-cell-sel-line: #c026d3;
--mxg-focus-ring: #c026d3;
}`,
default: '',
};
let style: HTMLStyleElement | undefined;
export function setup(_api: GridApi<Row>) {
style = document.createElement('style');
document.head.append(style);
return () => {
style?.remove();
style = undefined;
};
}
export const actions = {
colors: (_api: GridApi<Row>, value?: string) => {
if (style) style.textContent = PALETTES[value ?? 'default'] ?? '';
},
// 행 두 개와 칸 블록 하나를 한꺼번에 골라 두 색을 나란히 보여 줘요.
pick: (api: GridApi<Row>) => {
api.setSelectedKeys([2, 3]);
api.selectCells({ rowKey: 5, columnId: 'country' }, { rowKey: 8, columnId: 'price' });
},
clear: (api: GridApi<Row>) => {
api.clearSelection();
api.clearCellSelection();
},
};버튼을 누르면 이 예제의 코드가 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);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<select
aria-label="선택 색"
:disabled="!api"
@change="api && actions.colors(api, ($event.target as HTMLSelectElement).value)"
>
<option value="default" selected>처음 색 (기본)</option>
<option value="green">초록</option>
<option value="violet">보라</option>
</select>
<button type="button" :disabled="!api" @click="api && actions.pick(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 interface Row {
id: number;
name: string;
country: string;
product: string;
price: number;
}
const names = ['김민준', '이서연', '박지호', '최유나', '정도윤', '한소희'];
const countries = ['일본', '한국', '중국', '아일랜드', '이탈리아', '영국'];
// 행 선택(체크박스)과 칸 선택(끌기)을 함께 켰어요. 두 선택은 서로 다른 색으로 보여요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: '이름', width: 110 },
{ id: 'country', title: '국가', width: 110 },
{ id: 'product', title: '상품', width: 130 },
{ id: 'price', title: '가격(만원)', kind: 'number', width: 110, align: 'end' },
],
rows: Array.from({ length: 10 }, (_, i) => ({
id: i + 1,
name: names[i % 6] as string,
country: countries[(i * 5) % 6] as string,
product: `상품 ${i + 1}`,
price: 80 + i * 37,
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
cellSelection: 'multiple',
};
// 선택 색은 그리드의 CSS 변수예요. --mxg-bg-selected는 선택한 행, --mxg-bg-cell-selected는 선택한 칸,
// --mxg-bg-cell-anchor는 처음 누른 칸, --mxg-cell-sel-line은 칸 블록의 테두리 색이에요.
// 테마 규칙보다 우선하도록 선택자를 조금 더 구체적으로 쓰고(:root:root .mxg.mxg), 섞는 색(color-mix)으로 정해서, 밝은 테마와 어두운 테마 어디서나 읽기 편해요.
const PALETTES: Record<string, string> = {
green: `:root:root .mxg.mxg {
--mxg-bg-selected: color-mix(in srgb, #3fa534 28%, var(--mxg-bg));
--mxg-fg-selected: var(--mxg-fg);
--mxg-bg-cell-selected: color-mix(in srgb, #3fa534 14%, var(--mxg-bg));
--mxg-bg-cell-anchor: var(--mxg-bg);
--mxg-cell-sel-line: #3fa534;
--mxg-focus-ring: #3fa534;
}`,
violet: `:root:root .mxg.mxg {
--mxg-bg-selected: color-mix(in srgb, #a855f7 26%, var(--mxg-bg));
--mxg-fg-selected: var(--mxg-fg);
--mxg-bg-cell-selected: color-mix(in srgb, #a855f7 14%, var(--mxg-bg));
--mxg-bg-cell-anchor: var(--mxg-bg);
--mxg-cell-sel-line: #c026d3;
--mxg-focus-ring: #c026d3;
}`,
default: '',
};
let style: HTMLStyleElement | undefined;
export function setup(_api: GridApi<Row>) {
style = document.createElement('style');
document.head.append(style);
return () => {
style?.remove();
style = undefined;
};
}
export const actions = {
colors: (_api: GridApi<Row>, value?: string) => {
if (style) style.textContent = PALETTES[value ?? 'default'] ?? '';
},
// 행 두 개와 칸 블록 하나를 한꺼번에 골라 두 색을 나란히 보여 줘요.
pick: (api: GridApi<Row>) => {
api.setSelectedKeys([2, 3]);
api.selectCells({ rowKey: 5, columnId: 'country' }, { rowKey: 8, columnId: 'price' });
},
clear: (api: GridApi<Row>) => {
api.clearSelection();
api.clearCellSelection();
},
};버튼을 누르면 이 예제의 코드가 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.colors(api, $any($event.target).value)"
>
<option value="default" selected>처음 색 (기본)</option>
<option value="green">초록</option>
<option value="violet">보라</option>
</select>
<button
type="button"
[disabled]="!api"
(click)="api && actions.pick(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);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export interface Row {
id: number;
name: string;
country: string;
product: string;
price: number;
}
const names = ['김민준', '이서연', '박지호', '최유나', '정도윤', '한소희'];
const countries = ['일본', '한국', '중국', '아일랜드', '이탈리아', '영국'];
// 행 선택(체크박스)과 칸 선택(끌기)을 함께 켰어요. 두 선택은 서로 다른 색으로 보여요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: '이름', width: 110 },
{ id: 'country', title: '국가', width: 110 },
{ id: 'product', title: '상품', width: 130 },
{ id: 'price', title: '가격(만원)', kind: 'number', width: 110, align: 'end' },
],
rows: Array.from({ length: 10 }, (_, i) => ({
id: i + 1,
name: names[i % 6] as string,
country: countries[(i * 5) % 6] as string,
product: `상품 ${i + 1}`,
price: 80 + i * 37,
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
cellSelection: 'multiple',
};
// 선택 색은 그리드의 CSS 변수예요. --mxg-bg-selected는 선택한 행, --mxg-bg-cell-selected는 선택한 칸,
// --mxg-bg-cell-anchor는 처음 누른 칸, --mxg-cell-sel-line은 칸 블록의 테두리 색이에요.
// 테마 규칙보다 우선하도록 선택자를 조금 더 구체적으로 쓰고(:root:root .mxg.mxg), 섞는 색(color-mix)으로 정해서, 밝은 테마와 어두운 테마 어디서나 읽기 편해요.
const PALETTES: Record<string, string> = {
green: `:root:root .mxg.mxg {
--mxg-bg-selected: color-mix(in srgb, #3fa534 28%, var(--mxg-bg));
--mxg-fg-selected: var(--mxg-fg);
--mxg-bg-cell-selected: color-mix(in srgb, #3fa534 14%, var(--mxg-bg));
--mxg-bg-cell-anchor: var(--mxg-bg);
--mxg-cell-sel-line: #3fa534;
--mxg-focus-ring: #3fa534;
}`,
violet: `:root:root .mxg.mxg {
--mxg-bg-selected: color-mix(in srgb, #a855f7 26%, var(--mxg-bg));
--mxg-fg-selected: var(--mxg-fg);
--mxg-bg-cell-selected: color-mix(in srgb, #a855f7 14%, var(--mxg-bg));
--mxg-bg-cell-anchor: var(--mxg-bg);
--mxg-cell-sel-line: #c026d3;
--mxg-focus-ring: #c026d3;
}`,
default: '',
};
let style: HTMLStyleElement | undefined;
export function setup(_api: GridApi<Row>) {
style = document.createElement('style');
document.head.append(style);
return () => {
style?.remove();
style = undefined;
};
}
export const actions = {
colors: (_api: GridApi<Row>, value?: string) => {
if (style) style.textContent = PALETTES[value ?? 'default'] ?? '';
},
// 행 두 개와 칸 블록 하나를 한꺼번에 골라 두 색을 나란히 보여 줘요.
pick: (api: GridApi<Row>) => {
api.setSelectedKeys([2, 3]);
api.selectCells({ rowKey: 5, columnId: 'country' }, { rowKey: 8, columnId: 'price' });
},
clear: (api: GridApi<Row>) => {
api.clearSelection();
api.clearCellSelection();
},
};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="Selection colors" id="colors">
<option value="default" selected>Theme default</option>
<option value="green">Green</option>
<option value="violet">Violet</option>
</select>
<button type="button" id="pick">Pick rows and cells</button>
<button type="button" id="clear">Clear selection</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);
document.getElementById('colors').addEventListener('change', (event) => actions.colors(api, event.target.value));
document.getElementById('pick').addEventListener('click', () => actions.pick(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));const names = [
'Liam Carter',
'Emma Stone',
'Noah Parker',
'Olivia Reed',
'Ethan Brooks',
'Sophia Hayes',
];
const countries = ['Japan', 'Korea', 'China', 'Ireland', 'Italy', 'UK'];
// Row selection (checkbox) and cell selection (drag) are both on. The two selections show in different colors.
export const options = {
columns: [
{ id: 'name', title: 'Name', width: 130 },
{ id: 'country', title: 'Country', width: 110 },
{ id: 'product', title: 'Product', width: 130 },
{ id: 'price', title: 'Price ($)', kind: 'number', width: 110, align: 'end' },
],
rows: Array.from({ length: 10 }, (_, i) => ({
id: i + 1,
name: names[i % 6],
country: countries[(i * 5) % 6],
product: `Product ${i + 1}`,
price: 80 + i * 37,
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
cellSelection: 'multiple',
};
// Selection colors are CSS variables of the grid. --mxg-bg-selected is the selected row, --mxg-bg-cell-selected is the selected cell,
// --mxg-bg-cell-anchor is the cell pressed first, and --mxg-cell-sel-line is the border color of the cell block.
// The selector is made a bit more specific (:root:root .mxg.mxg) so it wins over the theme rules, and the colors are mixed (color-mix), so they read well on both light and dark themes.
const PALETTES = {
green: `:root:root .mxg.mxg {
--mxg-bg-selected: color-mix(in srgb, #3fa534 28%, var(--mxg-bg));
--mxg-fg-selected: var(--mxg-fg);
--mxg-bg-cell-selected: color-mix(in srgb, #3fa534 14%, var(--mxg-bg));
--mxg-bg-cell-anchor: var(--mxg-bg);
--mxg-cell-sel-line: #3fa534;
--mxg-focus-ring: #3fa534;
}`,
violet: `:root:root .mxg.mxg {
--mxg-bg-selected: color-mix(in srgb, #a855f7 26%, var(--mxg-bg));
--mxg-fg-selected: var(--mxg-fg);
--mxg-bg-cell-selected: color-mix(in srgb, #a855f7 14%, var(--mxg-bg));
--mxg-bg-cell-anchor: var(--mxg-bg);
--mxg-cell-sel-line: #c026d3;
--mxg-focus-ring: #c026d3;
}`,
default: '',
};
let style;
export function setup(_api) {
style = document.createElement('style');
document.head.append(style);
return () => {
style?.remove();
style = undefined;
};
}
export const actions = {
colors: (_api, value) => {
if (style)
style.textContent = PALETTES[value ?? 'default'] ?? '';
},
// Select two rows and one cell block at once to show the two colors side by side.
pick: (api) => {
api.setSelectedKeys([2, 3]);
api.selectCells({ rowKey: 5, columnId: 'country' }, { rowKey: 8, columnId: 'price' });
},
clear: (api) => {
api.clearSelection();
api.clearCellSelection();
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Change the row and cell selection colors</title>
</head>
<body>
<div class="toolbar">
<select aria-label="Selection colors" id="colors">
<option value="default" selected>Theme default</option>
<option value="green">Green</option>
<option value="violet">Violet</option>
</select>
<button type="button" id="pick">Pick rows and cells</button>
<button type="button" id="clear">Clear selection</button>
</div>
<div id="grid" style="height: 380px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const names = [
'Liam Carter',
'Emma Stone',
'Noah Parker',
'Olivia Reed',
'Ethan Brooks',
'Sophia Hayes',
];
const countries = ['Japan', 'Korea', 'China', 'Ireland', 'Italy', 'UK'];
// Row selection (checkbox) and cell selection (drag) are both on. The two selections show in different colors.
const options = {
columns: [
{ id: 'name', title: 'Name', width: 130 },
{ id: 'country', title: 'Country', width: 110 },
{ id: 'product', title: 'Product', width: 130 },
{ id: 'price', title: 'Price ($)', kind: 'number', width: 110, align: 'end' },
],
rows: Array.from({ length: 10 }, (_, i) => ({
id: i + 1,
name: names[i % 6],
country: countries[(i * 5) % 6],
product: `Product ${i + 1}`,
price: 80 + i * 37,
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
cellSelection: 'multiple',
};
// Selection colors are CSS variables of the grid. --mxg-bg-selected is the selected row, --mxg-bg-cell-selected is the selected cell,
// --mxg-bg-cell-anchor is the cell pressed first, and --mxg-cell-sel-line is the border color of the cell block.
// The selector is made a bit more specific (:root:root .mxg.mxg) so it wins over the theme rules, and the colors are mixed (color-mix), so they read well on both light and dark themes.
const PALETTES = {
green: `:root:root .mxg.mxg {
--mxg-bg-selected: color-mix(in srgb, #3fa534 28%, var(--mxg-bg));
--mxg-fg-selected: var(--mxg-fg);
--mxg-bg-cell-selected: color-mix(in srgb, #3fa534 14%, var(--mxg-bg));
--mxg-bg-cell-anchor: var(--mxg-bg);
--mxg-cell-sel-line: #3fa534;
--mxg-focus-ring: #3fa534;
}`,
violet: `:root:root .mxg.mxg {
--mxg-bg-selected: color-mix(in srgb, #a855f7 26%, var(--mxg-bg));
--mxg-fg-selected: var(--mxg-fg);
--mxg-bg-cell-selected: color-mix(in srgb, #a855f7 14%, var(--mxg-bg));
--mxg-bg-cell-anchor: var(--mxg-bg);
--mxg-cell-sel-line: #c026d3;
--mxg-focus-ring: #c026d3;
}`,
default: '',
};
let style;
function setup(_api) {
style = document.createElement('style');
document.head.append(style);
return () => {
style?.remove();
style = undefined;
};
}
const actions = {
colors: (_api, value) => {
if (style)
style.textContent = PALETTES[value ?? 'default'] ?? '';
},
// Select two rows and one cell block at once to show the two colors side by side.
pick: (api) => {
api.setSelectedKeys([2, 3]);
api.selectCells({ rowKey: 5, columnId: 'country' }, { rowKey: 8, columnId: 'price' });
},
clear: (api) => {
api.clearSelection();
api.clearCellSelection();
},
};
options.ariaLabel = "Change the row and cell selection colors";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api);
document.getElementById('colors').addEventListener('change', (event) => actions.colors(api, event.target.value));
document.getElementById('pick').addEventListener('click', () => actions.pick(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) : undefined), [api]);
return (
<>
<div className="toolbar">
<select
aria-label="Selection colors"
disabled={!api}
onChange={(event) => api && actions.colors(api, event.target.value)}
>
<option value="default">Theme default</option>
<option value="green">Green</option>
<option value="violet">Violet</option>
</select>
<button
type="button"
disabled={!api}
onClick={() => api && actions.pick(api)}
>Pick rows and cells</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.clear(api)}
>Clear selection</button>
</div>
<Grid style={{ height: 380 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
name: string;
country: string;
product: string;
price: number;
}
const names = [
'Liam Carter',
'Emma Stone',
'Noah Parker',
'Olivia Reed',
'Ethan Brooks',
'Sophia Hayes',
];
const countries = ['Japan', 'Korea', 'China', 'Ireland', 'Italy', 'UK'];
// Row selection (checkbox) and cell selection (drag) are both on. The two selections show in different colors.
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: 'Name', width: 130 },
{ id: 'country', title: 'Country', width: 110 },
{ id: 'product', title: 'Product', width: 130 },
{ id: 'price', title: 'Price ($)', kind: 'number', width: 110, align: 'end' },
],
rows: Array.from({ length: 10 }, (_, i) => ({
id: i + 1,
name: names[i % 6] as string,
country: countries[(i * 5) % 6] as string,
product: `Product ${i + 1}`,
price: 80 + i * 37,
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
cellSelection: 'multiple',
};
// Selection colors are CSS variables of the grid. --mxg-bg-selected is the selected row, --mxg-bg-cell-selected is the selected cell,
// --mxg-bg-cell-anchor is the cell pressed first, and --mxg-cell-sel-line is the border color of the cell block.
// The selector is made a bit more specific (:root:root .mxg.mxg) so it wins over the theme rules, and the colors are mixed (color-mix), so they read well on both light and dark themes.
const PALETTES: Record<string, string> = {
green: `:root:root .mxg.mxg {
--mxg-bg-selected: color-mix(in srgb, #3fa534 28%, var(--mxg-bg));
--mxg-fg-selected: var(--mxg-fg);
--mxg-bg-cell-selected: color-mix(in srgb, #3fa534 14%, var(--mxg-bg));
--mxg-bg-cell-anchor: var(--mxg-bg);
--mxg-cell-sel-line: #3fa534;
--mxg-focus-ring: #3fa534;
}`,
violet: `:root:root .mxg.mxg {
--mxg-bg-selected: color-mix(in srgb, #a855f7 26%, var(--mxg-bg));
--mxg-fg-selected: var(--mxg-fg);
--mxg-bg-cell-selected: color-mix(in srgb, #a855f7 14%, var(--mxg-bg));
--mxg-bg-cell-anchor: var(--mxg-bg);
--mxg-cell-sel-line: #c026d3;
--mxg-focus-ring: #c026d3;
}`,
default: '',
};
let style: HTMLStyleElement | undefined;
export function setup(_api: GridApi<Row>) {
style = document.createElement('style');
document.head.append(style);
return () => {
style?.remove();
style = undefined;
};
}
export const actions = {
colors: (_api: GridApi<Row>, value?: string) => {
if (style) style.textContent = PALETTES[value ?? 'default'] ?? '';
},
// Select two rows and one cell block at once to show the two colors side by side.
pick: (api: GridApi<Row>) => {
api.setSelectedKeys([2, 3]);
api.selectCells({ rowKey: 5, columnId: 'country' }, { rowKey: 8, columnId: 'price' });
},
clear: (api: GridApi<Row>) => {
api.clearSelection();
api.clearCellSelection();
},
};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);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<select
aria-label="Selection colors"
:disabled="!api"
@change="api && actions.colors(api, ($event.target as HTMLSelectElement).value)"
>
<option value="default" selected>Theme default</option>
<option value="green">Green</option>
<option value="violet">Violet</option>
</select>
<button type="button" :disabled="!api" @click="api && actions.pick(api)">Pick rows and cells</button>
<button type="button" :disabled="!api" @click="api && actions.clear(api)">Clear selection</button>
</div>
<Grid style="height: 380px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
name: string;
country: string;
product: string;
price: number;
}
const names = [
'Liam Carter',
'Emma Stone',
'Noah Parker',
'Olivia Reed',
'Ethan Brooks',
'Sophia Hayes',
];
const countries = ['Japan', 'Korea', 'China', 'Ireland', 'Italy', 'UK'];
// Row selection (checkbox) and cell selection (drag) are both on. The two selections show in different colors.
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: 'Name', width: 130 },
{ id: 'country', title: 'Country', width: 110 },
{ id: 'product', title: 'Product', width: 130 },
{ id: 'price', title: 'Price ($)', kind: 'number', width: 110, align: 'end' },
],
rows: Array.from({ length: 10 }, (_, i) => ({
id: i + 1,
name: names[i % 6] as string,
country: countries[(i * 5) % 6] as string,
product: `Product ${i + 1}`,
price: 80 + i * 37,
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
cellSelection: 'multiple',
};
// Selection colors are CSS variables of the grid. --mxg-bg-selected is the selected row, --mxg-bg-cell-selected is the selected cell,
// --mxg-bg-cell-anchor is the cell pressed first, and --mxg-cell-sel-line is the border color of the cell block.
// The selector is made a bit more specific (:root:root .mxg.mxg) so it wins over the theme rules, and the colors are mixed (color-mix), so they read well on both light and dark themes.
const PALETTES: Record<string, string> = {
green: `:root:root .mxg.mxg {
--mxg-bg-selected: color-mix(in srgb, #3fa534 28%, var(--mxg-bg));
--mxg-fg-selected: var(--mxg-fg);
--mxg-bg-cell-selected: color-mix(in srgb, #3fa534 14%, var(--mxg-bg));
--mxg-bg-cell-anchor: var(--mxg-bg);
--mxg-cell-sel-line: #3fa534;
--mxg-focus-ring: #3fa534;
}`,
violet: `:root:root .mxg.mxg {
--mxg-bg-selected: color-mix(in srgb, #a855f7 26%, var(--mxg-bg));
--mxg-fg-selected: var(--mxg-fg);
--mxg-bg-cell-selected: color-mix(in srgb, #a855f7 14%, var(--mxg-bg));
--mxg-bg-cell-anchor: var(--mxg-bg);
--mxg-cell-sel-line: #c026d3;
--mxg-focus-ring: #c026d3;
}`,
default: '',
};
let style: HTMLStyleElement | undefined;
export function setup(_api: GridApi<Row>) {
style = document.createElement('style');
document.head.append(style);
return () => {
style?.remove();
style = undefined;
};
}
export const actions = {
colors: (_api: GridApi<Row>, value?: string) => {
if (style) style.textContent = PALETTES[value ?? 'default'] ?? '';
},
// Select two rows and one cell block at once to show the two colors side by side.
pick: (api: GridApi<Row>) => {
api.setSelectedKeys([2, 3]);
api.selectCells({ rowKey: 5, columnId: 'country' }, { rowKey: 8, columnId: 'price' });
},
clear: (api: GridApi<Row>) => {
api.clearSelection();
api.clearCellSelection();
},
};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="Selection colors"
[disabled]="!api"
(change)="api && actions.colors(api, $any($event.target).value)"
>
<option value="default" selected>Theme default</option>
<option value="green">Green</option>
<option value="violet">Violet</option>
</select>
<button
type="button"
[disabled]="!api"
(click)="api && actions.pick(api)"
>Pick rows and cells</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.clear(api)"
>Clear selection</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);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { GridApi, GridOptions } from '@maxgrid/angular';
export interface Row {
id: number;
name: string;
country: string;
product: string;
price: number;
}
const names = [
'Liam Carter',
'Emma Stone',
'Noah Parker',
'Olivia Reed',
'Ethan Brooks',
'Sophia Hayes',
];
const countries = ['Japan', 'Korea', 'China', 'Ireland', 'Italy', 'UK'];
// Row selection (checkbox) and cell selection (drag) are both on. The two selections show in different colors.
export const options: GridOptions<Row> = {
columns: [
{ id: 'name', title: 'Name', width: 130 },
{ id: 'country', title: 'Country', width: 110 },
{ id: 'product', title: 'Product', width: 130 },
{ id: 'price', title: 'Price ($)', kind: 'number', width: 110, align: 'end' },
],
rows: Array.from({ length: 10 }, (_, i) => ({
id: i + 1,
name: names[i % 6] as string,
country: countries[(i * 5) % 6] as string,
product: `Product ${i + 1}`,
price: 80 + i * 37,
})),
rowKey: 'id',
selection: { mode: 'multiple', checkbox: true },
cellSelection: 'multiple',
};
// Selection colors are CSS variables of the grid. --mxg-bg-selected is the selected row, --mxg-bg-cell-selected is the selected cell,
// --mxg-bg-cell-anchor is the cell pressed first, and --mxg-cell-sel-line is the border color of the cell block.
// The selector is made a bit more specific (:root:root .mxg.mxg) so it wins over the theme rules, and the colors are mixed (color-mix), so they read well on both light and dark themes.
const PALETTES: Record<string, string> = {
green: `:root:root .mxg.mxg {
--mxg-bg-selected: color-mix(in srgb, #3fa534 28%, var(--mxg-bg));
--mxg-fg-selected: var(--mxg-fg);
--mxg-bg-cell-selected: color-mix(in srgb, #3fa534 14%, var(--mxg-bg));
--mxg-bg-cell-anchor: var(--mxg-bg);
--mxg-cell-sel-line: #3fa534;
--mxg-focus-ring: #3fa534;
}`,
violet: `:root:root .mxg.mxg {
--mxg-bg-selected: color-mix(in srgb, #a855f7 26%, var(--mxg-bg));
--mxg-fg-selected: var(--mxg-fg);
--mxg-bg-cell-selected: color-mix(in srgb, #a855f7 14%, var(--mxg-bg));
--mxg-bg-cell-anchor: var(--mxg-bg);
--mxg-cell-sel-line: #c026d3;
--mxg-focus-ring: #c026d3;
}`,
default: '',
};
let style: HTMLStyleElement | undefined;
export function setup(_api: GridApi<Row>) {
style = document.createElement('style');
document.head.append(style);
return () => {
style?.remove();
style = undefined;
};
}
export const actions = {
colors: (_api: GridApi<Row>, value?: string) => {
if (style) style.textContent = PALETTES[value ?? 'default'] ?? '';
},
// Select two rows and one cell block at once to show the two colors side by side.
pick: (api: GridApi<Row>) => {
api.setSelectedKeys([2, 3]);
api.selectCells({ rowKey: 5, columnId: 'country' }, { rowKey: 8, columnId: 'price' });
},
clear: (api: GridApi<Row>) => {
api.clearSelection();
api.clearCellSelection();
},
};