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

칸 하나에 스타일 얹기Style a single cell

setCellStyle(rowKey, columnId, { className, style })는 칸 하나에 클래스와 스타일을 얹고 null로 지워요. 행 키와 컬럼 id로 보관해서 정렬·필터·쪽 나누기를 거쳐도 같은 칸을 따라가요.setCellStyle(rowKey, columnId, { className, style }) layers a class and styles on one cell, and null removes it. It is kept by row key and column id, so it follows the cell through sort, filter and paging.

입문Beginner
직접 써 보기Live preview칸 하나에 스타일 얹기Style a single cell

코드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">
    <button type="button" id="mark">스타일 넣기</button>
    <button type="button" id="unmark">스타일 지우기</button>
    <button type="button" id="sort">점수로 정렬</button>
    <button type="button" id="clear">모두 지우기</button>
  </div>
  <div id="grid" style="height: 380px"></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('mark').addEventListener('click', () => actions.mark(api));
document.getElementById('unmark').addEventListener('click', () => actions.unmark(api));
document.getElementById('sort').addEventListener('click', () => actions.sort(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));
example.js
const en = document.documentElement.dataset.lang === 'en';
const names = en
  ? ['Alex', 'Blair', 'Casey', 'Drew', 'Emery', 'Finn', 'Gray', 'Harper']
  : ['김서연', '이준호', '박민지', '최도윤', '정하은', '한지우', '오세훈', '윤아름'];
const teams = en ? ['Dev', 'Design', 'Ops'] : ['개발', '디자인', '운영'];
export const options = {
  columns: [
    { id: 'name', title: en ? 'Name' : '이름', width: 120, sortable: true },
    { id: 'team', title: en ? 'Team' : '팀', width: 100, sortable: true },
    { id: 'score', title: en ? 'Score' : '점수', kind: 'number', width: 90, sortable: true },
  ],
  rows: names.map((name, i) => ({
    id: i + 1,
    name,
    team: teams[i % 3],
    score: 55 + ((i * 11) % 45),
  })),
  rowKey: 'id',
};
let say = () => { };
export function setup(_api, log) {
  say = log;
}
const MARK = { style: { backgroundColor: '#fff3bf', color: '#1f2933', fontWeight: 700 } };
export const actions = {
  // 3번 행의 점수 칸 하나에만 스타일을 얹어요. 정렬해도 같은 행의 칸을 따라가요.
  mark: (api) => {
    api.setCellStyle(3, 'score', MARK);
    say({ ko: '3번 행 점수 칸에 스타일을 얹었어요', en: 'Styled the score cell of row 3' });
  },
  unmark: (api) => {
    api.setCellStyle(3, 'score', null);
    say({ ko: '스타일을 지웠어요', en: 'Removed the style' });
  },
  sort: (api) => api.setSortModel([{ columnId: 'score', direction: 'desc' }]),
  clear: (api) => {
    api.clearCellStyles();
    say({ ko: '모든 칸 스타일을 지웠어요', en: 'Cleared every cell style' });
  },
};

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">
    <button type="button" id="mark">Add style</button>
    <button type="button" id="unmark">Remove style</button>
    <button type="button" id="sort">Sort by score</button>
    <button type="button" id="clear">Clear all</button>
  </div>
  <div id="grid" style="height: 380px"></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('mark').addEventListener('click', () => actions.mark(api));
document.getElementById('unmark').addEventListener('click', () => actions.unmark(api));
document.getElementById('sort').addEventListener('click', () => actions.sort(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));
example.js
const names = ['Alex', 'Blair', 'Casey', 'Drew', 'Emery', 'Finn', 'Gray', 'Harper'];
const teams = ['Dev', 'Design', 'Ops'];
export const options = {
  columns: [
    { id: 'name', title: 'Name', width: 120, sortable: true },
    { id: 'team', title: 'Team', width: 100, sortable: true },
    { id: 'score', title: 'Score', kind: 'number', width: 90, sortable: true },
  ],
  rows: names.map((name, i) => ({
    id: i + 1,
    name,
    team: teams[i % 3],
    score: 55 + ((i * 11) % 45),
  })),
  rowKey: 'id',
};
let say = () => { };
export function setup(_api, log) {
  say = log;
}
const MARK = { style: { backgroundColor: '#fff3bf', color: '#1f2933', fontWeight: 700 } };
export const actions = {
  // Style only the score cell of row 3. It follows the same row's cell when you sort.
  mark: (api) => {
    api.setCellStyle(3, 'score', MARK);
    say('Styled the score cell of row 3');
  },
  unmark: (api) => {
    api.setCellStyle(3, 'score', null);
    say('Removed the style');
  },
  sort: (api) => api.setSortModel([{ columnId: 'score', direction: 'desc' }]),
  clear: (api) => {
    api.clearCellStyles();
    say('Cleared every cell style');
  },
};