如何使 MUI 中的子下拉菜单拉伸以填充表格单元父元素。

huangapple go评论68阅读模式
英文:

MUI How to make a child dropdown stretch to fill a table-cell parent element

问题

我有一个具有table-cell显示的父元素,以及一个子下拉元素,我希望它能够拉伸以填充父元素内的可用空间。我尝试将子元素的宽度设置为100%,或者尝试调整显示/定位,但它并没有填充整个空间。

这是我尝试做的一个示例:
https://codesandbox.io/s/serene-hermann-b61vwr?file=/src/App.js

如你所见,下拉框的大小与表格单元格不一样,看起来很奇怪,我该如何修复它?

英文:

I have a parent element with a display of table-cell, and a child dropdown element that I want to stretch to fill the available space within the parent element. I've tried setting the child element's width to 100%, or playing with the display/positioning but it's not filling the entire space.

Here's a sandbox of what I'm trying to do:
https://codesandbox.io/s/serene-hermann-b61vwr?file=/src/App.js

as you can see the dropdown size is not the same as the table cell and it looks weird, how can I fix it?

答案1

得分: 1

这是关于表头单元格存在填充(padding)的问题,这与你的下拉菜单效果不太兼容。你需要对包含下拉菜单的表格单元格标题应用不同的样式。

使用你的代码可能会找到更加优雅的解决方案,但这应该可以帮助你入门。现在下拉菜单将占据其单元格的整个宽度。这里有一个示例

从 StyledTableCell 克隆

const StyledTableDropdownCell = styled(TableCell)(({ theme }) => ({
  `&.${tableCellClasses.head}`: {
    backgroundColor: theme.palette.primary.main,
    color: theme.palette.common.white,
    borderBottom: 0,
    border: "1px solid black",
    align: "center",
    padding: 0 // <--- 不再有表头单元格的填充
  },
  `&.${tableCellClasses.body}`: {
    fontSize: 14
  }
}));

table.js

<StyledTableRow>
  {tableHeadData.map(({ label, align, rowSpan, colSpan }, index) => {
    console.log({ label, index });
    if ([0,1].includes(index)) { // &lt;--- 这部分可以更好地完成
      return (
        <StyledTableDropdownCell
          key={index}
          align={align}
          rowSpan={rowSpan}
          colSpan={colSpan}
        >
          {label}
        </StyledTableDropdownCell>
      );
    }
    return (
      <StyledTableCell
        key={index}
        align={align}
        rowSpan={rowSpan}
        colSpan={colSpan}
      >
        {label}
      </StyledTableCell>
    );
  })}
</StyledTableRow>
英文:

The issue is the table header cells have padding which doesn't work well with your dropdowns. You essentially need to apply a different style to the table cell headers that contain dropdowns.

You may find a more elegant solution with your code but this should help you get started. Now the dropdowns will take the full-width of their cells. Here's an example:

Cloned from StyledTableCell:

const StyledTableDropdownCell = styled(TableCell)(({ theme }) =&gt; ({
  [`&amp;.${tableCellClasses.head}`]: {
    backgroundColor: theme.palette.primary.main,
    color: theme.palette.common.white,
    borderBottom: 0,
    border: &quot;1px solid black&quot;,
    align: &quot;center&quot;,
    padding: 0 // &lt;--- No more table header cell padding
  },
  [`&amp;.${tableCellClasses.body}`]: {
    fontSize: 14
  }
}));

table.js

&lt;StyledTableRow&gt;
  {tableHeadData.map(({ label, align, rowSpan, colSpan }, index) =&gt; {
    console.log({ label, index });
    if ([0,1].includes(index)) { // &lt;--- this could be done better
      return (
        &lt;StyledTableDropdownCell
          key={index}
          align={align}
          rowSpan={rowSpan}
          colSpan={colSpan}
        &gt;
          {label}
        &lt;/StyledTableDropdownCell&gt;
      );
    }
    return (
      &lt;StyledTableCell
        key={index}
        align={align}
        rowSpan={rowSpan}
        colSpan={colSpan}
      &gt;
        {label}
      &lt;/StyledTableCell&gt;
    );
  })}
&lt;/StyledTableRow&gt;

huangapple
  • 本文由 发表于 2023年2月19日 20:02:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500009.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定