Header React 的复选框选择 ag-grid(无限行模型)
Header checkbox selection for React ag-grid (infinite row model)
由于无限行模型不支持参数headerCheckboxSelection=true
,
我需要创建自己的复选框。
所以问题是,如何向第一列 header 添加一个复选框,该复选框具有 必要的 api-functions?
使用自定义 header 会重置所有 header 的所有过滤、排序、样式和菜单选项,是否可以只为第一个这样做?
我试过设置第一列 def columnDef.headerComponentFramework = CustomCheckbox
,或者
columnDef.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.
这是我的复选框组件:
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>
);
}
}
编辑:
我得到了显示将复选框组件更改为函数的复选框,并使用了这个 headerComponentFramework = CustomCheckboxFunction
,但我无法将 api 传递给它
找到解决方案!
Ag-grid 显然希望您在自定义组件中有一些 String 值,然后才允许您使用它。所以我所做的只是在我的复选框中添加一些文本,一切都很好。
总结一下:
- 正常创建您的自定义 header 组件(记得添加文本)
- 要设置要与自己的交换的列 header,您可以使用
colDef[index].headerComponentFramework = myNewCustomComponent
或通过 header 名称或 ID 等获取它。这取决于您.
- params object (with api functions) 会自动应用到那里,只记得在构造函数中使用 super(props)
由于无限行模型不支持参数headerCheckboxSelection=true
,
我需要创建自己的复选框。
所以问题是,如何向第一列 header 添加一个复选框,该复选框具有 必要的 api-functions?
使用自定义 header 会重置所有 header 的所有过滤、排序、样式和菜单选项,是否可以只为第一个这样做?
我试过设置第一列 def columnDef.headerComponentFramework = CustomCheckbox
,或者
columnDef.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.
这是我的复选框组件:
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>
);
}
}
编辑:
我得到了显示将复选框组件更改为函数的复选框,并使用了这个 headerComponentFramework = CustomCheckboxFunction
,但我无法将 api 传递给它
找到解决方案!
Ag-grid 显然希望您在自定义组件中有一些 String 值,然后才允许您使用它。所以我所做的只是在我的复选框中添加一些文本,一切都很好。
总结一下:
- 正常创建您的自定义 header 组件(记得添加文本)
- 要设置要与自己的交换的列 header,您可以使用
colDef[index].headerComponentFramework = myNewCustomComponent
或通过 header 名称或 ID 等获取它。这取决于您. - params object (with api functions) 会自动应用到那里,只记得在构造函数中使用 super(props)