我有一个 React Table( https://react-table.js.org ),它从来自 API 的数据中填充。在表格上方,我创建了一个过滤器组件,它包含一个带有多个选项的下拉列表。从下拉列表中选择特定参数后,我需要根据所选参数过滤表。我分别存储了过滤器组件的状态和表组件的状态。由于表和过滤器都是单独的组件,如何从过滤器组件中获取值并过滤表?我的表如下:
<ReactTable
data={tableData}
noDataText="No Appointments"
loading={this.props.loading}
showPagination={false}
filterable
defaultFilterMethod={(filter, row) =>
String(row[filter.id]) === filter.value}
columns={[
{
columns: [
{
sortable: false,
filterable: false,
Header: "Id",
accessor: "resourceId",
headerStyle: {
background: '#ECEFF1',
},
},
{
sortable: false,
filterable: false,
Header: "Tenant Name",
accessor: "Name",
id: "Tenant Name",
headerStyle: {
background: '#ECEFF1',
},
},
</ReactTable>
我的过滤器组件如下:
export default class PopoverExampleAnimation extends
React.Component {
constructor(props) {
super(props);
this.state = {
open: false,
clicked: [],
Id: '',
tenantName: '',
};
this.handleRequestClose = this.handleRequestClose.bind(this);
this.getId = this.getId.bind(this);
this.getTenantName = this.getTenantName.bind(this);
}
handleTouchTap = (event) => {
// This prevents ghost click.
event.preventDefault();
this.setState({
open: true,
anchorEl: event.currentTarget,
});
};
handleRequestClose = () => {
this.setState({
open: false,
});
};
getId = (Id) => {
console.log(Id);
this.setState({Id});
}
getTenantName = (tenantName) => {
console.log(tenantName);
this.setState({tenantName});
}
render() {
return (
<div>
<RaisedButton
onClick={this.handleTouchTap}
label="FILTER"
labelColor="#26A69A"
/>
<Popover
open={this.state.open}
anchorEl={this.state.anchorEl}
anchorOrigin={{ horizontal: 'left', vertical: 'bottom' }}
targetOrigin={{ horizontal: 'left', vertical: 'top' }}
onRequestClose={this.handleRequestClose}
animation={PopoverAnimationVertical}
>
<Menu>
<MenuItem
primaryText={"NAME - " + this.state.tenantName}
rightIcon={<ArrowDropRight />}
menuItems={[
<MenuItem
primaryText="Group 1"
onClick={() =>
this.getTenantName('Group 1')
}
/>,
<Divider />,
<MenuItem primaryText="Group 2"
onClick={() =>
this.getTenantName('Group 2')
}
/>,
]}
/>
<Divider />
<MenuItem
primaryText={"ID - " + this.state.Id}
rightIcon={<ArrowDropRight />}
menuItemStyle={{ backgroundcolor: '#E0F2F1' }}
menuItems={[
<MenuItem primaryText="1" onClick={() =>
this.getId('1')
}
/>,
<Divider />,
<MenuItem primaryText="2" onClick={() =>
this.getId('2')
}
/>,
<Divider />,
<MenuItem primaryText="3" onClick={() =>
this.getId('3')
}
/>,
<Divider />,
<MenuItem primaryText="4" onClick={() =>
this.getId('4')
}
/>,
]}
/>
<Divider />
<RaisedButton
label="APPLY"
style={{ margin: 2, width: '60px' }}
labelColor="#FAFAFA"
backgroundColor="#26A69A"
/>
<RaisedButton
label="CANCEL"
style={{ margin: 22, width: '60px' }}
labelColor="#26A69A"
onClick={() =>
//this.getId(' ')
this.handleRequestClose()
}
/>
</Menu>
</Popover>
</div>
);
}
}
表和过滤器都是独立的组件,我没有使用 Redux 进行状态管理。