The issue with ag-grid not displaying data when the column configurations are changed dynamically

I have been working with ag grid to display data in my component. I am fetching data through an API call and dynamically extracting the column names from the response to pass this information to the child component for display. However, the data is not showing up as expected.

Interestingly, when I use a static configuration stored in a variable, everything works fine.

Static Column Config =>

export let DETIALCONFIG= {
    columnDefs: [
        {
            headerName: 'VT Name',
            field: 'vtName',
            hide: false,
            minWidth: 235,
            serverFlag: true,
            tooltipFields: 'veName'
        },
        {
            headerName: 'Key',
            field: 'KEY',
            hide: false,
            serverFlag: true,
        },
        {
            headerName: 'ID',
            field: 'ID',
            hide: true,
            serverFlag: true,

        },
        // more column definitions...
    ],
    gridOptions: {
        domLayout: 'autoHeight',
        pagination: false,
        paginationPageSize: 6,
        rowSelection: 'multiple',
        // more grid options...
    }
}

Dynamic Logic

  private run = async (veName: string) => {
    let response: any
    try {
      // API call logic...
    } catch (err) {
      // error handling...
    }

When I try to dynamically display the columnConfig in the template within the gridOptions property, an additional columnDefs is being added every time. This issue does not occur with static behavior, and I believe this is where the problem lies. I would appreciate it if someone could help me figure out what I am doing wrong here. Thank you.

Answer №1

It seems that the issue lies in the code snippet below. When performing a map operation, it is crucial to assign the result to another array or the same array to ensure that the changes take effect.

private run = async (veName: string) => {
    let response: any
    try {
      response = await apiCall(veName, [], true)
      this.veTableData = [...response.data]
      const keys = Object.keys(this.veTableData[0])
      this.columnConfig = this.veDetailsGridConfig
      this.columnConfig.columnDefs = this.columnConfig.columnDefs.map((item: any) => { // This part has been modified!
        if(keys.includes(item.field)) {
          item.hide = false
        }
        return item;
      })
      console.log(this.columnConfig)
      this.tableHeader = veName.toUpperCase()
    } catch (err) {
      this.veTableData = []
      this.columnConfig = this.veDetailsGridConfig
    }

Typically, Aggrid may not reflect column changes when inputs are altered, necessitating a workaround like the one shown below.

this.gridApi.setColumnDefs([]);
this.gridApi.setColumnDefs(this.columnConfig.columnDefs);

If the column definitions are not being recognized, attempting array destructuring to create a new reference for the array might prompt the grid to detect the modifications!

this.columnConfig.columnDefs = [...this.columnConfig.columnDefs];

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Access the information from the text box and text area, then store it into a cookie using either jQuery or JavaScript

I need to save the text entered in a textbox within an iframe. How can I achieve this using jQuery or JavaScript? Any assistance would be greatly appreciated. ...

React modal not triggered on click event

As a newcomer to react, I am exploring a modal component import React, { useState, useEffect } from 'react'; import { Modal, Button } from "react-bootstrap"; function TaskModal(props) { return ( <Modal show={pro ...

Using jQuery to restrict the number of checked checkboxes based on a selected value - Here's how!

How can I restrict the number of checkboxes that can be checked based on the selected option from a dropdown menu? For example, selecting 'option1' should allow only 1 checkbox to be checked, 'option2' should allow 2 checkboxes, and so ...

Stagnant User Interface

My current project involves fetching data from a database in c# and passing it to Javascript using Ajax. I store this data in arrays and query the database every 3 seconds. The data is utilized to update a diagram, such as changing the color of the track. ...

AJAX list refresh, fetch additional items and tally

Looking for a solution to update the values of listUsernames and numUsernames after adding an item? Check out this scenario: <ul id='usernameList'> <li class='username'>John</li> <li class='username&apo ...

Conceal Navigation Panel while Scrolling Down, Reveal when Scrolling Up, Compatible with Chrome, Incompatible with Safari (on smartphones

I have implemented a code to hide my navbar when scrolling down and show it when scrolling up. This code works perfectly on desktop and in all browsers, including Chrome on mobile (iPhone). However, in Safari, the navbar sometimes shows, hides, and shows a ...

Encountering issues when attempting to compile in Angular due to a not assignable error while creating a new interface

Trying to create a new interface for my simple project but running into a compile error. Any ideas on what I did wrong? Here's the error message: C:/Users/vashon/Angular/Angular-GettingStarted-master/APM/src/app/products/product-list.component.ts (1 ...

Is it possible to test an Angular application using Protractor within an iframe that is hosted by a non-Angular

While trying to migrate a legacy app to Angular, we encountered an issue where the legacy app loads the new app in an iframe. Testing this integration with Protractor has proven challenging due to the fact that the legacy app is not built on Angular. If t ...

Explore visuals in the component repository

I am currently working on an Angular 7 component library and am facing some challenges in adding images for buttons and other elements. My project structure is organized as follows: projects components src lib myComponent assets ...

Tips for creating a seamless merge from background color to a pristine white hue

Seeking a seamless transition from the background color to white at the top and bottom of the box, similar to the example screenshot. Current look: The top and bottom of the box are filled with the background color until the edge https://i.stack.imgur.com ...

What is preventing Bootstrap properties from being applied to HTML elements appended to the DOM through JavaScript?

Currently, I am immersed in a project that utilizes Bootstrap to generate various nested components styled in accordance with Bootstrap's class system. I triumphantly crafted a card component using Bootstrap, but encountered a challenge when attemptin ...

Customize the text for the material icon

Can I customize an icon by using the following code: import FiberNewIcon from "@mui/icons-material/FiberNew"; Is there a way to add custom text to the icon? ...

Executing NPM scripts in bin directory upon import

My TypeScript 4.8.4 library is being packaged with npm. Within my package.json, I have a "bin" section that includes several utility scripts for consumers of the package. "bin": { "generate-docs": "./docs/docs.py&qu ...

Securing AJAX Requests: Encrypting GET and POST Requests in JavaScipt using Node.js

Looking for a way to secure ajax post and get requests using JavaScript. The process would involve: Server generates private and public key upon request Server sends the public key to client Client encrypts data with public key Server decrypts data wit ...

When utilizing Typescript to develop a form, it is essential to ensure that the operand of a 'delete' operator is optional, as indicated by error code ts(279

Can someone help me understand why I am encountering this error? I am currently working on a form for users to submit their email address. export const register = createAsyncThunk< User, RegisterProps, { rejectValue: ValidationErrors; } > ...

Tips for incorporating an if statement into embedded HTML using JavaScript in order to reveal a CSS class

How can I dynamically display different classes based on the presence of data in a JavaScript array? For example, if there is responseData.title, show the class 'grey'; otherwise, show the class 'white'. This is what I am attempting t ...

How to retrieve the value of a SelectField component within a constant using Material-UI

I am currently facing an issue with my Drawer component that contains a SelectField with some fields. I am struggling to get the value and implement the onChange functionality of the Field. Below is the snippet of my code: const DrawerCargoAddItem = (p ...

Issue with Loading Data on AJAX Causing Scrolling Difficulties

Currently, I am in the midst of website development using PHP, MySQL, and JavaScript (JQuery + Ajax). One issue that has arisen is with the customer scroll function and scrollbars. When loading data via ajax, the scroll function is generating numerous erro ...

JavaScript CompleteLink message

Hey there, I've come across a JavaScript code for a countdown timer but need some assistance with it. The issue I'm facing is that within the code, there's a line that displays a message to the user when the timer reaches zero. I want to kn ...

Utilizing Vue.js i18n for a multi-line text display

Currently, I am implementing i18n single file component in order to provide translation support for my application. In order to achieve this, I have been utilizing the i18n tag as shown below: <i18n> { "fr": { "text": "Lore ...