【问题标题】:How to pass the proper input to ag-grid?如何将正确的输入传递给 ag-grid?
【发布时间】:2021-11-12 08:35:09
【问题描述】:

我面临一个非常奇怪的问题,我正在为 agGrid 设置 rowData。如果我直接传递变量,它不起作用,但在控制台上打印它并分配它是有效的。我假设这可能是数据格式问题,但我想知道从控制台复制时它是如何工作的。

  const getPeerData = (rows) => {
    let company = rows;
    console.log(company) // If I copy from console and paste like below it's working fine
    company = [{
      "currency": "USD",
      "count": 24
    }]
    return company;
  };


  const gridOptions = {
    columnDefs : columnDefs,
    rowData : getPeerData(peerData),  // I tried rowData = peerData but it's not working
    rowSelection : 'single'
  };

这里可能有什么问题?

【问题讨论】:

    标签: javascript reactjs ag-grid


    【解决方案1】:

    在页面加载完成后设置网格:-

     const getPeerData = (rows) => {
            let company = rows;
            console.log(company) 
             company = [{
              "currency": "USD",
              "count": 24
            }];
            return company;
          };
        
        
        // setup the grid after the page has finished loading
        
            document.addEventListener('DOMContentLoaded', function () {
              var gridDiv = document.querySelector('#myGrid');
              new agGrid.Grid(gridDiv, gridOptions);
               gridOptions.api.setRowData(getPeerData());
            });
    

    或者,

    考虑使用 ag-grid 的 react 版本:-

    https://plnkr.co/edit/NfTowhZqpqwd87HX

    render() {
        return (
          <div style={{ width: '100%', height: '100%' }}>
              <AgGridReact
                modules={this.state.modules}
                columnDefs={this.state.columnDefs}
                defaultColDef={this.state.defaultColDef}
                onGridReady={this.onGridReady}
                rowData={this.state.rowData}
              />
          </div>
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-06
      • 2020-03-28
      • 2020-04-04
      • 2017-08-16
      • 1970-01-01
      • 1970-01-01
      • 2017-11-15
      • 2021-01-03
      相关资源
      最近更新 更多