In the given code below, there is td tag which includes input tag inside it. I have disabled it initially so that the user can't change the data. Because of this if the data inside it is more than 2 lines or something then all of it will not be visible.
1) I want to make data fit as per its length, like 'textarea'. 2) In the end, there is an "Edit" button, when I click on that button all the td tags should become input tags. Therefore I should be able to edit the data inside it. Please can anyone help me with this? I want to achieve this using react.js Thank you in advance.
import React, {useEffect, useState} from 'react';
import '../src/tabledata.css';
function TableData() {
const [data, getData] = useState([])
useEffect(() => {
fetchData()
},[])
const fetchData = () => {
fetch("/api")
.then((res) =>
res.json())
.then((response) => {
console.log(response);
getData(response);
})
}
function edit() {
}
return (
// whole body
<div className='body'>
<h1 className='heading'>GCP DOCUMENT AI DATA</h1> {/* heading */}
<div> {/* container for table */}
<div className='tbl'> {/* div for table inside container */}
<tbody>
<tr> {/* headings in table */}
<th>Keys</th>
<th>Values</th>
</tr>
{data.map((item,i) => (
<tr key={i}> {/* 1st column keys and 2nd column values */}
<td>{item.keys}</td>
<td><input className='tdValueInput' type="text" value={item.values} disabled/></td>
</tr>
))}
<div> {/* div for buttons */}
<input type="submit" className='bttn-edit' value="Edit" onClick={edit}/>
<input type="submit" className='bttn-approve' value="Approve"/>
</div>
</tbody>
</div>
</div>
</div>
);
}
export default TableData;
You need to do few things:
const [isEditing, setEditing] = useState(false);
setEditing(true);
<td>
vs <input>
based on isEditing
variable{isEditing ? <td>{item.values}</td> : <td><input value={item.values} /></td>}
onChange
prop for input to change edited data<input value={item.values} onChange={(e) => item.values = e.target.value} />
You have invalid HTML structure - missing <table>
and mixing <div>
with <tr>
element on same level
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.