Angular-Ag-Grid - Dynamically add columns


Helen

In 2 cases, I need to add some columns to Ag-grid. In other cases, I just need the base column.

So in the constructor, I declare the grid like this:

this.gridOption.columnDefs = [
  {
        headerName: 'Admission date',
        field: 'admissionPlannedDate',
        cellRendererFramework: DateCellRendererComponent,
        cellRendererParams: (params) => {
          return (params.data.admissionPlannedDate ? {dateFormat: 'dd.MM.yyyy - HH:mm'} : {dateFormat: ' '});
        },
        cellStyle: function (params) {
          return (params.data.admissionPlannedDate < new Date() ? {color: 'red'} : {});
        }
      },
      {
        headerName: 'Lastname',
        field: 'lastName',
        cellStyle: function (params) {
          return (params.data.edsId === null ? {color: 'orange'} : {});
        }
        },
      },
      {
        headerName: 'Sex',
        field: 'sex',
      },
      {
        headerName: 'Birthdate',
        field: 'birthDate',
        cellRendererFramework: DateCellRendererComponent,
        cellRendererParams: (params) => {
            return (params.data.birthDate ? {dateFormat: 'dd.MM.yyyy'} : {dateFormat: ' '});
        },
      },
      {
        headerName: 'Localisation',
        field: 'localisation',
      }
];

Then in my ngOnInit, in some cases, I need to add columns to my ag-grid.

I have tried the following:

 this.gridOption.columnDefs.push(
                {
                  headerName: 'Block',
                  field: 'block',
                }, {
                  headerName: 'SDS/Hosp',
                  field: 'sdsOrHosp'
                }
              );
console.log(this.gridOption); //I see the new columns here so the add worked but i don't see them visual in my grid

also tried

 this.gridOption.columnDefs.push({ headerName: 'Bloc', field:'block'});
 this.gridOption.columnDefs.push({ headerName: 'SDS/Hosp', field:'SDSorHosp'});

Does anyone have an idea? thanks

non-nail

You can't just push the new value to columnDefs, I mean you can, but the flow can't be the ag-gridcase.

So to achieve your goal (add\remove dynamically columnDefs) you need to use the methodsetColumnDefs(colDefs)

setColumnDefs(colDefs)Called to set a new column definition in the grid. The grid will redraw all column headers and then all rows.

So logically you just need to create new column array and then callthis.gridOption.api.setColumnDefs(...)

Related


Angular-Ag-Grid - Dynamically add columns

Helen In 2 cases, I need to add some columns to Ag-grid. In other cases, I just need the base column. So in the constructor, I declare the grid like this: this.gridOption.columnDefs = [ { headerName: 'Admission date', field: 'admissionPlanned

How to dynamically add custom columns in Ag-Grid

shivek parmar I am stacking to dynamically add custom columns in ag-grid. I have tried the following methods. additionalCol = { field:'SOME RANDOM', headerName: 'SOME RANDOM' }; $scope.gridOptions.columnApi.addValueColumn(additionalCol); I also tried $scope.c

How to dynamically add custom columns in Ag-Grid

shivek parmar I am stacking to dynamically add custom columns in ag-grid. I have tried the following methods. additionalCol = { field:'SOME RANDOM', headerName: 'SOME RANDOM' }; $scope.gridOptions.columnApi.addValueColumn(additionalCol); I also tried $scope.c

How to dynamically add custom columns in Ag-Grid

Shivek Parma I am stacking to dynamically add "custom" columns in ag-grid. I have tried the following methods. additionalCol = { field:'SOME RANDOM', headerName: 'SOME RANDOM' }; $scope.gridOptions.columnApi.addValueColumn(additionalCol); I also tried $scope.

How to dynamically add custom columns in Ag-Grid

Shivek Parma I am stacking to dynamically add "custom" columns in ag-grid. I have tried the following methods. additionalCol = { field:'SOME RANDOM', headerName: 'SOME RANDOM' }; $scope.gridOptions.columnApi.addValueColumn(additionalCol); I also tried $scope.

How to dynamically add custom columns in Ag-Grid

shivek parmar I am stacking to dynamically add custom columns in ag-grid. I have tried the following methods. additionalCol = { field:'SOME RANDOM', headerName: 'SOME RANDOM' }; $scope.gridOptions.columnApi.addValueColumn(additionalCol); I also tried $scope.c

How to dynamically add custom columns in Ag-Grid

shivek parmar I am stacking to dynamically add custom columns in ag-grid. I have tried the following methods. additionalCol = { field:'SOME RANDOM', headerName: 'SOME RANDOM' }; $scope.gridOptions.columnApi.addValueColumn(additionalCol); I also tried $scope.c

Dynamically add grid columns

Olaru Mircea I created a form with a single data source: InventJournalTable. I also added a grid on it and added two fields to the datasource: JournalTypeandJournalId This ActionPanehas a button and in its click event handler, I'm trying to do the following: 1

Dynamically add grid columns

Olaru Mircea I created a form with a single data source: InventJournalTable. I also added a grid on it and added two fields to the datasource: JournalTypeandJournalId This ActionPanehas a button and in its click event handler, I'm trying to do the following: 1

Kendo UI for Angular2 - How to dynamically add columns to grid

Jarreg For Grid compomemet, if I already have several columns predefined in html, but the result set can have many other columns on the predefined columns, how can I add these columns dynamically? After I retrieve the data, I don't know what these extra column

Kendo UI for Angular2 - How to dynamically add columns to grid

Jarreg For Grid compomemet, if I already have several columns predefined in html, but the result set can have many other columns on the predefined columns, how can I add these columns dynamically? After I retrieve the data, I don't know what these extra column

Kendo UI for Angular2 - How to dynamically add columns to grid

Jarreg For Grid compomemet, if I already have several columns predefined in html, but the result set can have many other columns on the predefined columns, how can I add these columns dynamically? After I retrieve the data, I don't know what these extra column

Kendo UI for Angular2 - How to dynamically add columns to grid

Jarreg For Grid compomemet, if I already have several columns predefined in html, but the result set can have many other columns on the predefined columns, how can I add these columns dynamically? After I retrieve the data, I don't know what these extra column

Kendo UI for Angular2 - How to dynamically add columns to grid

Jarreg For Grid compomemet, if I already have several columns predefined in html, but the result set can have many other columns on the predefined columns, how can I add these columns dynamically? After I retrieve the data, I don't know what these extra column

Dynamically add rows/columns to grid

water quality I have a child UserControlconsisting of multiple textboxes, labels, images, etc. I need to populate this user control in a "parent user" control that has a grid. The number of child user controls that need to be populated in the parent user contr

Angular 6 - Add new row in AG grid

username I want to add a new element in AG Grid. I have the following model: export class PersonModel { cars: CarModel[]; } AG Grid has an rowDataarray of Cars in my model. But this array is unobservable. Now I want to add a new car when the button is click

Angular 6 - Add new row in AG grid

username I want to add a new element in AG Grid. I have the following model: export class PersonModel { cars: CarModel[]; } AG Grid has an rowDataarray of Cars in my model. But this array is unobservable. Now I want to add a new car when the button is click

Angular 6 - Add new row in AG grid

username I want to add a new element in AG Grid. I have the following model: export class PersonModel { cars: CarModel[]; } AG Grid has an rowDataarray of Cars in my model. But this array is unobservable. Now I want to add a new car when the button is click

Angular 6 - Add new row in AG grid

username I want to add a new element in AG Grid. I have the following model: export class PersonModel { cars: CarModel[]; } AG Grid has an rowDataarray of Cars in my model. But this array is unobservable. Now I want to add a new car when the button is click

How to dynamically add row styles? -Ag-grid

knowledge I want to dynamically change the style of rows in ag-grid. What I'm trying to achieve is that if the user has edited the background color of the ag-grid row, the row changes. cellValueChanged(event) { //Add it only if the old value has changed if

Dynamically add column to ag-grid from http request

G_Yahia I'm trying to add a dynamic column to ag-grid waiting for a response from an http request. Fixed columns are rowData: Task[]; gridOptions: GridOptions; domLayout = "autoHeight"; columnDefs = [ { headerName: 'Titre', field: 'name', sortable: tru

How to dynamically add row styles? -Ag-grid

knowledge I want to dynamically change the style of rows in ag-grid. What I'm trying to achieve is that if the user has edited the background color of the ag-grid row, the row changes. cellValueChanged(event) { //Add it only if the old value has changed if

Dynamically change headerName ag-grid Angular 2?

Valitskiy Dmitriy I have the following code: public fy: string = "17"; let months = ['July', 'August', 'September', 'October', 'November', 'December', 'January', 'February', 'March', 'April', 'May', 'June',]; function headerCellRendererFunc(year, para

How to dynamically align ag-grid-angular headers

Vikas Nale I have an ag-grid with angular 6 and dynamically appended columns and data. Now I want to align the headers (parent and child in case of multiple headers) dynamically. Like the first column header should be left aligned and other headers should be c

Dynamically change headerName ag-grid Angular 2?

Valitskiy Dmitriy I have the following code: public fy: string = "17"; let months = ['July', 'August', 'September', 'October', 'November', 'December', 'January', 'February', 'March', 'April', 'May', 'June',]; function headerCellRendererFunc(year, para

Dynamically change headerName ag-grid Angular 2?

Valitskiy Dmitriy I have the following code: public fy: string = "17"; let months = ['July', 'August', 'September', 'October', 'November', 'December', 'January', 'February', 'March', 'April', 'May', 'June',]; function headerCellRendererFunc(year, para

How to dynamically align ag-grid-angular headers

Vikas Nale I have an ag-grid with angular 6 and dynamically appended columns and data. Now I want to align the headers (parent and child in case of multiple headers) dynamically. Like the first column header should be left aligned and other headers should be c

How to dynamically align ag-grid-angular headers

Vikas Nale I have an ag-grid with angular 6 and dynamically appended columns and data. Now I want to align the headers (parent and child in case of multiple headers) dynamically. Like the first column header should be left aligned and other headers should be c

How to dynamically align ag-grid-angular headers

Vikas Nale I have an ag-grid with angular 6 and dynamically appended columns and data. Now I want to align the headers (parent and child in case of multiple headers) dynamically. Like the first column header should be left aligned and other headers should be c