아바타: 사진이나 이니셜Avatars: photo or initials
이름 옆에 동그란 사진을 보여 줘요. 사진이 없거나 안전하지 않은 주소면 이름의 앞 글자(이니셜)로 대신해요.Show a round photo next to the name. Without a photo, or with an unsafe address, the initials are shown instead.
코드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="text">
<option value="both" selected>사진과 이름</option>
<option value="photo">사진만</option>
</select>
<button type="button" id="give">2번 줄에 사진 넣기</button>
</div>
<div id="grid" style="height: 280px"></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('text').addEventListener('change', (event) => actions.text(api, event.target.value));
document.getElementById('give').addEventListener('click', () => actions.give(api));// 사진 대신 쓸 작은 그림을 캔버스로 만들어요(네트워크 없이 보이게). 어떤 http(s) 주소나 래스터 data:image 주소도 쓸 수 있고,
// SVG data 주소나 그 밖의 방식은 막혀서 이니셜로 보여요.
function swatch(color) {
const canvas = document.createElement('canvas');
canvas.width = canvas.height = 32;
const g = canvas.getContext('2d');
if (g) {
g.fillStyle = color;
g.fillRect(0, 0, 32, 32);
g.fillStyle = 'rgba(255,255,255,0.55)';
g.beginPath();
g.arc(16, 13, 6, 0, Math.PI * 2);
g.fill();
g.fillRect(6, 22, 20, 10);
}
return canvas.toDataURL('image/png');
}
const rows = [
{ id: 1, name: '김서연', photo: swatch('#3b82f6'), role: '분석가' },
{ id: 2, name: '이준호', photo: '', role: '관리자' },
{ id: 3, name: '박민지', photo: 'javascript:alert(1)', role: '연구원' },
{ id: 4, name: 'Grace Hopper', photo: swatch('#16a34a'), role: 'Admiral' },
{ id: 5, name: '', photo: '', role: '미배정' },
];
let withText = true;
const columns = () => [
{
id: 'name',
title: '이름',
width: 190,
cellRenderer: 'avatar',
cellRendererParams: { image: 'photo', showText: withText },
},
{ id: 'role', title: '역할', width: 110 },
{
id: 'photo',
title: '사진 주소',
width: 130,
formatValue: (v) => (String(v).startsWith('data:') ? '(그림 데이터)' : String(v) || '(없음)'),
},
];
export const options = {
columns: columns(),
rows,
rowKey: 'id',
saveMode: 'none',
};
let say = () => { };
export function setup(_api, log) {
say = log;
withText = true;
log({
ko: '사진이 없거나 위험한 주소면 이름의 앞 글자(이니셜)가 동그라미에 들어가요.',
en: 'With no photo or an unsafe address, the first letter of the name (initials) goes in the circle.',
});
}
export const actions = {
text: (api, value) => {
withText = value !== 'photo';
api.setColumns(columns());
say({
ko: withText ? '사진과 이름을 함께 보여요.' : '사진(이니셜)만 보여요.',
en: withText ? 'Showing the photo and the name.' : 'Showing only the photo (initials).',
});
},
give: (api) => {
const row = api.getRowByKey(2);
if (row)
api.updateRows([{ ...row, photo: swatch('#f59e0b') }]);
say({ ko: '2번 줄에 사진을 넣었어요.', en: 'Put a photo on row 2.' });
},
};<!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="text">
<option value="both" selected>사진과 이름</option>
<option value="photo">사진만</option>
</select>
<button type="button" id="give">2번 줄에 사진 넣기</button>
</div>
<div id="grid" style="height: 280px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
// 사진 대신 쓸 작은 그림을 캔버스로 만들어요(네트워크 없이 보이게). 어떤 http(s) 주소나 래스터 data:image 주소도 쓸 수 있고,
// SVG data 주소나 그 밖의 방식은 막혀서 이니셜로 보여요.
function swatch(color) {
const canvas = document.createElement('canvas');
canvas.width = canvas.height = 32;
const g = canvas.getContext('2d');
if (g) {
g.fillStyle = color;
g.fillRect(0, 0, 32, 32);
g.fillStyle = 'rgba(255,255,255,0.55)';
g.beginPath();
g.arc(16, 13, 6, 0, Math.PI * 2);
g.fill();
g.fillRect(6, 22, 20, 10);
}
return canvas.toDataURL('image/png');
}
const rows = [
{ id: 1, name: '김서연', photo: swatch('#3b82f6'), role: '분석가' },
{ id: 2, name: '이준호', photo: '', role: '관리자' },
{ id: 3, name: '박민지', photo: 'javascript:alert(1)', role: '연구원' },
{ id: 4, name: 'Grace Hopper', photo: swatch('#16a34a'), role: 'Admiral' },
{ id: 5, name: '', photo: '', role: '미배정' },
];
let withText = true;
const columns = () => [
{
id: 'name',
title: '이름',
width: 190,
cellRenderer: 'avatar',
cellRendererParams: { image: 'photo', showText: withText },
},
{ id: 'role', title: '역할', width: 110 },
{
id: 'photo',
title: '사진 주소',
width: 130,
formatValue: (v) => (String(v).startsWith('data:') ? '(그림 데이터)' : String(v) || '(없음)'),
},
];
const options = {
columns: columns(),
rows,
rowKey: 'id',
saveMode: 'none',
};
let say = () => { };
function setup(_api, log) {
say = log;
withText = true;
log({
ko: '사진이 없거나 위험한 주소면 이름의 앞 글자(이니셜)가 동그라미에 들어가요.',
en: 'With no photo or an unsafe address, the first letter of the name (initials) goes in the circle.',
});
}
const actions = {
text: (api, value) => {
withText = value !== 'photo';
api.setColumns(columns());
say({
ko: withText ? '사진과 이름을 함께 보여요.' : '사진(이니셜)만 보여요.',
en: withText ? 'Showing the photo and the name.' : 'Showing only the photo (initials).',
});
},
give: (api) => {
const row = api.getRowByKey(2);
if (row)
api.updateRows([{ ...row, photo: swatch('#f59e0b') }]);
say({ ko: '2번 줄에 사진을 넣었어요.', en: 'Put a photo on row 2.' });
},
};
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('text').addEventListener('change', (event) => actions.text(api, event.target.value));
document.getElementById('give').addEventListener('click', () => actions.give(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">
<select
aria-label="표시"
disabled={!api}
onChange={(event) => api && actions.text(api, event.target.value)}
>
<option value="both">사진과 이름</option>
<option value="photo">사진만</option>
</select>
<button
type="button"
disabled={!api}
onClick={() => api && actions.give(api)}
>2번 줄에 사진 넣기</button>
</div>
<Grid style={{ height: 280 }} 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;
photo: string;
role: string;
}
// 사진 대신 쓸 작은 그림을 캔버스로 만들어요(네트워크 없이 보이게). 어떤 http(s) 주소나 래스터 data:image 주소도 쓸 수 있고,
// SVG data 주소나 그 밖의 방식은 막혀서 이니셜로 보여요.
function swatch(color: string): string {
const canvas = document.createElement('canvas');
canvas.width = canvas.height = 32;
const g = canvas.getContext('2d');
if (g) {
g.fillStyle = color;
g.fillRect(0, 0, 32, 32);
g.fillStyle = 'rgba(255,255,255,0.55)';
g.beginPath();
g.arc(16, 13, 6, 0, Math.PI * 2);
g.fill();
g.fillRect(6, 22, 20, 10);
}
return canvas.toDataURL('image/png');
}
const rows: Row[] = [
{ id: 1, name: '김서연', photo: swatch('#3b82f6'), role: '분석가' },
{ id: 2, name: '이준호', photo: '', role: '관리자' },
{ id: 3, name: '박민지', photo: 'javascript:alert(1)', role: '연구원' },
{ id: 4, name: 'Grace Hopper', photo: swatch('#16a34a'), role: 'Admiral' },
{ id: 5, name: '', photo: '', role: '미배정' },
];
let withText = true;
const columns = (): NonNullable<GridOptions<Row>['columns']> => [
{
id: 'name',
title: '이름',
width: 190,
cellRenderer: 'avatar',
cellRendererParams: { image: 'photo', showText: withText },
},
{ id: 'role', title: '역할', width: 110 },
{
id: 'photo',
title: '사진 주소',
width: 130,
formatValue: (v) => (String(v).startsWith('data:') ? '(그림 데이터)' : String(v) || '(없음)'),
},
];
export const options: GridOptions<Row> = {
columns: columns(),
rows,
rowKey: 'id',
saveMode: 'none',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
withText = true;
log({
ko: '사진이 없거나 위험한 주소면 이름의 앞 글자(이니셜)가 동그라미에 들어가요.',
en: 'With no photo or an unsafe address, the first letter of the name (initials) goes in the circle.',
});
}
export const actions = {
text: (api: GridApi<Row>, value?: string) => {
withText = value !== 'photo';
api.setColumns(columns());
say({
ko: withText ? '사진과 이름을 함께 보여요.' : '사진(이니셜)만 보여요.',
en: withText ? 'Showing the photo and the name.' : 'Showing only the photo (initials).',
});
},
give: (api: GridApi<Row>) => {
const row = api.getRowByKey(2);
if (row) api.updateRows([{ ...row, photo: swatch('#f59e0b') }]);
say({ ko: '2번 줄에 사진을 넣었어요.', en: 'Put a photo on row 2.' });
},
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<select
aria-label="표시"
:disabled="!api"
@change="api && actions.text(api, ($event.target as HTMLSelectElement).value)"
>
<option value="both" selected>사진과 이름</option>
<option value="photo">사진만</option>
</select>
<button type="button" :disabled="!api" @click="api && actions.give(api)">2번 줄에 사진 넣기</button>
</div>
<Grid style="height: 280px" :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;
photo: string;
role: string;
}
// 사진 대신 쓸 작은 그림을 캔버스로 만들어요(네트워크 없이 보이게). 어떤 http(s) 주소나 래스터 data:image 주소도 쓸 수 있고,
// SVG data 주소나 그 밖의 방식은 막혀서 이니셜로 보여요.
function swatch(color: string): string {
const canvas = document.createElement('canvas');
canvas.width = canvas.height = 32;
const g = canvas.getContext('2d');
if (g) {
g.fillStyle = color;
g.fillRect(0, 0, 32, 32);
g.fillStyle = 'rgba(255,255,255,0.55)';
g.beginPath();
g.arc(16, 13, 6, 0, Math.PI * 2);
g.fill();
g.fillRect(6, 22, 20, 10);
}
return canvas.toDataURL('image/png');
}
const rows: Row[] = [
{ id: 1, name: '김서연', photo: swatch('#3b82f6'), role: '분석가' },
{ id: 2, name: '이준호', photo: '', role: '관리자' },
{ id: 3, name: '박민지', photo: 'javascript:alert(1)', role: '연구원' },
{ id: 4, name: 'Grace Hopper', photo: swatch('#16a34a'), role: 'Admiral' },
{ id: 5, name: '', photo: '', role: '미배정' },
];
let withText = true;
const columns = (): NonNullable<GridOptions<Row>['columns']> => [
{
id: 'name',
title: '이름',
width: 190,
cellRenderer: 'avatar',
cellRendererParams: { image: 'photo', showText: withText },
},
{ id: 'role', title: '역할', width: 110 },
{
id: 'photo',
title: '사진 주소',
width: 130,
formatValue: (v) => (String(v).startsWith('data:') ? '(그림 데이터)' : String(v) || '(없음)'),
},
];
export const options: GridOptions<Row> = {
columns: columns(),
rows,
rowKey: 'id',
saveMode: 'none',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
withText = true;
log({
ko: '사진이 없거나 위험한 주소면 이름의 앞 글자(이니셜)가 동그라미에 들어가요.',
en: 'With no photo or an unsafe address, the first letter of the name (initials) goes in the circle.',
});
}
export const actions = {
text: (api: GridApi<Row>, value?: string) => {
withText = value !== 'photo';
api.setColumns(columns());
say({
ko: withText ? '사진과 이름을 함께 보여요.' : '사진(이니셜)만 보여요.',
en: withText ? 'Showing the photo and the name.' : 'Showing only the photo (initials).',
});
},
give: (api: GridApi<Row>) => {
const row = api.getRowByKey(2);
if (row) api.updateRows([{ ...row, photo: swatch('#f59e0b') }]);
say({ ko: '2번 줄에 사진을 넣었어요.', en: 'Put a photo on row 2.' });
},
};버튼을 누르면 이 예제의 코드가 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.text(api, $any($event.target).value)"
>
<option value="both" selected>사진과 이름</option>
<option value="photo">사진만</option>
</select>
<button
type="button"
[disabled]="!api"
(click)="api && actions.give(api)"
>2번 줄에 사진 넣기</button>
</div>
<mx-grid style="height: 280px" [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;
photo: string;
role: string;
}
// 사진 대신 쓸 작은 그림을 캔버스로 만들어요(네트워크 없이 보이게). 어떤 http(s) 주소나 래스터 data:image 주소도 쓸 수 있고,
// SVG data 주소나 그 밖의 방식은 막혀서 이니셜로 보여요.
function swatch(color: string): string {
const canvas = document.createElement('canvas');
canvas.width = canvas.height = 32;
const g = canvas.getContext('2d');
if (g) {
g.fillStyle = color;
g.fillRect(0, 0, 32, 32);
g.fillStyle = 'rgba(255,255,255,0.55)';
g.beginPath();
g.arc(16, 13, 6, 0, Math.PI * 2);
g.fill();
g.fillRect(6, 22, 20, 10);
}
return canvas.toDataURL('image/png');
}
const rows: Row[] = [
{ id: 1, name: '김서연', photo: swatch('#3b82f6'), role: '분석가' },
{ id: 2, name: '이준호', photo: '', role: '관리자' },
{ id: 3, name: '박민지', photo: 'javascript:alert(1)', role: '연구원' },
{ id: 4, name: 'Grace Hopper', photo: swatch('#16a34a'), role: 'Admiral' },
{ id: 5, name: '', photo: '', role: '미배정' },
];
let withText = true;
const columns = (): NonNullable<GridOptions<Row>['columns']> => [
{
id: 'name',
title: '이름',
width: 190,
cellRenderer: 'avatar',
cellRendererParams: { image: 'photo', showText: withText },
},
{ id: 'role', title: '역할', width: 110 },
{
id: 'photo',
title: '사진 주소',
width: 130,
formatValue: (v) => (String(v).startsWith('data:') ? '(그림 데이터)' : String(v) || '(없음)'),
},
];
export const options: GridOptions<Row> = {
columns: columns(),
rows,
rowKey: 'id',
saveMode: 'none',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
withText = true;
log({
ko: '사진이 없거나 위험한 주소면 이름의 앞 글자(이니셜)가 동그라미에 들어가요.',
en: 'With no photo or an unsafe address, the first letter of the name (initials) goes in the circle.',
});
}
export const actions = {
text: (api: GridApi<Row>, value?: string) => {
withText = value !== 'photo';
api.setColumns(columns());
say({
ko: withText ? '사진과 이름을 함께 보여요.' : '사진(이니셜)만 보여요.',
en: withText ? 'Showing the photo and the name.' : 'Showing only the photo (initials).',
});
},
give: (api: GridApi<Row>) => {
const row = api.getRowByKey(2);
if (row) api.updateRows([{ ...row, photo: swatch('#f59e0b') }]);
say({ ko: '2번 줄에 사진을 넣었어요.', en: 'Put a photo on row 2.' });
},
};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="Display" id="text">
<option value="both" selected>Photo and name</option>
<option value="photo">Photo only</option>
</select>
<button type="button" id="give">Give row 2 a photo</button>
</div>
<div id="grid" style="height: 280px"></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('text').addEventListener('change', (event) => actions.text(api, event.target.value));
document.getElementById('give').addEventListener('click', () => actions.give(api));// Builds a small picture on a canvas instead of a photo (so it shows without a network). Any http(s) address or raster data:image address works;
// SVG data addresses and other schemes are blocked and fall back to initials.
function swatch(color) {
const canvas = document.createElement('canvas');
canvas.width = canvas.height = 32;
const g = canvas.getContext('2d');
if (g) {
g.fillStyle = color;
g.fillRect(0, 0, 32, 32);
g.fillStyle = 'rgba(255,255,255,0.55)';
g.beginPath();
g.arc(16, 13, 6, 0, Math.PI * 2);
g.fill();
g.fillRect(6, 22, 20, 10);
}
return canvas.toDataURL('image/png');
}
const rows = [
{ id: 1, name: 'Olivia Reed', photo: swatch('#3b82f6'), role: 'Analyst' },
{ id: 2, name: 'Liam Carter', photo: '', role: 'Manager' },
{ id: 3, name: 'Emma Stone', photo: 'javascript:alert(1)', role: 'Researcher' },
{ id: 4, name: 'Grace Hopper', photo: swatch('#16a34a'), role: 'Admiral' },
{ id: 5, name: '', photo: '', role: 'Unassigned' },
];
let withText = true;
const columns = () => [
{
id: 'name',
title: 'Name',
width: 190,
cellRenderer: 'avatar',
cellRendererParams: { image: 'photo', showText: withText },
},
{ id: 'role', title: 'Role', width: 110 },
{
id: 'photo',
title: 'Photo address',
width: 130,
formatValue: (v) => (String(v).startsWith('data:') ? '(image data)' : String(v) || '(none)'),
},
];
export const options = {
columns: columns(),
rows,
rowKey: 'id',
saveMode: 'none',
};
let say = () => { };
export function setup(_api, log) {
say = log;
withText = true;
log('With no photo or an unsafe address, the first letter of the name (initials) goes in the circle.');
}
export const actions = {
text: (api, value) => {
withText = value !== 'photo';
api.setColumns(columns());
say(withText ? 'Showing the photo and the name.' : 'Showing only the photo (initials).');
},
give: (api) => {
const row = api.getRowByKey(2);
if (row)
api.updateRows([{ ...row, photo: swatch('#f59e0b') }]);
say('Put a photo on row 2.');
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Avatars: photo or initials</title>
</head>
<body>
<div class="toolbar">
<select aria-label="Display" id="text">
<option value="both" selected>Photo and name</option>
<option value="photo">Photo only</option>
</select>
<button type="button" id="give">Give row 2 a photo</button>
</div>
<div id="grid" style="height: 280px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
// Builds a small picture on a canvas instead of a photo (so it shows without a network). Any http(s) address or raster data:image address works;
// SVG data addresses and other schemes are blocked and fall back to initials.
function swatch(color) {
const canvas = document.createElement('canvas');
canvas.width = canvas.height = 32;
const g = canvas.getContext('2d');
if (g) {
g.fillStyle = color;
g.fillRect(0, 0, 32, 32);
g.fillStyle = 'rgba(255,255,255,0.55)';
g.beginPath();
g.arc(16, 13, 6, 0, Math.PI * 2);
g.fill();
g.fillRect(6, 22, 20, 10);
}
return canvas.toDataURL('image/png');
}
const rows = [
{ id: 1, name: 'Olivia Reed', photo: swatch('#3b82f6'), role: 'Analyst' },
{ id: 2, name: 'Liam Carter', photo: '', role: 'Manager' },
{ id: 3, name: 'Emma Stone', photo: 'javascript:alert(1)', role: 'Researcher' },
{ id: 4, name: 'Grace Hopper', photo: swatch('#16a34a'), role: 'Admiral' },
{ id: 5, name: '', photo: '', role: 'Unassigned' },
];
let withText = true;
const columns = () => [
{
id: 'name',
title: 'Name',
width: 190,
cellRenderer: 'avatar',
cellRendererParams: { image: 'photo', showText: withText },
},
{ id: 'role', title: 'Role', width: 110 },
{
id: 'photo',
title: 'Photo address',
width: 130,
formatValue: (v) => (String(v).startsWith('data:') ? '(image data)' : String(v) || '(none)'),
},
];
const options = {
columns: columns(),
rows,
rowKey: 'id',
saveMode: 'none',
};
let say = () => { };
function setup(_api, log) {
say = log;
withText = true;
log('With no photo or an unsafe address, the first letter of the name (initials) goes in the circle.');
}
const actions = {
text: (api, value) => {
withText = value !== 'photo';
api.setColumns(columns());
say(withText ? 'Showing the photo and the name.' : 'Showing only the photo (initials).');
},
give: (api) => {
const row = api.getRowByKey(2);
if (row)
api.updateRows([{ ...row, photo: swatch('#f59e0b') }]);
say('Put a photo on row 2.');
},
};
options.ariaLabel = "Avatars: photo or initials";
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('text').addEventListener('change', (event) => actions.text(api, event.target.value));
document.getElementById('give').addEventListener('click', () => actions.give(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">
<select
aria-label="Display"
disabled={!api}
onChange={(event) => api && actions.text(api, event.target.value)}
>
<option value="both">Photo and name</option>
<option value="photo">Photo only</option>
</select>
<button
type="button"
disabled={!api}
onClick={() => api && actions.give(api)}
>Give row 2 a photo</button>
</div>
<Grid style={{ height: 280 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string;
export interface Row {
id: number;
name: string;
photo: string;
role: string;
}
// Builds a small picture on a canvas instead of a photo (so it shows without a network). Any http(s) address or raster data:image address works;
// SVG data addresses and other schemes are blocked and fall back to initials.
function swatch(color: string): string {
const canvas = document.createElement('canvas');
canvas.width = canvas.height = 32;
const g = canvas.getContext('2d');
if (g) {
g.fillStyle = color;
g.fillRect(0, 0, 32, 32);
g.fillStyle = 'rgba(255,255,255,0.55)';
g.beginPath();
g.arc(16, 13, 6, 0, Math.PI * 2);
g.fill();
g.fillRect(6, 22, 20, 10);
}
return canvas.toDataURL('image/png');
}
const rows: Row[] = [
{ id: 1, name: 'Olivia Reed', photo: swatch('#3b82f6'), role: 'Analyst' },
{ id: 2, name: 'Liam Carter', photo: '', role: 'Manager' },
{ id: 3, name: 'Emma Stone', photo: 'javascript:alert(1)', role: 'Researcher' },
{ id: 4, name: 'Grace Hopper', photo: swatch('#16a34a'), role: 'Admiral' },
{ id: 5, name: '', photo: '', role: 'Unassigned' },
];
let withText = true;
const columns = (): NonNullable<GridOptions<Row>['columns']> => [
{
id: 'name',
title: 'Name',
width: 190,
cellRenderer: 'avatar',
cellRendererParams: { image: 'photo', showText: withText },
},
{ id: 'role', title: 'Role', width: 110 },
{
id: 'photo',
title: 'Photo address',
width: 130,
formatValue: (v) => (String(v).startsWith('data:') ? '(image data)' : String(v) || '(none)'),
},
];
export const options: GridOptions<Row> = {
columns: columns(),
rows,
rowKey: 'id',
saveMode: 'none',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
withText = true;
log(
'With no photo or an unsafe address, the first letter of the name (initials) goes in the circle.',
);
}
export const actions = {
text: (api: GridApi<Row>, value?: string) => {
withText = value !== 'photo';
api.setColumns(columns());
say(withText ? 'Showing the photo and the name.' : 'Showing only the photo (initials).');
},
give: (api: GridApi<Row>) => {
const row = api.getRowByKey(2);
if (row) api.updateRows([{ ...row, photo: swatch('#f59e0b') }]);
say('Put a photo on row 2.');
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<select
aria-label="Display"
:disabled="!api"
@change="api && actions.text(api, ($event.target as HTMLSelectElement).value)"
>
<option value="both" selected>Photo and name</option>
<option value="photo">Photo only</option>
</select>
<button type="button" :disabled="!api" @click="api && actions.give(api)">Give row 2 a photo</button>
</div>
<Grid style="height: 280px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string;
export interface Row {
id: number;
name: string;
photo: string;
role: string;
}
// Builds a small picture on a canvas instead of a photo (so it shows without a network). Any http(s) address or raster data:image address works;
// SVG data addresses and other schemes are blocked and fall back to initials.
function swatch(color: string): string {
const canvas = document.createElement('canvas');
canvas.width = canvas.height = 32;
const g = canvas.getContext('2d');
if (g) {
g.fillStyle = color;
g.fillRect(0, 0, 32, 32);
g.fillStyle = 'rgba(255,255,255,0.55)';
g.beginPath();
g.arc(16, 13, 6, 0, Math.PI * 2);
g.fill();
g.fillRect(6, 22, 20, 10);
}
return canvas.toDataURL('image/png');
}
const rows: Row[] = [
{ id: 1, name: 'Olivia Reed', photo: swatch('#3b82f6'), role: 'Analyst' },
{ id: 2, name: 'Liam Carter', photo: '', role: 'Manager' },
{ id: 3, name: 'Emma Stone', photo: 'javascript:alert(1)', role: 'Researcher' },
{ id: 4, name: 'Grace Hopper', photo: swatch('#16a34a'), role: 'Admiral' },
{ id: 5, name: '', photo: '', role: 'Unassigned' },
];
let withText = true;
const columns = (): NonNullable<GridOptions<Row>['columns']> => [
{
id: 'name',
title: 'Name',
width: 190,
cellRenderer: 'avatar',
cellRendererParams: { image: 'photo', showText: withText },
},
{ id: 'role', title: 'Role', width: 110 },
{
id: 'photo',
title: 'Photo address',
width: 130,
formatValue: (v) => (String(v).startsWith('data:') ? '(image data)' : String(v) || '(none)'),
},
];
export const options: GridOptions<Row> = {
columns: columns(),
rows,
rowKey: 'id',
saveMode: 'none',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
withText = true;
log(
'With no photo or an unsafe address, the first letter of the name (initials) goes in the circle.',
);
}
export const actions = {
text: (api: GridApi<Row>, value?: string) => {
withText = value !== 'photo';
api.setColumns(columns());
say(withText ? 'Showing the photo and the name.' : 'Showing only the photo (initials).');
},
give: (api: GridApi<Row>) => {
const row = api.getRowByKey(2);
if (row) api.updateRows([{ ...row, photo: swatch('#f59e0b') }]);
say('Put a photo on row 2.');
},
};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="Display"
[disabled]="!api"
(change)="api && actions.text(api, $any($event.target).value)"
>
<option value="both" selected>Photo and name</option>
<option value="photo">Photo only</option>
</select>
<button
type="button"
[disabled]="!api"
(click)="api && actions.give(api)"
>Give row 2 a photo</button>
</div>
<mx-grid style="height: 280px" [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;
export interface Row {
id: number;
name: string;
photo: string;
role: string;
}
// Builds a small picture on a canvas instead of a photo (so it shows without a network). Any http(s) address or raster data:image address works;
// SVG data addresses and other schemes are blocked and fall back to initials.
function swatch(color: string): string {
const canvas = document.createElement('canvas');
canvas.width = canvas.height = 32;
const g = canvas.getContext('2d');
if (g) {
g.fillStyle = color;
g.fillRect(0, 0, 32, 32);
g.fillStyle = 'rgba(255,255,255,0.55)';
g.beginPath();
g.arc(16, 13, 6, 0, Math.PI * 2);
g.fill();
g.fillRect(6, 22, 20, 10);
}
return canvas.toDataURL('image/png');
}
const rows: Row[] = [
{ id: 1, name: 'Olivia Reed', photo: swatch('#3b82f6'), role: 'Analyst' },
{ id: 2, name: 'Liam Carter', photo: '', role: 'Manager' },
{ id: 3, name: 'Emma Stone', photo: 'javascript:alert(1)', role: 'Researcher' },
{ id: 4, name: 'Grace Hopper', photo: swatch('#16a34a'), role: 'Admiral' },
{ id: 5, name: '', photo: '', role: 'Unassigned' },
];
let withText = true;
const columns = (): NonNullable<GridOptions<Row>['columns']> => [
{
id: 'name',
title: 'Name',
width: 190,
cellRenderer: 'avatar',
cellRendererParams: { image: 'photo', showText: withText },
},
{ id: 'role', title: 'Role', width: 110 },
{
id: 'photo',
title: 'Photo address',
width: 130,
formatValue: (v) => (String(v).startsWith('data:') ? '(image data)' : String(v) || '(none)'),
},
];
export const options: GridOptions<Row> = {
columns: columns(),
rows,
rowKey: 'id',
saveMode: 'none',
};
let say: (message: LogText) => void = () => {};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
say = log;
withText = true;
log(
'With no photo or an unsafe address, the first letter of the name (initials) goes in the circle.',
);
}
export const actions = {
text: (api: GridApi<Row>, value?: string) => {
withText = value !== 'photo';
api.setColumns(columns());
say(withText ? 'Showing the photo and the name.' : 'Showing only the photo (initials).');
},
give: (api: GridApi<Row>) => {
const row = api.getRowByKey(2);
if (row) api.updateRows([{ ...row, photo: swatch('#f59e0b') }]);
say('Put a photo on row 2.');
},
};