英文:
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 
    "& .MuiDataGrid-columnHeaders, & .MuiDataGrid-columnHeader": {
       display: "hidden",
      },
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=>(null)
<DataGrid
  rows={files}
  columns={columns}
  pageSize={100}
  rowHeight={40}
  headerHeight={0}
  components={{
    Header: () => null,// 完全移除头部
    Header: () => (<div> X 上传失败</div>),// 可以按您的喜好定制
  }}
/>
英文:
In MUI v5, It can be accomplish by making columnHeader display: none and its important to make its margin top to 0 in virtualScroller
"& .MuiDataGrid-columnHeaders, & .MuiDataGrid-columnHeader": {
  display: "none",
 }, 
"& .MuiDataGrid-virtualScroller": {
  marginTop: "0 !important",
 },
And headerHeight={0} , Header=>(null)
<DataGrid
      rows={files}
      columns={columns}
      pageSize={100}
      rowHeight={40}
      headerHeight={0}
      components={{
        Header: () => null,// to remove header completely
        Header:()=>(<div> X Upload Failed</div>),//can be customized of your choice
  }}
/>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论