reactjs 如何安排网格为后续布局?

vdgimpew  于 2022-12-22  发布在  React
关注(0)|答案(1)|浏览(109)

概述

  • 我正在尝试排列MUI网格上的项目。
  • 有多个动态的行和列
  • 每行可以有固定的第一列,其后可以有任意数量的列(动态)
  • 列的动态部分可相对于每行滚动
    要求
  • 由于每行的第一列是固定的,而其他列对于所有行都是可滚动的,因此我想将这些可滚动列分组为一个组,并实现一个滚动条
    当前

预期值

当前实施情况

{<Grid container>
      {rowArray.map(x => (
        <>
          <Grid item>"Fixed Item"</Grid>
          {columnArray.map(x => (
            <Grid item>Dynamic items</Grid>
          ))}
        </>
      ))}
    </Grid>}
wgx48brx

wgx48brx1#

使用网格分割空间时,请始终先将屏幕分割为,然后将每列分割为几行

import { Box, Grid } from "@mui/material";

const fixedItems = [
  "row 1's fixed item",
  "row 2's fixed item",
  "row 3's fixed item",
];
const dynamicItems = [
  ["row 1 dynamic item 1"],
  [
    "row 2 dynamic item 1",
    "row 2 dynamic item 2",
    "row 2 dynamic item 3",
    "row 2 dynamic item 4",
    "row 2 dynamic item 5",
  ],
  ["row 3 dynamic item 1", "dynamic item 2"],
];

const Item = (props) => {
  return (
    <Box
      sx={{
        display: "inline-block",
        backgroundColor: "cyan",
        borderRadius: 2,
        textAlign: "center",
        width: "300px",
        mx: 1,
      }}
    >
      {props.name}
    </Box>
  );
};

function Test() {
  return (
    <Box
      sx={{
        width: "90%",
        direction: "ltr",
        mx: "auto",
      }}
    >
      <Grid container spacing={1} sx={{ border: "1px solid red" }}>
        <Grid
          item
          xs={3}
          container
          spacing={1}
          sx={{
            textAlign: "center",
            border: "1px solid yellow",
          }}
        >
          {fixedItems.map((item, index) => (
            <Grid item xs={12} key={index}>
              <Item name={item} />
            </Grid>
          ))}
        </Grid>
        <Grid
          item
          xs={9}
          container
          spacing={1}
          sx={{
            overflowX: "scroll",
            whiteSpace: "nowrap",
            border: "1px solid green",
          }}
        >
          {dynamicItems.map((item, index) => (
            <Grid item xs={12} key={index}>
              {item.map((innerItem, innerIndex) => (
                <Item name={innerItem} key={innerIndex} />
              ))}
            </Grid>
          ))}
        </Grid>
      </Grid>
    </Box>
  );
}
export default Test;

相关问题