0
I need to create a table with Material UI that will actually be a matrix, the size of this matrix will be dynamic and precise that the cells in the table are inputs
to be completed and inputs
will create the truth matrix that will be passed to the server and saved in the database.
I managed to generate the table with the inputs
and everything else, but I don’t know how to manage state
of the inputs
to get the values that will be saved, until then the table code is like this:
import React, { useContext, useState } from 'react'
import {
Table,
TableBody,
TableCell,
TableContainer,
TableHead,
TableRow,
TextField,
Grid,
Button,
Paper
} from '@material-ui/core'
import { ProjetoContext } from 'contexts/projetos'
const TabelaPonderacao = () => {
const { projetoAtual } = useContext(ProjetoContext)
const [matriz, setMatriz] = useState([[]])
console.log(matriz)
const handleClickLimpar = () => {
console.log('Limpou')
}
const handleClickSalvar = () => {
console.log(matriz)
}
return (
<>
<Grid container spacing={4}>
<Grid item>
<TableContainer component={Paper}>
<Table aria-label='simple table'>
<TableHead>
<TableRow>
<TableCell>Poderação dos Critérios</TableCell>
{projetoAtual.criterios.map((criterio) => (
<TableCell key={criterio.id}>{criterio.nome}</TableCell>
))}
</TableRow>
</TableHead>
<TableBody>
{projetoAtual.criterios.map((criterio) => (
<TableRow key={criterio.id}>
<TableCell>{criterio.nome}</TableCell>
{projetoAtual.criterios.map((c) => (
<TableCell key={c.id}>
<TextField
id='outlined-basic'
variant='outlined'
onChange={(e) => {
const val = e.target.value
setMatriz(prevState => {
console.log(prevState)
return [...prevState, prevState[0].concat(val)]
})
}}
/>
</TableCell>
))}
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
</Grid>
<Grid container spacing={2} justify='flex-end'>
<Grid item>
<Button variant='outlined' onClick={handleClickLimpar}>
Limpar
</Button>
</Grid>
<Grid item>
<Button variant='outlined' onClick={handleClickSalvar} color='primary'>
Salvar
</Button>
</Grid>
</Grid>
</Grid>
</>
)
}
export default TabelaPonderacao
Example:
The state of inputs should return me an array with these values:
[[1,3,5,3], [1/3,1,5,7], [1/5,1/5,1,5], [1/3,1/7,1/5,1]]
and by clicking save I will make the persistence logic.
is not in the
array
criterios
?– novic
didn’t get it? What I need is to generate a matrix through the state, and do a setState on inputs, only I don’t know how to do this
– JelSnow
For example, if I were to generate an array, I would only need to do a prevState.Concat() passing the input value, only what I want is to generate an array and I don’t know how to do it.
– JelSnow
Friend if you want to update the data of an enumeration of values, like this has 3 inputs and consequently 3 states and you need to update? is about that? something else
criterios.map
is consequently aarray
? but in that code has not declared him... where he is?– novic
Well, I added the full code. What I want is from an array of objects, generate inputs that received numbers and with these numbers assemble an array. What I’m failing to do is manage the state of inputs that I’m dynamically creating. I don’t know how to do that. I hope now I’ve made my problem clear, if you don’t just ask me to answer,.
– JelSnow
So I put an example of how to manage objects inside a
array
– novic
Explain this matrix better and it has to be like this, a matrix, what is your idea?
– novic
In the case on the server side I will use this matrix to do some calculations, so I need the input data to be recorded as follows: [[1, 3, 4], [2, 1, 4], [7, 3, 1]], so what I want to do is render the matrix with the inputs so that the user can fill in and that the result of what the user fills in is in the matrix format. I don’t know if it’s clear, anything you can ask. Thank you so much for helping me
– JelSnow
I added the final rendering result to the question, along with the explanation of the previous comment should be clearer what I want to do.
– JelSnow