简体   繁体   English

单击按钮时如何获取我想要的 select 数据(React JS)

[英]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.

相关问题 我想从 react.js 中单击按钮的选择选项中获取选定的值 - I want to get selected value from select options on button click in react.js 当我单击 class 组件 React JS 中的按钮时,我希望图像发生变化 - I want the image to change when i click the button in class component React JS 当用户单击共享按钮时,我想共享此 JSON DATA - I want to share this JSON DATA when user click on share button 当我使用 react.js 单击选择时,如何显示三个参数? - How can I do to show three params when I click on the select using react.js? 使用d3-如何在单击按钮时从数组中选择特定数据以突出显示? - Using d3 - How do I select specific data from array to highlight when I click a button? 如何在 React JS 中单击按钮时在独立组件之间传递数据? - How to pass data between to independent component on button click in React JS? 如何在redux react js中通过按钮单击传递数据 - How to pass data by button click in redux react js 当我点击按钮时 Vue JS 我希望 div 滚动到 div 的底部 - Vue JS when I click on button I want that the div scroll to bottom of the div 当我单击 VUE js 中的按钮时,我想显示模态框 - I want to show Modal box when I click a button in VUE js 当有人点击 html 按钮或 div 时,我想 select 并在 google Geochart 上突出显示国家/地区 - I want to select and highlight country on google Geochart when someone click on a html button or div
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM