본문으로 건너뛰기Skip to content
예제 첫화면Examples home

아바타: 사진이나 이니셜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.

입문Beginner
직접 써 보기Live preview아바타: 사진이나 이니셜Avatars: photo or initials

코드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)

빌드 도구(Vite 등)를 쓰는 프로젝트용 모듈 파일이에요. 설치 없이 바로 돌려 보려면 HTML 탭이나 직접 해보기를 쓰세요.

버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.

index.html
<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>
main.js
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));
example.js
// 사진 대신 쓸 작은 그림을 캔버스로 만들어요(네트워크 없이 보이게). 어떤 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.' });
  },
};

Module files for a project with a bundler such as Vite. To run it with no setup, use the HTML tab or Try it.

When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.

index.html
<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>
main.js
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));
example.js
// 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.');
  },
};