Setting setSelected to true in multiple ag-grids seems to only work for the final grid

Currently, I am using a for loop to iterate over an array and dynamically creating ag-grid based on its content. With the data available, I am preselecting certain rows in the ag-grid.

For the gridReady method:

  onGridReady(event) {
    this.resultsArray.forEach(result => {
      result.gridOption.api = event.api;
      result.gridOption.columnApi = event.columnApi;  
    });
      event.api.forEachNode((node) => {
      if (node.data?.selected) {
        node.setSelected(true);
      }
    });
  }

The resultsArray holds the data and grid options for each grid. If a user deselects rows in both grids and then clicks on reset, only the last grid is being reset.

For the reset method:

  onReset(){
      this.resultsArray.forEach(result => {     
      result.gridOption.api.forEachNode((node) => {
      if (node.data?.selected) {
        node.setSelected(true);
      }
    });
    });
  }

View my stackblitz demo here.

Any suggestions on how to reset every grid, not just the last one?

Answer №1

When working with the grid API and the onGridReady method, it's important to note that you may be overriding the API unintentionally. Be mindful of how you set the API to avoid confusion.

To properly set the API:

Include an identifier, such as groupName, in the gridReady method to specify which API you intend to use:

<!-- src/app/my-grid-application/my-grid-application.component.html -->
<div>
    <button (click)="onReset()">Reset</button>
</div>

<div *ngFor="let result of resultsArray; let resultIndex = index">
    <div>{{result.groupName}}</div>
    <ag-grid-angular #agGrid style="width: 80%; height: 80%;" class="ag-theme-fresh" [gridOptions]="result.gridOption"
        [defaultColDef]="defaultColDef" [columnDefs]="myColumnDefs" (gridReady)="onGridReady($event, result.groupName)"
        [rowSelection]="rowSelection" [rowData]="result.data" [rowMultiSelectWithClick]="true">
    </ag-grid-angular>
</div>

Implement a check within an if statement:

onGridReady(event, groupName) {
    this.resultsArray.forEach(result => {
      if(result.groupName == groupName) {
        result.gridOption.api = event.api;
        result.gridOption.columnApi = event.columnApi;
        result.gridOption.api.sizeColumnsToFit(); 
      }
    });
      event.api.forEachNode((node) => {
      if (node.data?.selected) {
        node.setSelected(true);
      }
    });
  }

For a visual example, check out this stackblitz project.

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

Angular's focus function poses certain challenges that need to be addressed

I am seeking assistance as a new programmer facing a beginner's challenge. I have two inputs and I would like to enter a series of numbers in the first input (DO) and have them automatically populate in the second input (communal code). Inputs: http ...

Expanding the property of an established type to a nested type

Let's talk about titles. Well, maybe not this one. I tried to come up with a good title, but it didn't work out as planned. In my coding journey, I have created a cool interface called DefaultTheme: export interface DefaultTheme { colors: ...

A guide on determining the number of rows in an ag-grid with TypeScript and Protractor

I am currently working on extracting the number of rows in an ag-grid. The table is structured as follows: <div class="ag-body-container" role="presentation" style="height: 500px; top: 0px; width: 1091px;"> <div role="row" row-index="0" row-id="0 ...

What steps should I take to correctly identify the type in this specific situation?

Let's consider the function f, defined as follows: function f<T extends Fields = Fields>(props: Props<T>) { return null; } In this context, T represents a generic type that extends Fields. The concept of Fields is captured by the follow ...

The functionality of the KendoReact Grid for filtering and sorting is not functioning correctly when data is grouped

Once I group the data, the filter and sort functions in the KendoReact Grid stop working. Interestingly, if I bypass the grouping step and show the data without grouping, the filter and sort functions function perfectly fine. My main objective is to figu ...

Guide on Importing All Functions from a Custom Javascript File

As a beginner in learning Angular, I am faced with the task of converting a template into Angular. However, I am struggling to find a solution for importing all functions from a custom js file into my .component.ts file at once. I have already attempted t ...

What distinguishes between a public variable declared as var: any = []; versus a public variable declared as var: any[] =

I'm seeking clarification on the distinction between the following: public var: any = []; // versus public var: any[] = []; ...

How does one distinguish between the uses of "any" and "any[ ]"?

Exploring the Difference Between any and any[ ] An Illustrative Example (Functioning as Expected) variable1: any; variable2: any[]; this.variable1 = this.variable2; Another Example (Also Functioning as Intended) variable1: any; v ...

Retrieving Response Status Codes with Angular 4 and Express

When making GET requests to an Express REST API, I am setting res.status to 200 and returning data. However, in Angular, the response contains the data, but response.status always returns undefined. [UPDATE START] After trying Martin Adámek's sugge ...

Encountering ERR_INVALID_HTTP_RESPONSE when trying to establish a connection with a Python gRPC server using @bufbuild/connect

Attempting to establish a connection to a python grpc server developed with grpcio through a web browser using connect-query. Encountering an issue where upon making a request, an ERR_INVALID_HTTP_RESPONSE error is displayed in the browser. Below is the Re ...

Angular fails to detect newly added component

Just dipping my toes into the world of angular and decided to create my first app tour of heroes. However, I encountered a problem where angular wasn't recognizing a new component. Instead of throwing an error, it simply refused to render in the brows ...

Navigating within components using code is an essential skill when working with Vue Router

I am currently developing a Quasar application powered by Vue 3 with vue-router version 4 All my routes are properly configured and function well when navigating from a component template using <router-link to="/route">Go to route</rout ...

Remove all spaces from input fields in angular Typescript, excluding the enter key

I've encountered an issue where the code below removes all spaces, but it's also removing the enter key. Is there a way to remove only spaces and not affect the enter key? static stripDoubleSpaces(str: string): string { if (!!str) { ...

When I bring in a component from my personal library, it will assign the type "any" to it

I'm facing an issue when trying to import a component from my own library. The component within the library is actually sourced from another one (so I import the component, customize it, and then export the customized version). However, upon importi ...

Is it possible for TypeScript to preserve the return type while consolidating multiple classes or objects of functions in a reducer method?

Describing my issue with the title was challenging, but here it is: I have several objects that follow this structure: type TUtilityFunction = {[key: string]: <T>(a: T, b: any) => T} For example: class UtilityA{ DoSomeWork = function (arg1: So ...

Enhance your PrimeNG p-calendar by customizing the background-color of the dates

Hello, I am currently attempting to customize the appearance of the p-calendar, but I am struggling with changing the color of the displayed dates. Can anyone provide assistance? Thank you in advance. Below is my template: <div class="p-field p-co ...

Best practices for managing data loading with composition API and onBeforeRouteUpdate

I have a Vue 3 single-page component that contains the following script: export default defineComponent({ props: { id: String, }, setup(props) { const error = ref<boolean>(false) const thisCategory = ref<CategoryDetails>() ...

Unveiling a node through code in the angular tree component

Struggling to figure out a simple task using the angular-tree-component - given how amazing the component is, I'm sure it's something straightforward in the API that I am missing. I am trying to dynamically display a node by expanding all its pa ...

The module 'AppModule' is throwing an error with the import of 'MatDialogRef' which is causing unexpected value. To resolve this issue, make sure to include a @

I am currently facing an issue while trying to incorporate Angular Material into my Angular project. Despite successful compilation of the program, I encounter an error when running it in the browser. Uncaught Error: Unexpected value 'MatDialogRef&ap ...

Converting milliseconds into days, hours, minutes, and seconds using Angular

Currently, I am attempting to convert milliseconds to the format dd:hh:mm:ss. For example, given 206000 milliseconds. The desired format for this value would be: 00:00:03:26. However, when utilizing the following code: showTimeWithHour(milliSeconds: numb ...