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

링크 칸: 홈페이지·이메일·전화·티켓Link cells: website, e-mail, phone, ticket

주소 값을 누를 수 있는 링크로 그려요. 이메일은 메일 쓰기로, 전화는 전화 걸기로, 티켓 번호는 baseUrl로 추적 사이트 주소를 만들고 onClick으로 클릭을 먼저 받아요. 안전하지 않은 주소는 링크가 되지 않아요.Draw address values as clickable links: e-mail opens a new message, phone dials, ticket numbers build a tracker address from baseUrl and onClick sees the click first. Unsafe addresses are not turned into links.

입문Beginner
직접 써 보기Live preview링크 칸: 홈페이지·이메일·전화·티켓Link cells: website, e-mail, phone, ticket

코드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="open">
      <option value="new" selected>새 창</option>
      <option value="same">지금 창</option>
    </select>
    <button type="button" id="unsafe">위험한 주소 넣기</button>
    <button type="button" id="restore">원래 주소로</button>
  </div>
  <div id="grid" style="height: 260px"></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('open').addEventListener('change', (event) => actions.open(api, event.target.value));
document.getElementById('unsafe').addEventListener('click', () => actions.unsafe(api));
document.getElementById('restore').addEventListener('click', () => actions.restore(api));
example.js
// 손가락으로 쓰는 화면(폰)인지 알려 줘요. 이런 화면에서는 누를 곳을 40px 이상으로 키워요.
const coarse = () => window.matchMedia('(pointer: coarse), (max-width: 480px)').matches;
// 이 예제에서만 쓰는 꾸밈 규칙을 화면에 넣고, 정리할 때 함께 지워요.
function addStyle(css) {
  const el = document.createElement('style');
  el.textContent = css;
  document.head.append(el);
  return () => el.remove();
}
// cellRenderer: 'link'는 값을 누를 수 있는 링크로 그려요. http, https, mailto, tel 주소만 링크가 되고,
// 그 밖의 값(javascript: 같은 것)은 링크 없이 글자로만 보여요.
const rows = [
  {
    id: 1,
    company: '한빛푸드',
    site: 'https://hanbit.example.com',
    email: 'sales@hanbit.example.com',
    phone: '+82-2-555-0101',
    ticket: 'BUG-101',
  },
  {
    id: 2,
    company: '파란마켓',
    site: 'https://blue.example.com',
    email: 'hello@blue.example.com',
    phone: '+82-51-555-0142',
    ticket: 'BUG-102',
  },
  {
    id: 3,
    company: '단풍상회',
    site: 'http://maple.example.com',
    email: 'orders@maple.example.com',
    phone: '+82-62-555-0177',
    ticket: 'FEAT-7',
  },
  {
    id: 4,
    company: '주소 없는 곳',
    site: '',
    email: 'info@nosite.example.com',
    phone: '+82-42-555-0100',
    ticket: 'OPS-3',
  },
];
let newWindow = true;
const columns = () => [
  { id: 'company', title: '업체', width: 80 },
  {
    id: 'site',
    title: '홈페이지',
    width: 150,
    cellRenderer: 'link',
    cellRendererParams: { newWindow },
  },
  {
    id: 'email',
    title: '이메일',
    width: 170,
    cellRenderer: 'link',
    cellRendererParams: { href: 'mailto:{value}' },
  },
  {
    id: 'phone',
    title: '전화',
    width: 125,
    cellRenderer: 'link',
    cellRendererParams: { href: 'tel:{value}' },
  },
  {
    id: 'ticket',
    title: '티켓',
    width: 80,
    cellRenderer: 'link',
    // baseUrl 뒤에 칸의 글자를 붙여요. onClick은 이동 전에 불리고, preventDefault()로 이동을 멈춰요.
    cellRendererParams: {
      baseUrl: 'https://tracker.example.com/issues/',
      onClick: ({ text, href, event }) => {
        event.preventDefault();
        say({
          ko: `티켓 ${text}를 눌렀어요. 이동은 멈췄어요(${href}).`,
          en: `Clicked ticket ${text}. Navigation was stopped (${href}).`,
        });
      },
    },
  },
];
export const options = {
  columns: columns(),
  rows,
  rowKey: 'id',
  saveMode: 'none',
  ...(coarse() ? { rowHeight: 44 } : {}),
};
let say = () => { };
let removeStyle = () => undefined;
export function setup(_api, log) {
  say = log;
  newWindow = true;
  // 폰에서는 링크를 누르기 쉽게 40px 높이로 키워요.
  removeStyle = addStyle('a { display: inline-flex; align-items: center; min-height: 24px; } @media (pointer: coarse), (max-width: 480px) { .mxg a { min-height: 40px; } }');
  log({
    ko: '링크는 눌러도 되고, 칸을 고른 뒤 Enter로 칸 안에 들어가 Enter로 열 수도 있어요(Esc로 나와요). 예제 주소라서 실제 페이지는 없어요.',
    en: 'You can click a link, or select a cell, press Enter to go into it and Enter again to open it (Esc to leave). The addresses are examples, so there are no real pages.',
  });
  return removeStyle;
}
export const actions = {
  open: (api, value) => {
    newWindow = value !== 'same';
    api.setColumns(columns());
    say({
      ko: newWindow ? '홈페이지 링크는 새 창에서 열려요.' : '홈페이지 링크는 지금 창에서 열려요.',
      en: newWindow
        ? 'Homepage links open in a new window.'
        : 'Homepage links open in this window.',
    });
  },
  unsafe: (api) => {
    const row = api.getRowByKey(1);
    if (row)
      api.updateRows([{ ...row, site: 'javascript:alert(1)' }]);
    say({
      ko: '1번 줄 홈페이지를 위험한 주소로 바꿨어요. 링크가 아니라 글자로만 보여요.',
      en: 'Changed the homepage of row 1 to an unsafe address. It shows as text, not as a link.',
    });
  },
  restore: (api) => {
    const row = api.getRowByKey(1);
    if (row)
      api.updateRows([{ ...row, site: 'https://hanbit.example.com' }]);
    say({
      ko: '1번 줄 홈페이지를 원래 주소로 되돌렸어요.',
      en: 'Changed the homepage of row 1 back to the original address.',
    });
  },
};

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="Opens in" id="open">
      <option value="new" selected>New window</option>
      <option value="same">Same window</option>
    </select>
    <button type="button" id="unsafe">Put an unsafe address</button>
    <button type="button" id="restore">Restore address</button>
  </div>
  <div id="grid" style="height: 260px"></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('open').addEventListener('change', (event) => actions.open(api, event.target.value));
document.getElementById('unsafe').addEventListener('click', () => actions.unsafe(api));
document.getElementById('restore').addEventListener('click', () => actions.restore(api));
example.js
// Tells whether this is a touch screen (phone). On such screens the tap target grows to at least 40px.
const coarse = () => window.matchMedia('(pointer: coarse), (max-width: 480px)').matches;
// Adds a style rule used only by this example and removes it on cleanup.
function addStyle(css) {
  const el = document.createElement('style');
  el.textContent = css;
  document.head.append(el);
  return () => el.remove();
}
// cellRenderer: 'link' draws the value as a clickable link. Only http, https, mailto and tel addresses become links;
// any other value (such as javascript:) is shown as plain text without a link.
const rows = [
  {
    id: 1,
    company: 'Harbor Foods',
    site: 'https://hanbit.example.com',
    email: 'sales@hanbit.example.com',
    phone: '+1-212-555-0101',
    ticket: 'BUG-101',
  },
  {
    id: 2,
    company: 'Blue Market',
    site: 'https://blue.example.com',
    email: 'hello@blue.example.com',
    phone: '+1-415-555-0142',
    ticket: 'BUG-102',
  },
  {
    id: 3,
    company: 'Maple Trading',
    site: 'http://maple.example.com',
    email: 'orders@maple.example.com',
    phone: '+1-312-555-0177',
    ticket: 'FEAT-7',
  },
  {
    id: 4,
    company: 'No website',
    site: '',
    email: 'info@nosite.example.com',
    phone: '+1-617-555-0100',
    ticket: 'OPS-3',
  },
];
let newWindow = true;
const columns = () => [
  { id: 'company', title: 'Company', width: 110 },
  {
    id: 'site',
    title: 'Website',
    width: 150,
    cellRenderer: 'link',
    cellRendererParams: { newWindow },
  },
  {
    id: 'email',
    title: 'Email',
    width: 170,
    cellRenderer: 'link',
    cellRendererParams: { href: 'mailto:{value}' },
  },
  {
    id: 'phone',
    title: 'Phone',
    width: 125,
    cellRenderer: 'link',
    cellRendererParams: { href: 'tel:{value}' },
  },
  {
    id: 'ticket',
    title: 'Ticket',
    width: 80,
    cellRenderer: 'link',
    // The cell text is appended after baseUrl. onClick is called before navigating; preventDefault() stops the navigation.
    cellRendererParams: {
      baseUrl: 'https://tracker.example.com/issues/',
      onClick: ({ text, href, event }) => {
        event.preventDefault();
        say(`Clicked ticket ${text}. Navigation was stopped (${href}).`);
      },
    },
  },
];
export const options = {
  columns: columns(),
  rows,
  rowKey: 'id',
  saveMode: 'none',
  ...(coarse() ? { rowHeight: 44 } : {}),
};
let say = () => { };
let removeStyle = () => undefined;
export function setup(_api, log) {
  say = log;
  newWindow = true;
  // On a phone, links grow to 40px tall so they are easy to tap.
  removeStyle = addStyle('a { display: inline-flex; align-items: center; min-height: 24px; } @media (pointer: coarse), (max-width: 480px) { .mxg a { min-height: 40px; } }');
  log('You can click a link, or select a cell, press Enter to go into it and Enter again to open it (Esc to leave). The addresses are examples, so there are no real pages.');
  return removeStyle;
}
export const actions = {
  open: (api, value) => {
    newWindow = value !== 'same';
    api.setColumns(columns());
    say(newWindow ? 'Homepage links open in a new window.' : 'Homepage links open in this window.');
  },
  unsafe: (api) => {
    const row = api.getRowByKey(1);
    if (row)
      api.updateRows([{ ...row, site: 'javascript:alert(1)' }]);
    say('Changed the homepage of row 1 to an unsafe address. It shows as text, not as a link.');
  },
  restore: (api) => {
    const row = api.getRowByKey(1);
    if (row)
      api.updateRows([{ ...row, site: 'https://hanbit.example.com' }]);
    say('Changed the homepage of row 1 back to the original address.');
  },
};