简体   繁体   English

Material UI Data Grid:有没有办法右对齐整列?

[英]Material UI Data Grid: is there a way to right align an entire column?

I'm new to Material UI and have been trying to figure this out.我是 Material UI 的新手,一直在努力解决这个问题。

In Material UI, is there a way to have two columns in a Data Grid, one left aligned and one right aligned?在 Material UI 中,有没有办法在数据网格中设置两列,一列左对齐,一列右对齐? So far I have been able to left and right align the headers, but the way I do it seems to leave the cells behind.到目前为止,我已经能够左右对齐标题,但我这样做的方式似乎将单元格留在了后面。

import * as React from "react";
import { DataGrid } from '@material-ui/data-grid';

const columns = [
    { field: 'id', headerName: 'Column 1', align: "left", headerAlign: "left", width: "50%" },
    { field: 'data', headerName: 'Column 2', align: "right", headerAlign: "right", width: "50%" },
  ];

const rows = [
  { id: 1, data: 'asdfasdf'},
  { id: 2, data: '12951d'}, ];


function DataTable () {

    return (
        <div style={{ height: 400, width: '100%' }}>
          <DataGrid  rows={rows} columns={columns} pageSize={5} />
        </div>
      );
    
}

export default DataTable;

As you can see, the way I did it was by setting each column width to 50%.如您所见,我的做法是将每列宽度设置为 50%。 This actually works for the headers, but the format is not carried over to the cells.这实际上适用于标题,但格式不会转移到单元格。 Is there a way I can do this?有什么办法可以做到这一点?

Sandbox: https://codesandbox.io/s/material-demo-forked-gcvi8?file=/demo.js沙盒: https://codesandbox.io/s/material-demo-forked-gcvi8?file=/demo.js

You can make use of custom CSS so that you can increase the width of the cascading columns to 100%.您可以使用自定义 CSS 以便将级联列的宽度增加到 100%。

.MuiDataGrid-renderingZone, .MuiDataGrid-root .MuiDataGrid-row {
  width: 100% !important;
}

I used !important for overriding the inline CSS added by ReactJS. There could be inbuilt solutions if you refer to the ReactJS documentation.我使用!important来覆盖由 ReactJS 添加的内联 CSS。如果您参考 ReactJS 文档,可能会有内置解决方案。

Codesandbox demo: https://j0geb.csb.app/ Codesandbox演示: https://j0geb.csb.app/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM