javascript - React Js onClick 下拉菜单打开
解决方案
您可以为每个按钮使用 id。这里的例子:
import React, { Component, Fragment } from "react";
import Data from "./parameters.json";
import { Table } from "react-bootstrap";
class Parameter extends Component {
container = React.createRef();
state = {
open: null,
handleOpen: false,
selectedOptions: []
};
handleButtonClick = (e) => {
const id = parseInt(e.target?.id);
if (this.state.open && this.state.open !== id)
return;
this.setState((state) => {
return {
open: state.open !== 0 && !state.open ? id : null
};
});
};
handleChange = (selectedOptions) => {
this.setState({ selectedOptions });
};
render() {
const uniqueTags = [];
Data.map((img) => {
if (uniqueTags.indexOf(img.groupName) === -1) {
uniqueTags.push(img.groupName);
}
});
return (
<div>
<Table style={{ width: "100%" }}>
<thead>
<tr>
<th>Parameter Name</th>
</tr>
</thead>
<tbody>
{uniqueTags.map((value, index) => {
return (
<Fragment>
<tr>
<button
type="button"
className="button"
onClick={this.handleButtonClick}
id={index}
>
<div id={index} style={{ marginLeft: "30px" }}>
<td id={index}>▼{value}</td>
</div>
</button>
</tr>
{this.state.open === index &&
Data.map(
(item) =>
item.zeroBasedEnumeration !== "0" &&
item.groupName === value && (
<tr>
<td style={{ paddingLeft: "80px" }}>
{item.objectName}
</td>
</tr>
)
)}
</Fragment>
);
})}
</tbody>
</Table>
</div>
);
}
}
export default Parameter;
推荐阅读
- ios - Healthkit per hour 包含步数、距离和卡路里的活动摘要?
- javascript - 在显示从 0 到 100 % 的进度时,如何仅在数字符合某些标准时才显示它?
- parameters - 如何在空手道请求的字段中参数化部分字符串
- postgresql - ANY 运算符在使用数组作为参数时存在严重的性能问题
- symfony - Symfony 4.2/API 平台:非确定性安全相关错误
- prolog - Prolog 迷宫赛跑者
- .htaccess - .htaccess RewriteRule 带有额外的查询字符串
- lambda - Kafka Streams 中的 RocksDB 异常
- python - 带有“int”列的 .apply() 方法的问题
- sql-server - 通过传递 SQL 变量更新 xml 数据节点