<html>
<head>
<meta charset="UTF-8" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script>
</head>
<body>
<div id="app"></div>
<script type="text/babel">
const { useState} = React;
function App() {
const [rows, setRows] = useState([
{ id: 1685700582922, name: 'John Doe', age: 25 },
{ id: 1685700616359, name: 'Jane Smith', age: 30 },
{ id: 1685700626357, name: 'Mark Johnson', age: 35 },
{ id: 1685700636194, name: 'Emily Davis', age: 28 }]);
const [name, setName] = useState('');
const [age, setAge] = useState('');
const [showModal, setShowModal] = useState(false);
const addRow = () => {
const newRow = {
id: Date.now(),
name: name,
age: age,
};
setRows([...rows, newRow]);
setName('');
setAge('');
setShowModal(false);
};
const deleteRow = (index) => {
const updatedRows = [...rows];
updatedRows.splice(index, 1);
setRows(updatedRows);
};
return (
<div className="container">
<h3>React Js Add & Delete Table Row Dynamically</h3>
<button class='Add-button' onClick={() => setShowModal(true)}>Add Row</button>
{showModal && (
<div className="modal">
<div className="modal-content">
<span className="close" onClick={() => setShowModal(false)}>
×
</span>
<input
type="text"
placeholder="Name"
value={name}
onChange={(e) => setName(e.target.value)}
/>
<input
type="text"
placeholder="Age"
value={age}
onChange={(e) => setAge(e.target.value)}
/>
<button className='save-button' onClick={addRow}>Save</button>
<button className='close-button' onClick={() => setShowModal(false)}>Close</button>
</div>
</div>
)}
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{rows.map((row, index) => (
<tr key={row.id}>
<td>{row.id}</td>
<td>{row.name}</td>
<td>{row.age}</td>
<td>
<button className='delete-button ' onClick={() => deleteRow(index)}>Delete</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
);
}
ReactDOM.render(<App />, document.getElementById("app"));
</script>
<style>
* {
box-sizing: border-box;
}
.container {
margin: 0 auto;
width: 600px;
text-align: center;
box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.12), 0 2px 4px 0 rgba(0, 0, 0, 0.24);
padding: 20px;
}
.Add-button {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
cursor: pointer;
font-size: 16px;
margin-bottom: 20px;
float: right;
}
.modal {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
z-index: 9999;
}
.modal-content {
background-color: white;
border-radius: 5px;
padding: 30px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
max-width: 400px;
width: 100%;
position: relative;
}
.close {
position: absolute;
top: 0px;
right: 10px;
font-size: 25px;
color: #999;
cursor: pointer;
}
.close:hover {
color: #666;
}
input[type="text"] {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
margin-right: 10px;
}
.save-button {
background-color: #4CAF50;
color: white;
}
.save-button:hover {
background-color: #45a049;
}
.close-button {
background-color: #f44336;
color: white;
}
.close-button:hover {
background-color: #d32f2f;
}
table {
width: 100%;
border-collapse: collapse;
}
th,
td {
border: 1px solid #ddd;
padding: 8px;
}
th {
background-color: #f2f2f2;
text-align: left;
}
.delete-button {
background-color: #f44336;
color: white;
padding: 8px 16px;
border: none;
cursor: pointer;
font-size: 14px;
}
.delete-button:hover {
background-color: #cc0000;
}
</style>
</body>
</html>