如何在React中使用MUI v5数据表格(DataGrid)中删除列标题?

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

How to remove column header of MUI v5 datagrid in react ts?

问题

在React TypeScript中使用MUI datagrid,需要移除不再需要的datagrid列标题,需要移除标题,而不是添加此内容。我通过以下方式实现:

& .MuiDataGrid-columnHeaders, & .MuiDataGrid-columnHeader: {
   display: none;
}

标题不再可见,但仍然会留下一些空间,即使我调整了高度。如下图所示:

[![enter image description here][3]][3]


请注意,这是CSS样式代码,用于隐藏datagrid的列标题。

<details>
<summary>英文:</summary>

In React Typescript using MUI datagrid, need to remove column header which is no longer needed a datagrid without column header ,
[![enter image description here][1]][1]

need to remove header instead need to add this,

[![enter image description here][2]][2]


I did this by 
    &quot;&amp; .MuiDataGrid-columnHeaders, &amp; .MuiDataGrid-columnHeader&quot;: {
       display: &quot;hidden&quot;,
      },
the header is not visible any more still it leave some space even I adjust the height. Like this,
[![enter image description here][3]][3]


  [1]: https://i.stack.imgur.com/Urrey.png
  [2]: https://i.stack.imgur.com/NAnP0.png
  [3]: https://i.stack.imgur.com/cujCQ.png

</details>


# 答案1
**得分**: 0

```javascript
在 MUI v5 中,可以通过将 columnHeader 的显示设置为 none,并确保将其上边距设置为 0 在 virtualScroller 中来实现。

"& .MuiDataGrid-columnHeaders, & .MuiDataGrid-columnHeader": {
  display: "none",
},
"& .MuiDataGrid-virtualScroller": {
  marginTop: "0 !important",
},

并且设置 headerHeight={0},Header=&gt;(null)

&lt;DataGrid
  rows={files}
  columns={columns}
  pageSize={100}
  rowHeight={40}
  headerHeight={0}
  components={{
    Header: () =&gt; null,// 完全移除头部
    Header: () =&gt; (&lt;div&gt; X 上传失败&lt;/div&gt;),// 可以按您的喜好定制
  }}
/&gt;

如何在React中使用MUI v5数据表格(DataGrid)中删除列标题?

英文:

In MUI v5, It can be accomplish by making columnHeader display: none and its important to make its margin top to 0 in virtualScroller

&quot;&amp; .MuiDataGrid-columnHeaders, &amp; .MuiDataGrid-columnHeader&quot;: {
  display: &quot;none&quot;,
 }, 
&quot;&amp; .MuiDataGrid-virtualScroller&quot;: {
  marginTop: &quot;0 !important&quot;,
 },

And headerHeight={0} , Header=>(null)

&lt;DataGrid
      rows={files}
      columns={columns}
      pageSize={100}
      rowHeight={40}
      headerHeight={0}
      components={{
        Header: () =&gt; null,// to remove header completely
        Header:()=&gt;(&lt;div&gt; X Upload Failed&lt;/div&gt;),//can be customized of your choice
  }}
/&gt;

如何在React中使用MUI v5数据表格(DataGrid)中删除列标题?

huangapple
  • 本文由 发表于 2023年6月13日 16:38:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76463098.html
匿名

发表评论

匿名网友

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

确定