Search code examples
angularag-grid-angular

Ag-Grid show a third column that I don't have


I'm working with Angular, and I started using the Ag-Grid.

I need to display a grid with two columns, but when I opened it, it shows a third separator like if there were 3 columns.

enter image description here

Even if I display data in the rows, there are 3 columns, this is the code.

gridView (HTML)

<ag-grid-angular style="width: 100%; height: 100px;" class="ag-theme-balham" [rowData]="rowData"
[columnDefs]="columnDefs" [animateRows]="true" [enableSorting]="true" [enableFilter]="true"
(gridReady)="onGridReady($event)">
</ag-grid-angular>

gridView (Ts)

export class QSubReviewerGridComponent implements OnInit {
    gridApi;
    gridColumnApi;
    defaultColDef;
    columnDefs;
    rowData= [];

    constructor() { 
        this.columnDefs = [
            { headerName: "Review Type", sortable: true, filter: true, field: 'reviewerType' },
            { headerName: "Reviewer", sortable: true, filter: true, field: 'reviewer' },
        ]
        this.defaultColDef = { filter: true };
    }
    ngOnInit() {
    }
    onGridReady(params) {
         this.gridApi = params.api;
         this.gridColumnApi = params.columnApi;
    }

}

I don't know if the Ag-grid shows 3 columns by default, I've used it before with more columns and it never showed an extra one.


Solution

  • I am thinking you have to call sizeColumnsToFit.

    Try:

    onGridReady(params) {
             this.gridApi = params.api;
             this.gridApi.sizeColumnsToFit();
             this.gridColumnApi = params.columnApi;
     }
    

    ================== Edit ============================

    To make it responsive, I would do the following:

    <ag-grid-angular 
        style="width: 100%; height: 100px;" 
        class="ag-theme-balham" 
        [rowData]="rowData"
        [columnDefs]="columnDefs" 
        [animateRows]="true" 
        [enableSorting]="true" 
        [enableFilter]="true"
        (gridReady)="onGridReady($event)"
        (gridSizeChanged)="onGridSizeChanged($event)"
    >
    </ag-grid-angular>
    ....
    onGridSizeChanged(params: GridSizeChangedEvent) {
      params.api.sizeColumnsToFit();
    }
    

    Now when you increase or decrease the size of the div holding the grid, it will be responsive.