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

말풍선 내용을 내 마음대로 만들기Build the tooltip text yourself

셀의 값뿐 아니라 같은 줄의 다른 값, 몇 번째 줄인지, 계산한 금액까지 모아서 여러 줄짜리 말풍선을 만들 수 있어요. 내용은 코드로 정해요.Combine the cell value, other values of the same row, the row number and even a computed amount into a multi-line tooltip. The text is decided in code.

활용Intermediate
직접 써 보기Live preview말풍선 내용을 내 마음대로 만들기Build the tooltip text yourself

코드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="style">
      <option value="detail" selected>자세히</option>
      <option value="simple">간단히</option>
    </select>
  </div>
  <div id="grid" style="height: 300px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options } from './example';
const api = createGrid(document.getElementById('grid'), options);
document.getElementById('style').addEventListener('change', (event) => actions.style(api, event.target.value));
example.js
const customers = ['김서연', '이준호', '박민지', '최도윤', '정하은', '강지후'];
const products = ['무선 이어폰', '노트북 14형', '스마트워치', '태블릿 11형', '보조배터리'];
const colors = ['블랙', '화이트', '실버', '네이비', '핑크'];
const memos = [
  '오전 중 배송 요청. 경비실에 맡겨 주세요.',
  '선물 포장 필요. 카드에 "생일 축하해요" 문구를 넣어 주세요.',
  '',
  '회사 주소로 발송. 세금계산서는 다음 달 초에 발행해 주세요.',
  '교환 이력 있음. 동일 색상으로 다시 보내기.',
  '부재 시 문 앞에 두고 문자 남겨 주세요.',
];
const rows = Array.from({ length: 6 }, (_, i) => ({
  id: i + 1,
  customer: customers[i],
  product: products[(i * 2) % products.length],
  color: colors[(i * 3) % colors.length],
  price: 59000 + ((i * 7) % 9) * 30000,
  quantity: 1 + ((i * 5) % 6),
  memo: memos[i],
}));
const won = (value) => `${Number(value).toLocaleString('ko-KR')}원`;
let style = 'detail';
// tooltip에 함수를 주면 줄마다 말풍선 내용을 만들어요. 함수는 그 줄(row)과 줄 번호(rowIndex)를 받아요.
// 같은 줄의 다른 값, 몇 번째 줄인지, 계산한 값을 모아 여러 줄(\n) 글을 만들 수 있어요. 글자만 들어가서 HTML 태그도 그대로 글자로 보여요.
const tipOf = (header, valueOf) => (row, rowIndex) => style === 'simple'
  ? `${header}: ${valueOf(row)}`
  : [
    `${row.customer} 님의 주문`,
    `${rowIndex + 1}번째 줄 · ${header}`,
    `값: ${valueOf(row)}`,
    `상품: ${row.product} (${row.color})`,
    `금액: ${won(row.price * row.quantity)}`,
  ].join('\n');
const columns = () => [
  { id: 'customer', title: '고객', width: 100, tooltip: tipOf('고객', (row) => row.customer) },
  { id: 'product', title: '상품', width: 140, tooltip: tipOf('상품', (row) => row.product) },
  {
    id: 'price',
    title: '가격',
    kind: 'number',
    width: 110,
    align: 'end',
    formatValue: won,
    tooltip: tipOf('가격', (row) => won(row.price)),
  },
  {
    id: 'quantity',
    title: '수량',
    kind: 'number',
    width: 80,
    align: 'end',
    tooltip: tipOf('수량', (row) => String(row.quantity)),
  },
];
export const options = {
  columns: columns(),
  rows,
  rowKey: 'id',
};
export const actions = {
  style: (api, value) => {
    style = value === 'simple' ? 'simple' : 'detail';
    api.setColumns(columns());
  },
};

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="Tooltip content" id="style">
      <option value="detail" selected>Detailed</option>
      <option value="simple">Simple</option>
    </select>
  </div>
  <div id="grid" style="height: 300px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options } from './example';
const api = createGrid(document.getElementById('grid'), options);
document.getElementById('style').addEventListener('change', (event) => actions.style(api, event.target.value));
example.js
const customers = [
  'Emma Stone',
  'Liam Carter',
  'Mia Foster',
  'Noah Brooks',
  'Ava Morgan',
  'Lucas Bell',
];
const products = ['Wireless Earbuds', 'Laptop 14"', 'Smartwatch', 'Tablet 11"', 'Power Bank'];
const colors = ['Black', 'White', 'Silver', 'Navy', 'Pink'];
const memos = [
  'Please deliver in the morning and leave it with the front desk.',
  'Gift wrapping needed. Put "Happy birthday" on the card.',
  '',
  'Ship to the office address. Please issue the tax invoice early next month.',
  'Has an exchange history. Send again in the same color.',
  'If nobody is home, leave it at the door and send a text.',
];
const rows = Array.from({ length: 6 }, (_, i) => ({
  id: i + 1,
  customer: customers[i],
  product: products[(i * 2) % products.length],
  color: colors[(i * 3) % colors.length],
  price: 59 + ((i * 7) % 9) * 30,
  quantity: 1 + ((i * 5) % 6),
  memo: memos[i],
}));
const usd = (value) => `$${Number(value).toLocaleString('en-US')}`;
let style = 'detail';
// Pass a function to tooltip to build the tooltip content per row. The function receives the row and its index (rowIndex).
// You can combine other values of the same row, the row position and calculated values into multi-line (\n) text. Only text goes in, so HTML tags are shown as plain text.
const tipOf = (header, valueOf) => (row, rowIndex) => style === 'simple'
  ? `${header}: ${valueOf(row)}`
  : [
    `${row.customer}'s order`,
    `Row ${rowIndex + 1} · ${header}`,
    `Value: ${valueOf(row)}`,
    `Product: ${row.product} (${row.color})`,
    `Amount: ${usd(row.price * row.quantity)}`,
  ].join('\n');
const columns = () => [
  {
    id: 'customer',
    title: 'Customer',
    width: 100,
    tooltip: tipOf('Customer', (row) => row.customer),
  },
  { id: 'product', title: 'Product', width: 140, tooltip: tipOf('Product', (row) => row.product) },
  {
    id: 'price',
    title: 'Price',
    kind: 'number',
    width: 110,
    align: 'end',
    formatValue: usd,
    tooltip: tipOf('Price', (row) => usd(row.price)),
  },
  {
    id: 'quantity',
    title: 'Qty',
    kind: 'number',
    width: 80,
    align: 'end',
    tooltip: tipOf('Qty', (row) => String(row.quantity)),
  },
];
export const options = {
  columns: columns(),
  rows,
  rowKey: 'id',
};
export const actions = {
  style: (api, value) => {
    style = value === 'simple' ? 'simple' : 'detail';
    api.setColumns(columns());
  },
};