Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 707
Lucas Avatar asked May 24 '17 19:05

Lucas


1 Answers

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"
        }
    }
]
like image 135
Ishwor Timilsina Avatar answered Oct 17 '22 07:10

Ishwor Timilsina