조건부 행 스타일링 (Row Styling)

특정 조건(예: 결제 취소, 재고 부족, VIP 회원 등)을 만족하는 행에 커스텀 CSS 클래스를 동적으로 부여하는 방법을 학습합니다.

#getRowClassName#conditional-styling#row-color#highlight#css-classes
검토일: 2026-08-17
GitHub
import * as React from 'react';
import { AXDataGrid, AXDGColumn } from '@axboot/datagrid';
import DataGridContainer from '../components/DataGridContainer';
import { useContainerSize } from '../hooks/useContainerSize';

interface Props {}

interface IListItem {
  no: number;
  id: string;
  title: string;
  writer: string;
  createAt: string;
}

const list = Array.from(Array(1000)).map((v, i) => ({
  values: {
    no: i,
    id: `ID_${i}`,
    title: `title_${i}`,
    writer: `writer_${i}`,
    createAt: `2022-09-08`,
  },
}));

export default function GetRowClassName(props: Props) {
  const [selectedRowKey, setSelectedRowKey] = React.useState<number>();
  const [columns, setColumns] = React.useState<AXDGColumn<IListItem>[]>([
    {
      key: 'id',
      label: '아이디 IS LONG !',
      width: 100,
    },
    {
      key: 'title',
      label: '제목',
      width: 300,
      itemRender: ({ values }) => {
        return (
          <>
            {values.writer} / {values.title}
          </>
        );
      },
    },
    {
      key: 'writer',
      label: '작성자',
      width: 100,
      itemRender: ({ values: values }) => {
        return <>{values.writer} / A</>;
      },
    },
    {
      key: 'createAt',
      label: '작성일',
      width: 100,
      sortDisable: true,
    },
  ]);
  const containerRef = React.useRef<HTMLDivElement>(null);
  const { width: containerWidth, height: containerHeight } = useContainerSize(containerRef);

  return (
    <>
      <DataGridContainer ref={containerRef} className={'get-row-class-example'}>
        <AXDataGrid<IListItem>
          showLineNumber
          width={containerWidth}
          height={containerHeight}
          headerHeight={35}
          data={list}
          columns={columns}
          onChangeColumns={(columnIndex, { width, columns }) => {
            console.log('onChangeColumnWidths', columnIndex, width, columns);
            setColumns(columns);
          }}
          onClick={({ item }) => {
            // console.log('item.id', item.id);
            setSelectedRowKey(item.no);
          }}
          rowKey={'no'}
          selectedRowKey={selectedRowKey}
          getRowClassName={(ri, item) => {
            if (item.values.no < 3) {
              return 'notice-tr';
            }
          }}
        />
      </DataGridContainer>
    </>
  );
}

1. 언제 사용하며 왜 필요한가요?

경고 상태(에러 발생 로그, 위험 재고 알림)나 중요 상태(VIP 고객, 완료된 작업)의 행 전체에 빨간색/노란색/초록색 배경 하이라이트를 적용하여 사용자의 시선을 집중시켜야 할 때 getRowClassName을 사용합니다.


2. 실무 완성형 예제: 재고 부족 품목 경고 하이라이트

import React, { useState } from 'react';
import { AXDataGrid, type AXDGColumn, type AXDGDataItem } from '@axboot/datagrid';

interface InventoryItem {
  id: string;
  name: string;
  stock: number;
  threshold: number;
}

export default function RowStylingGrid() {
  const [data] = useState<AXDGDataItem<InventoryItem>[]>([
    { values: { id: 'P1', name: '고속 충전 어댑터 65W', stock: 4, threshold: 10 } }, // 재고 위험
    { values: { id: 'P2', name: '무선 블루투스 이어폰', stock: 28, threshold: 10 } }, // 정상
    { values: { id: 'P3', name: '스마트폰 강화유리 필름', stock: 0, threshold: 5 } }, // 품절
    { values: { id: 'P4', name: '태블릿 마그네틱 거치대', stock: 15, threshold: 5 } }, // 정상
  ]);

  const columns: AXDGColumn<InventoryItem>[] = [
    { key: 'id', label: '코드', width: 80, align: 'center' },
    { key: 'name', label: '품목명', width: 240 },
    {
      key: 'stock',
      label: '현재 재고',
      width: 100,
      align: 'right',
      itemRender: ({ values }) => <strong>{values.stock}개</strong>,
    },
    { key: 'threshold', label: '안전 재고', width: 100, align: 'right', itemRender: ({ values }) => `${values.threshold}개` },
  ];

  return (
    <div>
      <style>{`
        .row-out-of-stock {
          background-color: #fee2e2 !important; /* 연한 빨강 */
          color: #991b1b;
        }
        .row-low-stock {
          background-color: #fef3c7 !important; /* 연한 노랑 */
          color: #92400e;
        }
      `}</style>

      <AXDataGrid<InventoryItem>
        width={560}
        height={220}
        columns={columns}
        data={data}
        rowKey="id"
        // 조건부 행 클래스 반환
        getRowClassName={(_, item) => {
          if (item.values.stock === 0) return 'row-out-of-stock';
          if (item.values.stock < item.values.threshold) return 'row-low-stock';
          return '';
        }}
      />
    </div>
  );
}