简体   繁体   English

如何使用 React 更改 Material UI 表格单元格的宽度?

[英]How can I change the width of a table cell of Material UI with React?

I have the following problem, I try to increase the table cell width, but I have no idea how to do it.我有以下问题,我尝试增加表格单元格的宽度,但我不知道该怎么做。 I work with Typescript.我与 Typescript 合作。 I can only select the size for the TableCellProps and I can only set medium or small.我只能 select TableCellProps 的大小,我只能设置中或小。 Does anyone have a solution?有没有人有办法解决吗? I want to enlarge the blue colored table cell.我想放大蓝色表格单元格。 1 1

<TableRow>
  <TableCell align="left">address</TableCell> //sample Code, how i can set here the width
</TableRow>

As a second question, how can you limit the height of the whole table, so that you have to scroll horizontally and vertically in the table?作为第二个问题,你怎么能限制整个表格的高度,以至于你必须在表格中水平和垂直滚动?

You can simply add the width to the cell like this:您可以像这样简单地将宽度添加到单元格:

<TableCell width={300}>Dessert (100g serving)</TableCell>

This will make this cell 300 px wide.这将使该单元格宽 300 像素。

If you have not the material-ui installed, then you need to install it first as follows:如果您还没有安装 material-ui,则需要先安装如下:

// with npm
npm install @material-ui/core

// with yarn
yarn add @material-ui/core

then import it to the top of your file as follow:然后将其导入文件顶部,如下所示:

   import {          
      createStyles,
      Grid,
      makeStyles,          
      Table,
      TableBody,
      TableCell,
      TableContainer,
      TableHead,
      TableRow,
      Theme,
      withStyles,
   } from '@material-ui/core';

create a function like this and set the width value as you wish:像这样创建一个 function 并根据需要设置宽度值:

    const StyledTableCell = withStyles((theme: Theme) =>
  createStyles({
    head: {
      backgroundColor: theme.palette.common.black,
      color: theme.palette.common.white,
      width: '50%',
      fontSize: 20
    },
  }),
)(TableCell);

now look at the following example to see how you can use StyledTableCell in your code:现在查看以下示例,了解如何在代码中使用StyledTableCell

              <Table
            aria-label="collapsible table"
            aria-labelledby="tableTitle"
          >
            <TableHead>
              <TableRow>
                <TableCell />
                <TableCell>Document Title</TableCell>
                <StyledTableCell  align="right">Document Content</StyledTableCell>
              </TableRow>
            </TableHead>
            <TableBody>
              {rows.map((row) => (
                <Row key={row.documentId} row={row} />
              ))}
            </TableBody>
          </Table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改 Material-ui React 表中的表格单元格宽度 - How to change Table Cell width in Material-ui React table 如何在react +材料中赋予表格单元全宽? - how to give full width to table cell in react + material? 如何更改 Material UI Table 中特定单元格中的按钮颜色和名称 - How to change Button Color and Name in specific cell in Material UI Table React Material UI 表中的单元格引用 - Cell References in React Material UI Table 如何将我的 React class 更改为 function 以便我可以包含 Material UI 类? - How can I change my React class to a function so that I can include Material UI classes? 如何在使用材料 ui 表的反应中将超链接添加到表头列条目? - How can I add hyperlink to table head column entries in react using material ui table? React和material-ui / DatePicker:如何以编程方式更改为年份视图? - React and material-ui/DatePicker: how can I change to year view programmatically? 在 Material-UI React 中将鼠标悬停在复选框上时,如何更改复选框样式和图标? - How can I change the checkbox styles and icons when hover on it in Material-UI React? 如何在 Material-UI React 中更改 TextField 的边框半径? - How can I change the border radius of a TextField in Material-UI React? React Material-UI 如何添加与表格行单击分开的复选框 - React Material-UI How can I add a checkbox which is separate from table row click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM