17

If any cell in ag-grid has numeric value like decimal, int or numeric i want that cell to be right aligned.

this.defaultColumnDefs = {
      width: 75,
      exportColumn: true,
      type: 'numericColumn',
      cellClass(params) {
        return params.colDef.type === 'numericColumn' ? 'ag-numeric-cell' : '';
      },
      filterParams: {
        buttons: ['clear'],
      },
      menuTabs: ['filterMenuTab'],
      filter: 'agTextColumnFilter',
      resizable: true,
      sortable: true
    };

It used to work before now i have to go to each column and specify the type. Why doesn't it take the default type?

Ekta
  • 261
  • 1
  • 2
  • 11

6 Answers6

19

There is an easy solution provided by Ag-grid documentation:

columnDefs: [
        { headerName: 'Column A', field: 'a' },
        { headerName: 'Column B', field: 'b', type: 'rightAligned' },
    ]

Just add type: 'rightAligned' property.

NicuVlad
  • 2,491
  • 3
  • 28
  • 47
13

If you just want both the column header and cells to be right-aligned, you can use NicuVlad's answer:

columnDefs: [
        { headerName: 'Column A', field: 'a' },
        { headerName: 'Column B', field: 'b', type: 'rightAligned' },
    ]

However, if you just want the cells right-aligned and not the header:

columnDefs: [
        { headerName: 'Column A', field: 'a' },
        { headerName: 'Column B', field: 'b', cellClass: 'ag-right-aligned-cell' },
    ]

And if you want the header right-aligned and not the cells:

columnDefs: [
        { headerName: 'Column A', field: 'a' },
        { headerName: 'Column B', field: 'b', headerClass: 'ag-right-aligned-header' },
    ]
dustydojo
  • 449
  • 5
  • 14
6

Fairly straightforward...

var colDef = {
    name: 'No. Field',
    field: 'noField',
    cellStyle: function(params) {
        if (typeof params.value === 'number') {
            return {text-align: 'right'};
        } else {
            return null;
        }
    }
}

You can go through your entire set of your ColumDefinitions (ColDef) any apply that cellstyle to all your columns programmatically.

By the by, this comes from the official documentation of ag-grid (albeit somewhat changed)

ak.leimrey
  • 1,137
  • 2
  • 11
  • 25
  • I actually have code pasted in desc which used to work before i updated then it is not taking this style, I have updated my question – Ekta Sep 25 '20 at 09:53
4

The above solutions didn't work for me. (In AG Grid 27)

What did work was using:

cellStyle: {justifyContent: "flex-end"},
Ben in CA
  • 688
  • 8
  • 22
0
        cellStyle: { "direction": "rtl" }

Insert this in needed column definitions

-1

Combination of 2 answers above

Column definition

 <...>      
 {
    headerName: 'Foo',
    field: 'bar',
    type: 'rightAligned', // <----- that one
  },
 <...>      

Css

.ag-right-aligned-cell.ag-cell-value {
    justify-content: flex-end;
 }
Pipo
  • 4,653
  • 38
  • 47