[英]How to insert the input data dynamically in table for particular ID in react JS?
[In this I want to store the value of marks,id,name of each cell in objects of an array.. I try it but not get correct answer please help me how to store marks and id of each cells in objects of an array [在此我想存储数组对象中每个单元格的标记、id、名称的值。我尝试但没有得到正确答案请帮助我如何在数组对象中存储每个单元格的标记和 id
const [values, setValues] = useState(new Array(tableData.length).fill(''));
return (
<TableContainer>
<Table style={{ height: 'max-content' }} stickyHeader>
<TableHead>
<TableRow>
<TableCell
style={{
border: '0px solid transparent',
fontFamily: "'Inter', sans-serif",
}}
align='center'
>
Roll Number
</TableCell>
<TableCell
style={{
border: '0px solid transparent',
fontFamily: "'Inter', sans-serif",
}}
align='center'
>
Name
</TableCell>
<TableCell
style={{
border: '0px solid transparent',
fontFamily: "'Inter', sans-serif",
}}
align='center'
>
Email
</TableCell>
<TableCell
style={{
border: '0px solid transparent',
fontFamily: "'Inter', sans-serif",
}}
align='center'
>
Marks
</TableCell>
</TableRow>
</TableHead>
<TableBody>
{tableData.map((data, index) => (
<>
<TableRow>
<TableCell
key={index}
style={{
border: '0px solid transparent',
fontFamily: "'Inter', sans-serif",
}}
align='center'
>
{data.roll}
</TableCell>
<TableCell
key={index}
style={{
border: '0px solid transparent',
fontFamily: "'Inter', sans-serif",
}}
align='center'
>
{data.name}
</TableCell>
<TableCell
key={index}
style={{
border: '0px solid transparent',
fontFamily: "'Inter', sans-serif",
}}
align='center'
>
{data.email}
</TableCell>
<TableCell
key={index}
style={{
border: '0px solid transparent',
fontFamily: "'Inter', sans-serif",
}}
align='center'
>
<input
key={index}
type={'text'}
style={{
border: '1px solid black',
width: '66px',
height: '32px',
background: '#F2F2F2',
paddingLeft: '5px',
}}
required
value={values[index]}
onChange={(e) => {
setValues(e.target.value);
}}
/>
</TableCell>
</TableRow>
</>
))}
</TableBody>
</Table>
</TableContainer>
);
[1]: https://i.stack.imgur.com/oGvYW.png![enter image description here]( https://i.stack.imgur.com/usHRa.png ) [1]: https://i.stack.imgur.com/oGvYW.png![在此处输入图片描述]( https://i.stack.imgur.com/usHRa.png )
key
attribute only for the parent TableRow
elementTableRow
元素设置key
属性values
state as an object of the form { id, value }
and update the value corresponding to the id changed.values
state 初始化为{ id, value }
形式的 object 并更新与更改的 id 对应的值。 This configuration should prevent potential errors due to table filtering and index mismatches:const [values, setValues] = useState(tableData.map(data => { id: data.id, value: ''}));
const handleValueChange = (e, id) => {
const otherValues = values.filter(v => v.id !== id);
const updatedValue = { id, value: e.target.value };
setValues({ ...otherValues, updatedValue })
}
return (
<TableContainer>
<Table style={{ height: 'max-content' }} stickyHeader>
...
<TableBody>
{tableData.map((data, index) => (
<TableRow key={data.id}>
<TableCell
style={{
border: '0px solid transparent',
fontFamily: "'Inter', sans-serif",
}}
align='center'
>
{data.roll}
</TableCell>
<TableCell
style={{
border: '0px solid transparent',
fontFamily: "'Inter', sans-serif",
}}
align='center'
>
{data.name}
</TableCell>
<TableCell
style={{
border: '0px solid transparent',
fontFamily: "'Inter', sans-serif",
}}
align='center'
>
{data.email}
</TableCell>
<TableCell
style={{
border: '0px solid transparent',
fontFamily: "'Inter', sans-serif",
}}
align='center'
>
<input
type='text'
style={{
border: '1px solid black',
width: '66px',
height: '32px',
background: '#F2F2F2',
paddingLeft: '5px',
}}
required
value={values.find(v => v.id === data.id)?.value || ''}
onChange={(e) => { handleValueChange(e, data.id); }}
/>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.