简体   繁体   English

从Ag-grid Custom Renderer更新React组件的状态

[英]Update State of React Component from ag-grid Custom Renderer

I need to update the state of React Component from Custom Rendered Component of Ag-grid. 我需要从Ag-grid的自定义渲染组件更新React组件的状态。 I hae tried to setState() in custom component but it says setState() is not a function 我曾尝试在自定义组件中使用setState(),但它说setState()不是函数

 // function to act as a class //https://www.ag-grid.com/javascript-grid-cell-rendering-components/ function editCellRenderer () {} // gets called once before the renderer is used editCellRenderer.prototype.init = function(params) { // create the cell this.eGui = document.createElement('div'); let row = params.data; let className = 'edit-btn-style'; this.eGui.innerHTML = '<span class="my-css-class"><div class="edit-button '+className+'" title="'+row.id+'">\\ <a target="_blank" href="URL='+row.id+'" >\\ <i class="fa fa-edit fa-1x not-selectable-element"></i></a></div> </span>'; }; // gets called once when grid ready to insert the element editCellRenderer.prototype.getGui = function() { return this.eGui; }; // gets called whenever the user gets the cell to refresh editCellRenderer.prototype.refresh = function(params) { // set value into cell again // return true to tell the grid we refreshed successfully return true; }; // gets called when the cell is removed from the grid editCellRenderer.prototype.destroy = function() { // do cleanup, remove event listener from button }; //This is the way i am initializing AgGridReact in Reactjs component. <AgGridReact columnDefs={this.state.columnDefs} defaultColDef={this.state.defaultColDef} rowData={jobs_reindexed} components={this.state.components} frameworkComponents={this.state.frameworkComponents} onGridReady={this.onGridReady} /> 

It is possible to pass context property in grid options. 可以在网格选项中传递上下文属性。 Then you can call parent method from cell renderer. 然后,您可以从单元格渲染器调用父方法。

https://www.ag-grid.com/javascript-grid-context/ https://www.ag-grid.com/javascript-grid-context/

It will look like this: 它看起来像这样:

class ParentComponent extends Component {
    constructor(props) {
        super(props)
        this.state = {
            columnDefs: [
                {
                    headerName: '-',
                    field: '',
                    cellRendererFramework: CustomBtn
                }
            ],
        }

        this.handleStateChange = this.handleStateChange.bind(this)
    }

    handleStateChange(data) {
        /**
         * setState...
         */
    }

    /**
     * onGridReady..., onCellClicked etc.
     */

    render() {
        return (
            <div>
                <AgGridReact
                    columnDefs={this.state.columnDefs}
                    rowData={this.props.rowData}
                    onGridReady={this.onGridReady}
                    onCellClicked={this.handleOnCellClicked}
                    gridOptions={{
                        context: { componentParent: this }
                    }}
                />
            </div>
        )
    }
}

export default ParentComponent;


const CustomBtn = (props) => {
    const handleClick = (e) => {
        e.stopPropagation();
        props.context.componentParent.handleStateChange(props.data);
    };

    return (
        <div>
            <button onClick={handleClick}>
               Change State
            </button>
        </div>
    );
};

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

相关问题 Angular ag-Grid:如何刷新从父组件传递的自定义单元格渲染器中的参数值? - Angular ag-Grid: how to refresh param value in custom cell renderer that is passed from the parent component? React 中的 Ag-grid 纯 JS 单元格渲染器 - Ag-grid plain JS cell renderer in React AG-Grid React,无法在数据更改时更新自定义单元格渲染器。 Function 组件的行为不同于 class 组件 - AG-Grid React, trouble getting custom cell renderer to update when data changes. Function components are behaving differently than class components Ag-grid cell渲染器,滑块不工作 - Ag-grid cell renderer with slider not working 如何将“无行显示”更改为某些自定义组件 ag-grid 反应? - How can I change 'no rows to show' to some custom component ag-grid react? 为单元格渲染器导出 ag-grid 中的数据 - Export data in ag-grid for cell renderer AG-Grid & React Hooks:如何从 cellRendererFramework 组件调用父组件中的 function? - AG-Grid & React Hooks: How to call a function in a parent component from a cellRendererFramework component? Ag-grid 自定义工具提示组件不起作用 - Ag-grid custom tool tip component doesn't work ag-Grid 中的 cellRenderer 函数可以返回 React 组件吗? - Can a cellRenderer function in ag-Grid return a React component? 通过自定义上下文菜单Ag-Grid更新和删除 - Update & Delete by Custom Context Menu Ag-Grid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM