Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to right align text in ag-grid for number

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?

like image 548
Ekta Avatar asked Sep 25 '20 08:09

Ekta


People also ask

How do you right align in Ag grid?

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.

How do I align text numbers?

Select the text box, shape, table cell, row, or column. In the Text pane of the Format inspector, click Style, then click the alignment buttons you want.


2 Answers

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.

like image 69
NicuVlad Avatar answered Sep 28 '22 19:09

NicuVlad


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' },
    ]
like image 41
dustydojo Avatar answered Sep 28 '22 20:09

dustydojo