Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Three cards in a row instead of all cards in one column

I'm using react with material UI, I have 40 dynamic cards in an array and when I render them , I want to have 3 cards in a row and I get all the cards in one column. I'm using this card: https://codesandbox.io/s/r084q99q34

like image 990
ofirthedev Avatar asked Jul 30 '18 13:07

ofirthedev


3 Answers

Maybe you can use Flexbox ? I had the same problem and I resolved it thanks to FlexBox trough material ui. Also, be sure to use a material core version superior or equal to 4. Hope it helps !

import React from 'react'
import { makeStyles } from '@material-ui/core/styles'
import {
    Grid,
    Card,
    CardContent,
    Typography,
    CardHeader
} from '@material-ui/core/'

const useStyles = makeStyles(theme => ({
    root: {
        flexGrow: 1,
        padding: theme.spacing(2)
    }
}))

export default function AltCard() {
    const classes = useStyles()
    const data = [
        { quarter: 1, earnings: 13000 },
        { quarter: 2, earnings: 16500 },
        { quarter: 3, earnings: 14250 },
        { quarter: 4, earnings: 19000 }
    ]
    return (
        <div className={classes.root}>
            <Grid
                container
                spacing={2}
                direction="row"
                justify="flex-start"
                alignItems="flex-start"
            >
                {data.map(elem => (
                    <Grid item xs={12} sm={6} md={3} key={data.indexOf(elem)}>
                        <Card>
                            <CardHeader
                                title={`quarter : ${elem.quarter}`}
                                subheader={`earnings : ${elem.earnings}`}
                            />
                            <CardContent>
                                <Typography variant="h5" gutterBottom>
                                    Hello World
                                </Typography>
                            </CardContent>
                        </Card>
                     </Grid>
                ))}
            </Grid>
        </div>
    )
}
like image 130
Clément MARTZLOFF Avatar answered Nov 08 '22 06:11

Clément MARTZLOFF


You could use the Grid component for this, e.g.

<Grid container spacing={24}>
  <Grid item md={3}>
    <Demo />
  </Grid>
  <Grid item md={3}>
    <Demo />
  </Grid>
  <Grid item md={3}>
    <Demo />
  </Grid>
</Grid>

https://codesandbox.io/s/rzmz5jnom

like image 38
eronisko Avatar answered Nov 08 '22 06:11

eronisko


Material UI provides GridList and GridListTile components. That let you configure it pretty well.

Grid lets you define columns and cells. A tile takes up one slot and it keeps fitting them in columns and overflowing them automatically if required. You can read more in the official documents and take a look at their samples for inspiration.

let nums = Array.from(Array(40).keys());

  render(
    <GridList cols={3}>
      {nums.map(n => {
        return (
          <GridListTile key={n}>
            <Demo key={n} num={n} />
          </GridListTile>
        );
      })}
    </GridList>,
    rootElement
  );

See a forked sandbox: https://codesandbox.io/s/5yjpp339w4

If for some reason the inbuilt components do not suit your needs or if grid still confuses you, do take a look at their interactive layout examples

like image 43
dubes Avatar answered Nov 08 '22 05:11

dubes