how to do bind data with material-ui table and also pagination - reactjs

I have facing an issue with binding my data with the material-UI table
(1) first thing it is just showing the product name and when I am trying to add product price so it is now binding there
(2) also I have done pagination on my server-side but when i change the page of my table it is just showing the first 5 rows
in this image, you can easily see that just names are showing on the table, and in the console, there are two arrays showing, the first one occurred when the page was loaded and the second one occurred when I change my pagination, check the pagination in the page.
here is my table code
products.map((row) => (
<TableRow key={row.name}>
<TableCell component="th" scope="row">
{row.name}
</TableCell>
<TableCell style={{ width: 160 }} align="right">
{row.calories}
</TableCell>
<TableCell style={{ width: 160 }} align="right">
{row.fat}
</TableCell>
</TableRow>
))}
here i am fetching data
const dispatch = useDispatch();
const {loading, products, error, productCount} = useSelector(state => state.products);
useEffect(()=>{
dispatch(getProducts());
},[dispatch])
I am also attaching the material-UI link here which I am using, I just simple copied and paste code the
on the page of material-UI, you can see Custom pagination actions
https://mui.com/components/tables/#main-content

For Pagination, you can use DataGrid in MUI,
First, you have to define a constant for columns naming like below then use the MUI DataGrid
const columns = [
{ field: "name", headerName: "Name" },
{ field: "calories", headerName: "Calories" },
{ field: "fat", headerName: "Fat" },
]
<DataGrid
rows={products}
columns={columns}
pageSize={5}
rowsPerPageOptions={[5, 10, 15]}
checkboxSelection={false}
/>
Here Products is the list you are providing the page to render.
And you have to handle the listing rows pagination by Top, Skip Strategy

Related

How to make the Material UI's table header sticky in relation to an outer scroll?

I have a page that uses Material UI's table and I need to get the sticky header working when the entire page is scrolled, since the table must be allowed to take as much vertical space as it needs, like in this pure HTML+CSS example.
I couldn't manage to do this with MUI's table, though. How can it be achieved?
demo
Set overflowX to initial on TableContainer
...
<TableContainer style={{ overflowX: "initial" }}>
...
Read more on this link
Related issue
Wanted mui table with sticky header & rotated to -45 degree angle
Below din't work:
Wasted 3 hours to make stickyHeader work
<Table stickyHeader ...
Below worked:
Apply head1 CSS for TableHead:
Apply rotatedContent1 CSS for TableCell's child (NOTE: child):
Full code is something like below:
const TableStyles = theme => ({
head1: {
zIndex: 3,
position: 'sticky',
top: '0px',
},
rotatedContent1: {
transform: 'rotate(270deg)',
}
})
const Component1 = ({ classes }) => {
return (
<React.Fragment>
<Table>
<TableHead className={classes.head1}>
<TableCell>
<div className={classes.rotatedContent1}> header value </div>
</TableCell>
</TableHead>
<TableBody>
<TableRow>
<TableCell> value </TableCell>
</TableRow>
</TableBody>
</Table>
</React.Fragment>
)
}
export default withStyles(TableStyles)(Component1)

Material Table React Custom Row Rendering

How to render custom nodes from data in Material Table react
( Ex: I want to show rows only with particular filed value>0)
If I understood your question correctly, you need to show a row in the table based on your condition. You can add that same condition while preparing the data.
I have attached a sandbox for rendering the same. I took a boolean value to display the row or not in the table.
Something like this.
<>
{row?.showValue && (
<TableRow
key={row.name}
sx={{ "&:last-child td, &:last-child th": { border: 0 } }}
>
<TableCell component="th" scope="row">
{row['property']}
</TableCell>
</TableRow>
)}
</>;

How can I populate a material ui tree with data in React

I'm using React and meterial-ui. The two components from material ui I am using are Collapsible Table and a Tree view inside it.
The data structure goes like this:
Projects -> workpackages -> work packages within work packages
So I list my projects in my table, then when you click the collapsible arrow it drops down and displays the work packges within that project in a tree, now I'm trying to figure out how to display sub trees within the tree if a work package has more work packages within it.
My goal is to populate the tree with some hardcoded data, then if it has another child object of data within it, create a sub tree.
I currently have it to where the tree is displaying within the table, but I can't figure out how to display a sub tree.
Columns for my table:
const columns = [
{ field: "project", title: "Project" },
{ field: "projectManager", title: "Project Manager" },
{ field: "startDate", title: "Start Date" },
{ field: "endDate", title: "End Date" },
];
Projects data for the table:
const projects = [
{
title: "Build Backend",
projectManager: "Bruce",
startDate: "12/12/12",
endDate: "12/12/16"
},
{
title: "Build Frontend",
projectManager: "Medhat",
startDate: "11/11/22",
endDate: "12/12/25"
}
]
My Table:
export default function CollapsibleTable() {
return (
<TableContainer component={Paper}>
<Table aria-label="collapsible table">
<TableHead>
<TableRow>
<TableCell />
{columns.map((column) => (
<TableCell key={column.field}>
<Typography variant="body1">
<strong>{column.title}</strong>
</Typography>
</TableCell>
))}
</TableRow>
</TableHead>
<TableBody>
{projects.map((row) => (
<Row key={row.title} row={row} />
))}
</TableBody>
</Table>
</TableContainer>
);
}
My work packages data, the first one has a sub work package within it 'package':
const workPackages = [
{
title: "Setup Database",
startDate: "01/12/22",
package: {
title: "Create Crud"
}
},
{
title: "Install express",
startDate: "01/11/12"
}
]
Table rows within my table that have the tree view:
function Row(props) {
const { row } = props;
const [open, setOpen] = React.useState(false);
return (
<React.Fragment>
<TableRow sx={{ '& > *': { borderBottom: 'unset' } }}>
<TableCell>
<IconButton
aria-label="expand row"
size="small"
onClick={() => setOpen(!open)}
>
{open ? <KeyboardArrowUpIcon /> : <KeyboardArrowDownIcon />}
</IconButton>
</TableCell>
<TableCell component="th" scope="row">
{row.title}
</TableCell>
<TableCell >{row.projectManager}</TableCell>
<TableCell >{row.startDate}</TableCell>
<TableCell >{row.endDate}</TableCell>
</TableRow>
<TableRow>
<TableCell style={{ paddingBottom: 0, paddingTop: 0 }} colSpan={6}>
<Collapse in={open} timeout="auto" unmountOnExit>
<TreeView
aria-label="file system navigator"
defaultCollapseIcon={<ExpandMoreIcon />}
defaultExpandIcon={<ChevronRightIcon />}
sx={{ height: 240, flexGrow: 1, maxWidth: 400, overflowY: 'auto' }}
>
{workPackages.map((tree) => (
<TreeItem label={tree.title}>
</TreeItem>
))}
</TreeView>
</Collapse>
</TableCell>
</TableRow>
</React.Fragment>
);
}
How it looks currently:
Table
How do I check and add a subtree to the tree for that package?
Read material ui docs (Multi selection section). They show how to add a tree in another one (subtree).

Is it possible to add "Sub-Headers/Columns" to a Material Table Table in React?

I am creating a table for larger more complex data sets using react and the material-table library.
Is there a way to add sub-header/columns to the material-table?
So: title
title1 | title2
I had the same problem few months ago. I only could achieve what I wanted by overriding the Header component of the material-table.
components={{ Header: props => (
<TableHead {...props}>
<TableRow>
<TableCell rowSpan={2}> Column 1 </TableCell>
<TableCell colSpan={2}> Column 2 </TableCell>
<TableCell colSpan={2}> Column 3 </TableCell>
<TableCell rowSpan={2}> Column 4 </TableCell>
<TableCell rowSpan={2}> Action </TableCell>
</TableRow>
<TableRow>
<TableCell> Column 2 A </TableCell>
<TableCell> Column 2 B </TableCell>
<TableCell> Column 3 A </TableCell>
<TableCell> Column 3 B </TableCell>
</TableRow>
</TableHead>
), }}
If you are using the editable material-table u should have a tablecell called Action.
And when you are inserting the columns it should be as usual. For example:
columns = {
[{
field: "column1",
title: "Column 1"
},
{
field: "column2a",
title: "Column 2 A"
},
{
field: "column2b",
title: "Column 2 B"
},
{
field: "column3a",
title: "Column 3 A"
},
{
field: "column3b",
title: "Column 3 B"
},
{
field: "column4",
title: "Column 4"
},
]
}
You will have to style it using inline css and/or makeStyles (material ui) to make it look presentable.
Hope this helps. Please feel free to ask if you need more explainations.
Try with Column Grouping
You can group column headers by rendering multiple table rows inside a table head:
<TableHead>
<TableRow />
<TableRow />
</TableHead>

React Material UI conditional render table cell color

I am current using material ui to display some data in a table. I want to be able to change the color of the row when a string property in the response has information in it.
At the moment I can change the color of the row to a specific color but i cant do it conditional. Can anyone help me thanks.
<TableRow className={classes.row}>
</TableCell>
<TableCell component="th" id={labelId} scope="row" padding="none">
{row.title}
</TableCell>
</TableRow>
const useStyles = makeStyles((theme) => ({
root: {
width: '100%',
},
row:{
background:'red'
},
}));
I don't know how your data is coming in, so I will use the typical way in this demo.
<TableRow className={ String ? classes.row : classes.row2}>
</TableCell>
<TableCell component="th" id={labelId}
scope="row" padding="none">
{row.title}
</TableCell>
</TableRow>
const useStyles = makeStyles((theme) => ({
root: {
width: '100%',
},
row:{
background:'red'
},
row2: {
background: 'pink'
}
}));
Create a different styling and use a tarnary to set the className based on a condition.

Resources