How to fix the algorithm for table generation? - reactjs

I have an algorithm for generating a table of two arrays.
data = [mail, cat, dog]
words = [{postman, mailbox, letter}, {kitty, kitten, koshak, kotyara}, {dog, friend}]
The number of "subarrays" in the words array is always equal to the number of elements in the data array.
The data array is the column headings, and the "subarrays" of words must be columns.
My generation algorithm works fine, but the "subarrays" in my implementation are obtained as rows, and as I said, I need to have columns.
<Paper style={{ maxWidth: 936, margin: "auto" }}>
<Table>
<TableHead>
<TableRow>
{data.map((TableRow, i) => (
<TableCell key={i} align="center">
{TableRow.split("_").join(" ")}
</TableCell>
))}
</TableRow>
</TableHead>
<TableBody>
{this.state.array.map((user, i) => (
<TableRow key={i}>
{Object.values(user).map((v, j) => (
<TableCell key={j} align="center">
<IconButton>
<AddIcon/>
</IconButton>
{v}
<IconButton>
<RemoveIcon/>
</IconButton>
</TableCell>
))}
</TableRow>
))}
<TableRow>
<TableCell align="center">
<Button variant="contained"
color="primary"
style={{
background:
"linear-gradient(45deg, #00ACD3 30%, #00BE68 90%)"
}}
>Обучить
</Button>
</TableCell>
</TableRow>
</TableBody>
</Table>
</Paper>

Related

Chip Component doesn't show up

const ResultsAccordion = ({projects, projectStatus}) =>
{
return (
<Accordion>
<AccordionSummary expandIcon={<ExpandMoreIcon />} aria-controls="panel1a-content" id="panel1a-header">
<Typography>{projectStatus}</Typography>
</AccordionSummary>
<AccordionDetails>
<TableContainer component={Paper} sx={{my:2 }}>
<Table>
<TableHead>
<TableRow style={{ width:"100%" }}>
<TableCell align="center">Team Name</TableCell>
<TableCell align="center">Project Link</TableCell>
<TableCell align="center">Team Members</TableCell>
</TableRow>
</TableHead>
<TableBody>
{
projects.map((value) =>
{
return (
<TableRow key={value.name}>
<TableCell align="center">
{value.name}
</TableCell>
<TableCell align="center">
<Link href={value.repo}>Project Repo</Link>
</TableCell>
<TableCell align="center">
<Stack direction="row" spacing={1} alignContent="center">
{
(value.members).map((item, key) =>
{
return (
<Chip key={key} component="a" href={`https://github.com/${item.github}`} clickable label={item.github} avatar={<Avatar alt={item.github} src={item.avatar}/> } />
);
})
}
</Stack>
</TableCell>
</TableRow>
);
})
}
</TableBody>
</Table>
</TableContainer>
</AccordionDetails>
</Accordion>
);
};
Chips are supposed to be returned in the Table Cell with Stack..
But there is no one.
I used the react Developers tools and there is no childrens in Stack.
The chips are rendered if i made any changes in the prop using ReactDevTools
I checked, the value.members array & it is also not empty.

Material UI, Warning <td> cannot appear as a child of <div>

I'm using React, typescript and Material UI. I've created a pretty common "Create user form" with basic inputs for username, name, email, ect. Added two buttons, "Edit" and "Delete." Everything seems to function properly, however, I cannot get this error message resolved.
Warning: validateDOMNesting(...): <td> cannot appear as a child of <div>.
Here's the table from a react component:
<TableContainer className={classes.scroll} component={Paper}>
<Table stickyHeader aria-label="table">
<TableHead>
<TableRow>
<TableCell>Username</TableCell>
<TableCell align="right">First name</TableCell>
<TableCell align="right">Last name</TableCell>
<TableCell align="right">Email</TableCell>
<TableCell align="right">Connect_username</TableCell>
<TableCell align="right">Role</TableCell>
<TableCell align="left">Edit</TableCell>
<TableCell align="left">Delete</TableCell>
</TableRow>
</TableHead>
<TableBody>
{(rowsPerPage > 0
? props?.items.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
: props?.items
).map(item => (
<TableRow key={item.user_id}>
<TableCell component="th">{item.username}</TableCell>
<TableCell align="right">{item.first_name}</TableCell>
<TableCell align="right">{item.last_name}</TableCell>
<TableCell align="right">{item.email}</TableCell>
<TableCell align="right">{item.connect_username}</TableCell>
<TableCell align="right">{item.role?.map(r=>r.role).join(",")}</TableCell>
<TableCell>
<Button onClick={() => props.handleEdit(item)}>Edit</Button>
</TableCell>
<TableCell>
<Button onClick={() => props.handleConfirmDelete(item.user_id)}>Delete</Button>
</TableCell>
</TableRow>
))}
</TableBody>
<TablePagination
rowsPerPageOptions={[10, 25, { label: 'All', value: -1 }]}
count={props.items.length}
rowsPerPage={rowsPerPage}
page={page}
onPageChange={handleChangePage}
onRowsPerPageChange={handleChangeRowsPerPage}
/>
</Table>
</TableContainer>
component={Paper} is likely causing this. Can you try removing it? If you want the table to appear on a Paper component, then try nesting TableContainer under Paper.
put TablePagination in the TableBody tag and wrap it in TableRow.
Had a similar issue and this fixed it.
Like this:
<TableBody>
{(rowsPerPage > 0
? props?.items.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
: props?.items)
.map(item => (
<TableRow key={item.user_id}>
<TableCell component="th">{item.username}</TableCell>
<TableCell align="right">{item.first_name}</TableCell>
<TableCell align="right">{item.last_name}</TableCell>
<TableCell align="right">{item.email}</TableCell>
<TableCell align="right">{item.connect_username}</TableCell>
<TableCell align="right">{item.role?.map(r=>r.role).join(",")}</TableCell>
<TableCell>
<Button onClick={() => props.handleEdit(item)}>
Edit
</Button>
</TableCell>
<TableCell>
<Button onClick={() => props.handleConfirmDelete(item.user_id)}>
Delete
</Button>
</TableCell>
</TableRow>
<TableRow>
<TablePagination
rowsPerPageOptions={[10, 25, { label: 'All', value: -1 }]}
count={props.items.length}
rowsPerPage={rowsPerPage}
page={page}
onPageChange={handleChangePage}
onRowsPerPageChange={handleChangeRowsPerPage}
/>
</TableRow>
))}
</TableBody>
update: "#mui/material": "^5.10.9"
in my case this error was causing TablePagination and not Paper component.
working solution is wrapping pagination not only with row but also footer as follow
<TableContainer component={Paper}>
<Table>
<TableBody>
...
</TableBody>
<TableFooter>
<TableRow>
<TablePagination />
</TableRow>
</TableFooter>
</Table>
</TableContainer>
mui docs: https://mui.com/material-ui/react-table/#custom-pagination-actions

Change Table Row color based on condition in React

I want to change table row color based on condition .
My code :
<TableBody>
{
this.props.result.alpha.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage).map((row,i) =>(
<TableRow key={i} >
<TableCell component="th" scope="row" >
<Typography variant="h4"> {row.a} </Typography>
</TableCell>
<TableCell align="left" > <Typography variant="h4">{row.b}</Typography> </TableCell>
<TableCell align="left" > <Typography variant="h4"> {row.c} </Typography> </TableCell>
</TableRow>
))}
</TableBody>
Now i want to check if the value of key==null then color of row should be some color1 or else color2
i.e., Something like this.
if(row.c==null) then rowcolor= color1;
else rowcolor= color2;
How to do this in react ?
Nt: Your question is rather vague and need more clarification.
I suppose you want to set a class based on a condition, yeah?
You can use a tarnary operator to set a class name based on your condition.
First you need to create two classes. One for the positive condition and the other for the contrary.
Then do something like
<TableBody>
{
this.props.result.alpha.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage).map((row,i) =>(
<TableRow key={i} >
<TableCell component="th" scope="row" >
<Typography variant="h4"> {row.a} </Typography>
</TableCell>
<TableCell align="left" > <Typography variant="h4">{row.b}</Typography> </TableCell>
<TableCell align="left" > <Typography variant="h4" style={row.c === null ? blue : pink > {row.c} </Typography> </TableCell>
</TableRow>
))}
</TableBody>
const pink = {
backgroundColor: 'pink'
}
const blue ={
backgroundColor: 'blue'
}
Edit
In regards to the clarification made by the OP, I have changed from className to style.

How to create a new table with selected options from a dropdown?

I want to create a new table that is composed of previously selected options from a dropdown menu (Material-UI). Since I'm new to React and Material-UI, I can't figure out how to do this.
There is already a table including a column that contains a dropdown. This dropdown allows to select multiple options.
Component TableInput:
function TableInput() {
return (
<div>
{/* First table with the multiselect-dropdown */}
<Paper>
<Table>
<TableHead>
<TableRow>
<TableCell align="right">Examns</TableCell>
</TableRow>
</TableHead>
<TableBody>
<TableRow>
<TableCell align="right">
<ExamInput />
</TableCell>
</TableRow>
</TableBody>
</Table>
</Paper>
{/* Second table that should be rendered according to the selected options in the first table */}
<Paper>
<Table>
<TableHead>
<TableRow>
<TableCell align="right">Exam</TableCell>
</TableRow>
</TableHead>
<TableBody>
<TableRow>
<TableCell component="th" scope="row">
{/* Here the selected options from the first table should be listed */}
{/* for example. "Master, Bachelor, PhD" */}
</TableCell>
<TableCell align="right">
{/* Here, another multiselect-dropdown should appear according to the rendered option in the first column
It is used to select the achieved score in the examn
Each examn has a predefined list of score options.*/}
</TableCell>
</TableRow>
</TableBody>
</Table>
</Paper>
</div>
)
}
Component ExamnInput (that is used in TableInput):
function ExamInput() {
const names = ['Abitur', 'Mittlere Reife', 'Master', 'Bachelor']
const [examn, setExamn] = React.useState<string[]>([])
function handleChange(event: React.ChangeEvent<{ value: unknown }>) {
setExamn(event.target.value as string[])
}
return (
<Paper>
<FormControl>
<InputLabel htmlFor="select-multiple">Exams</InputLabel>
<Select
multiple
value={examn}
onChange={handleChange}
input={<Input id="select-multiple" />}
>
{names.map(name => (
<MenuItem
key={name}
value={name}
>
{name}
</MenuItem>
))}
</Select>
</FormControl>
</Paper>
)
}
Furthermore I created a super simple image showing how the whole thing should look.
Thanks so much in advance!

get firestore collection record count for pagination react [duplicate]

This question already has answers here:
Cloud Firestore collection count
(29 answers)
Closed 3 years ago.
I have a table to display users collection including search filter. Now I want to add a material-ui pagination, where i want to pass the total record count.
the pagination works fine. How can I get that?
Any help appreciated.!
table and table pagination code
<Table className={classes.table}>
<TableHead>
<TableRow className={classes.tableHeader}>
<TableCell >#</TableCell>
<TableCell ></TableCell>
<TableCell>Name</TableCell>
<TableCell align="right">Phone</TableCell>
<TableCell align="right">Role</TableCell>
<TableCell align="right">Service</TableCell>
<TableCell align="right">Location</TableCell>
</TableRow>
</TableHead>
<TableBody>
{filteredList && filteredList
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
.map(row => (
<TableRow key={row.id}>
<TableCell>{items++}</TableCell>
<TableCell align="right">
<Link to={'/admin/profile/' + row.id} key={row.id} >
<Tooltip title="View Profile">
<UserIcon/>
</Tooltip>
</Link>
</TableCell>
<TableCell component="th" scope="row">
{row.sp_Name}
</TableCell>
<TableCell align="right">{row.sp_Phone}</TableCell>
<TableCell align="right">{row.sp_Role}</TableCell>
<TableCell align="right">{row.sp_Service}</TableCell>
<TableCell align="right">{row.sp_Location}</TableCell>
{/* <TableCell align="right">
<ApproveIcon onClick={this.handleClickDialogOpen} className={classes.icon} />
</TableCell> */}
<TableCell align="right">
<Tooltip title="Delete">
<DeleteIcon onClick={() => this.handleClickDialogOpen(row.id)} className={classes.icon} />
{/* <DeleteIcon onClick={() => deleteSP(row.id)} className={classes.icon} /> */}
</Tooltip>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
<TablePagination
rowsPerPageOptions={[5, 10, 25]}
component="div"
//count={5}
count ={filteredList.length} -- I want to pass count
rowsPerPage={this.state.rowsPerPage}
page={this.state.page}
backIconButtonProps={{
'aria-label': 'Previous Page',
}}
nextIconButtonProps={{
'aria-label': 'Next Page',
}}
onChangePage={this.handleChangePage}
onChangeRowsPerPage={this.handleChangeRowsPerPage}
/>
Firestore doesn't keep a count of the number of documents in a collection. If you need that, you'll have to store it yourself and keep it up to date. For an example of this, see the documentation on aggregation queries and distributed counters.
Also see: Firebase firestore collection count

Resources