next.js 如何将id从删除按钮传递到弹出的删除按钮

svgewumm  于 2023-10-18  发布在  其他
关注(0)|答案(2)|浏览(90)

我在一个前端应用程序中有一个表,其中每一行代表一个项目,对于每一行,您都有一个“删除”按钮,单击时会触发一个警告弹出窗口。在这个弹出窗口中,您希望传递项的ID,这样如果用户确认删除,您就可以从后端的表中删除相应的项。

import axios from "axios";
    import { useEffect, useState } from "react"
    import styles from '../../styles/Home.module.css'
    
    
    const List = () => {
        const [rowData, setRowData] = useState([]);
    
        useEffect(() => {
            axios.get('/api/institute/instituteInfo')
            .then((response) => {
                setRowData(response.data);
                })
        }, [])
    
        const popupBox = (id) => {
          document.getElementById('deleteBox').style.display='block';
        }
    
        const closePopupBox = () => {
          document.getElementById('deleteBox').style.display='none';
        }
       
        const softDelete = () => {
          axios.delete(`/api/institute/instituteInfo/${id}`)
          .then(() => {
          getData();
      })
        }

    return (



      
    <div className="flex flex-col relative">
    <div className="-my-2 overflow-x-auto sm:-mx-6 lg:-mx-8">
    <div className="py-2 align-middle inline-block min-w-full sm:px-6 lg:px-8">
    <div className="shadow overflow-hidden border-b border-gray-200 sm:rounded-lg">
    <table className="min-w-full divide-y divide-gray-200">
    <thead className="bg-gray-50">
    <tr>
    <th scope="col" className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
     Institute Name
     </th>
     <th scope="col" className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
     Domain or Subdomain
     </th>
     <th scope="col" className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
     No. of Users
     </th>
     </tr>
     </thead>
     <tbody className="bg-white divide-y divide-gray-200">
     {rowData.map((data => {
     return (
     <tr key={data._id}>
     <td className="px-6 py-4 whitespace-nowrap">
     <div className="flex items-center">
                              
     <div className="ml-4">
     <div className="text-base font-medium text-gray-900">
     {data.institute_name}
     </div>
     <div className="text-sm text-gray-500">
     {data.institute_address}
     </div>
                                
     <div className="text-sm text-blue mt-1">
     {data.institute_email}
     </div>
     <div className="text-sm text-gray-500">
     {data.institute_phone}
     </div>
     </div>
     </div>
     </td>
     <td className="px-6 py-4 whitespace-nowrap">
     <div className="text-xs text-gray-900">Domain</div>
     <div className="text-bs text-gray-500">{data.institute_subdomain}</div>
                            
     <div className="text-xs text-gray-900 mt-1">Sub-Domain</div>
     <div className="text-bs text-gray-500">{data.institute_subdomain}</div>
     </td>
                          
     <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
     Admin
     </td>
     <td className="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
     <div className="flex flex-col">
     <button className='border-2 border-color: border-blue-700 text-blue-dark w-28 h-8 mt-6 rounded' 
     type='submit'>Edit</button>
     <button className='border-2 border-color: border-gray-300 w-28 h-8 mt-1 rounded' 
     type='submit' onClick={() => popupBox(data._id)} >Remove</button>
     <button className='border-b-2 border-color: border-gray-500 w-28 h-8 mt-1 rounded' 
     type='submit'>Add to Blacklist</button>
     </div>
     </td>
     </tr>
     )}))}
     </tbody>
     </table>
                     
     </div>
     </div>
     </div>
     div className={styles.manage}>
     <div id="deleteBox" className='w-11/12 pl-9 hidden'>
     <h1 className='mt-4 font-bold text-lg'>Delete the institute?</h1>
     <p className='text-sm'>You'll lose all contacted data. We can't recover them once you delete.</p>
     <div>
     <textarea id="reasonToDelete" name="reasonToDelete" rows="4" cols="50" 
     className='border-2 border-color: border-gray-300 rounded p-2 mt-4' placeholder="Write the reason to delete"></textarea>
      </div>
       <div  className='mb-10'>
       <button className='border-2 border-color: border-gray-300 w-28 h-8 mt-1 rounded'
       onClick={() => closePopupBox()}>Cancel</button>
       <button className='bg-red-color text-gray-light bg-opacity-75 font-normal w-28 h-8 mt-1 ml-1 rounded' 
       onClick={() => softDelete()}>Yes, delete it</button>
       </div>
       </div>    
       </div>             
     </div>
            
)
 }

导出默认列表;

//Home.module.css
.manage {
    margin-top: -64rem;
    margin-left: 50rem;
    background-color: white;

}
wfveoks0

wfveoks01#

你可以在这里使用状态,
当弹出窗口打开时设置模态id,在remove按钮调用时传递状态。
const [modalId, setModalId] = useState("");
当模态关闭时,将状态重置为默认值。

kmb7vmvb

kmb7vmvb2#

嘿,兄弟们,首先使用react忘记了直接操作DOM,事实上,你应该做的是使用REFS来获得POPUP警报,尝试找到一个YouTube视频解释如何在react中实现模态,其次,当你有一个引用到你的模态时,它与document.getElementByID(“deleteBox”)“相同,但在react中”现在获得ID很简单,你应该做的是:无论何时你打开这个模式(弹出窗口),检查Ref中的parent,并在常量中获取它的ID,然后将其转发到axios中的链接。

相关问题