简体   繁体   English

Header 复选框选择用于 React ag-grid(无限行模型)

[英]Header checkbox selection for React ag-grid (infinite row model)

Since infinite row model is not supporting the parameter headerCheckboxSelection=true , I need to create my own checkbox.由于无限行 model 不支持参数headerCheckboxSelection=true ,我需要创建自己的复选框。 So the question is, how do I add a checkbox to the first column header, that has the necessary api-functions?所以问题是,如何在第一列 header 中添加一个具有必要 api 功能的复选框?
Using a custom header resets all filtering, sorting, styling and menu options for all headers, is it possible to do it just for the first one?使用自定义 header 会重置所有标题的所有过滤、排序、样式和菜单选项,是否可以只为第一个做呢?
I've tried setting the first column def columnDef.headerComponentFramework = CustomCheckbox , or columnDef.headerComponent = CustomCheckbox , or using them as a string, but all I get is this error:我尝试设置第一列 def columnDef.headerComponentFramework = CustomCheckboxcolumnDef.headerComponent = CustomCheckbox ,或者将它们用作字符串,但我得到的只是这个错误:

Warning: React.createElement: type is invalid -- expected a string (for built-in components or a class/function (for composite components) but got: undefined.

Here is my checkbox component:这是我的复选框组件:

export default class customCheckbox extends Component {
  constructor(props) {
    super(props);
    console.log(props)
    this.state = {
      checked: false,
    };
  }

  updateState = e => {
    this.setState({ checked: e.value });
    if (!this.state.checked) this.selectAllRows(true);
    if (this.state.checked) this.selectAllRows(false);
  };

  selectAllRows = bool => {
    this.props.api.forEachNode(row => {
      this.props.api.getRowNode(row.id).selectThisNode(bool);
    });
  };
  render() {

    return (
      <div className="custom-header-checkbox">
        <CheckBox value={this.state.checked} onChange={this.updateState} />
      </div>
    );
  }
}

Edit:编辑:

I got the checkbox to show changing the checkbox component to a function and with this headerComponentFramework = CustomCheckboxFunction , but I couldn't pass the api to it我得到了复选框来显示将复选框组件更改为 function 并使用此headerComponentFramework = CustomCheckboxFunction ,但我无法将 api 传递给它

Found the solution!找到了解决方案!

Ag-grid apparently expects you to have some String value in your custom component before it allows you to use it. Ag-grid 显然希望您在自定义组件中拥有一些 String 值,然后才能使用它。 So all I did was add some text to my checkbox and all was fine.所以我所做的就是在我的复选框中添加一些文本,一切都很好。

To summarize:总结一下:

  1. Create your custom header component normally (remember to add text)正常创建您的自定义 header 组件(记得添加文本)
  2. To set the column header you want to swap with your own, you can use colDef[index].headerComponentFramework = myNewCustomComponent or by getting it via header name or id etc. It is up to you.要设置要与自己交换的列 header,可以使用colDef[index].headerComponentFramework = myNewCustomComponent或通过 header 名称或 ID 等获取它。这取决于您。
  3. The params object (with api functions) are automatically applied there, just remember to super(props) in the constructor参数 object (带有 api 函数)会自动应用到那里,只需记住在构造函数中使用 super(props)

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

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