简体   繁体   中英

ag-Grid Cell Rendering - Custom Props

After looking through the ag-Grid Cell Rendering documentation I don't see a way to add custom properties to the cell renderer via the cellRenderFramework argument. For example, I'd like to pass in a callback to my custom renderer that is called when the button is clicked.

class AddCellRenderer extends React.Component<ICellRendererParams, {}> {

  /**
   * Callback handle to pass data to on a click.
   */
  public static callback = (data: MyData): void => { return; };

  public constructor(params: ICellRendererParams) {
    super(params);
  }

  public render() {
    let className = 'pt-button pt-intent-success pt-icon-add';
    let text = 'Click to add';
    if (this.props.data.saved) {
      className = 'pt-button pt-intent-danger pt-icon-remove';
      text = 'Click to remove';
    }
    return (
      <button type="button" onClick={this.onClick} className={className}>{text}</button>
    );
  }

  private onClick = (event: React.FormEvent<HTMLButtonElement>): void => {
    AddCellRenderer.callback(this.props.data);
    this.setState({ ...this.state }); // Make React update the component.
  }
}

And I have another component that uses it like so

public render() {
  let saveCellRenderer = AddCellRenderer;
  saveCellRenderer.callback = this.onSelectData;
  const columnDefs: ColDef[] = [
    {
      headerName: 'Add to Available Data',
      cellRendererFramework: AddCellRenderer,
    },
    ...
  ];
  ...
  }

There must be another way to pass this callback as props other than a static public method which is ugly and will not work when multiple components use the renderer, how do I go about doing this with ag-Grid?

I know this is late. But have you tried cellRendererParams with cellRenderer ?
You can set custom props for the custom cell component using it. Eg:

columnDef = [
    .......
    {
        .......
        cellRenderer: CustomCellComponent,
        cellRendererParams: {
            prop1: "this is prop1"
        }
    }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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