简体   繁体   English

ag-Grid 中的 cellRenderer 函数可以返回 React 组件吗?

[英]Can a cellRenderer function in ag-Grid return a React component?

I would like to use a cellRenderer in React for most of my columns.我想在 React 中为我的大多数专栏使用 cellRenderer。 So, in my colDefs, I have an additional field called unit.因此,在我的 colDefs 中,我有一个名为 unit 的附加字段。 If the unit exists, I am trying to have a heatMap like color grid which is handled in my TableCell React component.如果该单元存在,我正在尝试在我的 TableCell React 组件中处理一个类似颜色网格的热图。 This identical react component has worked in other data grids like ZippyUI.这个相同的反应组件已经在其他数据网格中工作,比如 ZippyUI。 Can the cellRenderer function return a React component, which is a virtual DOM object, or does it have to be a true HTML DOM object? cellRenderer 函数可以返回一个 React 组件,它是一个虚拟的 DOM 对象,还是必须是一个真正的 HTML DOM 对象? Would doing something like this with ag-Grid's cellRenderer component method be preferable?用 ag-Grid 的 cellRenderer 组件方法做这样的事情会更可取吗?

colDefs.map((x) => {
    if (x.hasOwnProperty('unit')) {
        x.cellRenderer = (params) => {
            return <TableCell value={params.value} units={x.unit} min={min[x.field]} max={max[x.field]} colorScheme={colorScheme} />;
        };
    }
});

对于 React,您希望使用cellRendererFrameworkcellEditorFramework并传入具有setUprender方法的类,而不是使用cellRenderercellEditor等。

I figured out the implementation that is required and figured I'd leave this question up for future users.我想出了所需的实现,并认为我会将这个问题留给未来的用户。 It seems that the component approach is required, and you can use the cellRendererParms to pass in additional arguments.似乎需要组件方法,您可以使用 cellRendererParms 传入其他参数。 Then, within the actual renderer (ReactTableCellRenderer below), you can access them via this.props.params.units (or replace units with whatever other param you pass in the object).然后,在实际的渲染器(下面的 ReactTableCellRenderer)中,你可以通过 this.props.params.units 访问它们(或者用你在对象中传递的任何其他参数替换单元)。

 colDefs.map((x) => {
        if (x.hasOwnProperty('unit')) {
            x.cellRenderer = reactCellRendererFactory(ReactTableCellRenderer);
            x.cellRendererParams = {units: x.unit, min: min[x.field], max: max[x.field], colorScheme: colorScheme};
        }
    });

The params passed via cellRendererParams are then available within the React component (in my case, ReactTableCellRenderer) via this.props.params.units (or substitute appropriate vairable name for units)通过 cellRendererParams 传递的参数可以通过 this.props.params.units 在 React 组件(在我的例子中是 ReactTableCellRenderer)中使用(或用适当的变量名称替换单位)

Update 2021 2021 年更新

Complementing @Tom answer.补充@Tom 的回答。 You want to use cellRendererFramework in the columns definition.您想在列定义中使用cellRendererFramework Here's an example:下面是一个例子:

import * as React from "react";
import { FontIcon } from "@fluentui/react";
import { ColDef, ColGroupDef } from "ag-grid-community";

const isFavoriteCellRenderer = (params: any) => {
    if (params.value) {
        return (
            <div>
                <FontIcon
                    iconName="FavoriteStarFill"
                    title="Favorite"
                    aria-label="Favorite"
                />
            </div>
        );
    }

    return (
        <div>
            <FontIcon
                iconName="FavoriteStar"
                title="Not favorite"
                aria-label="Not favorite"
            />
        </div>
    );
};

export const getIsFavoriteColumn = (): ColDef | ColGroupDef => ({
    headerName: "isFavorite",
    field: "isFavorite",
    cellRendererFramework: isFavoriteCellRenderer,
});

Note params.value is boolean, this could be any type depending on your row model.注意params.value是布尔值,这可以是任何类型,具体取决于您的行模型。

Cell renderer docs for react grid 反应网格的单元格渲染器文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM