[英]How to select data that i want when click button (React JS)
i have this example data, when i click button show more it will show popup (using Modal reactbootstrap) and i will show more detail like ID,Name,Age,City,Number,Address,Education and many more.. how i can select and get all data in popup only when i click button 'show more'我有这个示例数据,当我单击按钮显示更多时,它将显示弹出窗口(使用模态 reactbootstrap),我将显示更多详细信息,如 ID、姓名、年龄、城市、数字、地址、教育等等。我怎么能 select只有当我单击按钮“显示更多”时才能在弹出窗口中获取所有数据
and this my code这是我的代码
import React from "react";
import MUIDataTable from "mui-datatables";
import axios from "axios";
import { Modal, Button } from "react-bootstrap";
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
data: [],
errors: null,
isLoading: true,
};
}
get = async () => {
const option = {
url: "/api/url",
method: 'POST',
headers: {
"Access-Control-Allow-Origin": "*"
},
data: {
"data": {
"data": "........."
},
"encrypt": 0
}
};
axios(option)
.then(response => {
const tableData = response.data.data.map(post => {
const {ID,Name,Age,City,Number,Address,Education} = post;
const Popup = () => {
const [lgShow, setLgShow] = React.useState(false);
const [isOpen, setIsOpen] = React.useState(false);
const showModal = () => {
setIsOpen(true);
};
const hideModal1 = () => {
setIsOpen1(false);
};
return (
<div>
<Button onClick={() => setLgShow(true)}>Show more</Button>
<Modal
size="lg"
show={lgShow}
onHide={() => setLgShow(false)}
aria-labelledby="example-modal-sizes-title-lg"
>
<Modal.Header closeButton class="modal-dialog modal-lg">
<Modal.Title id="example-modal-sizes-title-lg">
Data {nama_loket}
</Modal.Title>
</Modal.Header>
<Modal.Body>
Detail<br/>
<div><b> ID</b></div><br />
<div>{ID}</div><br />
<div><b>Name</b></div><br />
<div >{Name}</div><br />
<div><b>Age</b></div><br />
<div>{Age}</div><br />
<div><b>City</b></div><br />
<div>{City}</div><br />
<div><b>Number</b></div><br />
<div>{Number}</div><br />
<div><b>Adress</b></div><br />
<div>{Address}</div><br />
<div><b>Educaton</b></div><br />
<div>{Education}</div><br />
</Modal.Body>
</Modal>
</div>
);
};
return [
[ID],
[Name],
[Age],
[City],
[Number],
<Popup></Popup>
];
});
this.setState({
data: tableData,
isLoading: false
});
console.log(response.data.data);
console.log(this.state.data)
})
// If we catch any errors connecting, let's update accordingly
.catch(error => {
console.log(error.response);
this.setState({ error, isLoading: false })
}
);
}
componentDidMount() {
this.get();
}
render() {
const { isLoading} = this.state;
const columns = ["ID", "Name", "Age", "City", "Phone Number",""];
const options = {
filterType: "dropdown",
responsive: "scroll",
selectableRows:false,
};
return (
<div>
{!isLoading ? (
<MUIDataTable
data={this.state.data}
columns={columns}
options={options}
/>)
: (
<p>Loading...</p>
)}
</div>
);
}
}
export default App
how i get data in my popup when i click.单击时如何在弹出窗口中获取数据。 example i have 5 row, when i click second row, data will selected and get is only the second data in second row.. can anyone help me?例如我有 5 行,当我点击第二行时,数据将被选中并且得到的只是第二行中的第二个数据..有人可以帮我吗?
You can do this by saving the row clicked in the state and then using the state to show it on the modal, just like you are doing to show your modal.您可以通过保存在 state 中单击的行,然后使用 state 在模态上显示它来做到这一点,就像您正在显示您的模态一样。
<Button
onClick={() => {
setLgShow(true)
setSelectedPost(post)
}}
>
Show more
</Button>
The idea is to have one button for each post, but you don't need to render the Modal
more than once, so render the Modal outside of the response.data.data.map
and use the state saved on the selectedPost
to show the data inside the Modal.我们的想法是为每个帖子设置一个按钮,但您不需要多次渲染Modal
,因此在response.data.data.map
之外渲染 Modal 并使用保存在selectedPost
上的 state 来显示模态中的数据。
I think it's better to create the table manually using css https://www.w3schools.com/css/css_table.asp我认为最好使用 ZC7A62 8CBA22E28EB17B5F5C6AE2A266AZ https://www.w3schools.com/css/css_table.asp手动创建表
And for the rows just use React mapping https://reactjs.org/docs/lists-and-keys.html对于行,只需使用 React 映射https://reactjs.org/docs/lists-and-keys.html
Then create a button inside your mapping that call a function to open the modal.然后在映射中创建一个按钮,调用 function 来打开模式。
Create a new state called displayedData to store the row that you want to display.创建一个名为 displayData 的新 state 来存储要显示的行。 Inside your render:在你的渲染里面:
{data.map((value, index) => {
<tr key={index}>
<td>{value.id}</td>
<td>{value.name}</td>
. . .
<td>{value.phone}</td>
<td> <button onClick={()=> this.OpenData(value)}>Show More</button> </td>
</tr>
})}
and for the OpenData function:对于 OpenData function:
async OpenData(value){
await this.setState({displayedData : value})
this.openModal()
}
Last, just use displayedData state to display your data inside the modal.最后,只需使用displayData state 在模态框内显示您的数据。
edit:编辑:
Also, move your modal from your axios fetch, just create its own function.此外,从您的 axios 提取中移动您的模态,只需创建自己的 function。 use your axios fetch just to update your data state使用您的 axios 获取来更新您的数据state
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.