Skip to content

Custom Cell

  • Customize body cell content via StkTableColumn['customCell'].
  • Customize header cell content via StkTableColumn['customHeaderCell'].

customCell and customHeaderCell are used similarly. Here we'll use customCell as an example.

Recommendations

  • It's recommended to wrap customCell with an element (div, span, etc.), otherwise having TextNode as the child of <td> may cause layout issues.
  • Be cautious when setting root elements of customCell as inline elements (inline, inline-block, inline-flex, etc.), as this layout may stretch row heights in virtual lists.

Using with a React Component

Supports passing React components. The props of the component need to be specially defined with the CustomCellProps type.

Best Practice

Define columns in a separate file and export them.

ts
import { StkTableColumn } from 'stk-table-solid/src/StkTable/index';
import type { DataType } from './types';
import YieldCell from './YieldCell';
export const columns: StkTableColumn<DataType> = [{
    title: '收益率',
    dataIndex: 'yield',
    customCell: YieldCell
}]
tsx
import type { CustomCellProps } from 'stk-table-solid/src/StkTable/index';
import type { DataType } from './types';

export default function YieldCell(props: CustomCellProps<DataType>) {
    let className = '';
    if (props.cellValue > 0) {
        className = 'color-up';
    } else if (props.cellValue < 0) {
        className = 'color-down';
    }
    return (
        <span class={className}>
            {props.cellValue > 0 ? '+' : ''}
            {(props.cellValue * 100).toFixed(4)}%
        </span>
    );
}
ts
export type DataType = {
    name: string;
    yield: number;
};
css
.color-up {
    color: #2fc87b;
}
.color-down {
    color: #ff2b48;
}
loading

Using with JSX

customCell can also return JSX directly, which is convenient for simple modifications.

For example, we can multiply the value by 100 and add a unit.

tsx
import { StkTableColumn } from 'stk-table-solid/src/StkTable/index';

const columns: StkTableColumn<any>[] = [
    {
        title: '收益率',
        dataIndex: 'yield',
        customCell: ({ cellValue }) => <span>{cellValue * 100}%</span>,
    },
]

You can also style based on the cell value:

tsx
import { StkTableColumn } from 'stk-table-solid/src/StkTable/index';

const columns: StkTableColumn<any>[] = [
    {
        title: '姓名',
        dataIndex: 'name',
        customCell: ({ row, col, cellValue }) => {
            return <span style={{ color: 'red' }}>{cellValue}</span>;
        },
    },
]

API

PropertypropsDefaultDescription
customCellComponentType<CustomCellProps>-Custom cell rendering component
customHeaderCellComponentType<CustomHeaderCellProps>-Custom header cell rendering component

types

customCell props type

ts
export type CustomCellProps<T extends Record<string, any>> = {
    row: T;
    col: StkTableColumn<T>;
    /** row[col.dataIndex] 的值 */
    cellValue: any;
    rowIndex: number;
    /** 
     * 列索引(从0开始)。
     * 
     * 注意:
     * 在virtual-x 下,否则表示虚拟列表中的列索引
     */
    colIndex: number;
    /**
     * 当前行是否展开
     * - 不展开: null
     * - 展开: 返回column配置
     */
    expanded?: StkTableColumn<any>;
    /** 树节点当前行是否展开 */
    treeExpanded?: boolean;
};

export type CustomHeaderCellProps<T extends Record<string, any>> = {
    col: StkTableColumn<T>;
    rowIndex: number;
    /** 
     * 列索引(从0开始)。
     * 
     * 注意:
     * 在virtual-x 下,否则表示虚拟列表中的列索引
     */
    colIndex: number;
};

Released under the MIT License