How can I center the text in the headers for an AG-grid control?

前端 未结 9 1172
长情又很酷
长情又很酷 2021-02-19 00:22

How can I center the text in the headers for an AG-grid control? I have tried using cellstyle and cellclass in the column definition but that did not work. Thank you

相关标签:
9条回答
  • 2021-02-19 01:04

    Assign a class to the cell as below:

    gridOptions = {
      ...
      columnDefs: [
        ...
            { headerName: "field1", field: "field1", cellClass: "grid-cell-centered"}
        ...
      ]
    }
    

    css file:

    .grid-cell-centered {
      text-align: center;
    }
    
    0 讨论(0)
  • 2021-02-19 01:08

    For (independent) customization of each header, you can make a custom header component: https://www.ag-grid.com/javascript-grid-header-rendering/

    0 讨论(0)
  • 2021-02-19 01:09

    A more complete solution I've been using for a while now, is to create a column definition helper like this:

    export const columnCentered = {
      headerClass: 'text-center',
      cellStyle: {
        textAlign: 'center'
      }
    }
    

    then add the following to your style.scss

    .ag-header-cell.text-center {
      .ag-header-cell-label {
        justify-content: center;
      }
    }
    

    finally you can easily use it like this:

    columnDefs: [
      { field: 'normalCol' },
      { field: 'centeredColA' ...columnCentered },
      { field: 'centeredColB' ...columnCentered }
    ]
    
    0 讨论(0)
提交回复
热议问题