Encountering "Undefined error" while using @ViewChildren in Angular Typescript

In my application, I am facing an issue with a mat-table that displays data related to Groups. The table fetches more than 50 rows of group information from a data source, but initially only loads the first 14 rows until the user starts scrolling down. Alongside this table, there is an array of checkboxes which dynamically display values. I use @ViewChildren to retrieve the checkbox values (true/false).

 @ViewChildren('groupCheckboxes') viewGroupCheckboxes: QueryList<CheckboxComponent>;

.....

Let me provide a scenario to illustrate the problem. Let's say we have groups ranging from agency A to Z, and data for groups A, B, Y are stored in the data source. When the page loads, checkboxes next to A, B, and Y should be already checked.

//In this example, when data for all groups A to Z has been loaded, the setupGroupCheckboxes() method will locate A, B, and Y in the data table and set their checkboxes to true.
setupGroupCheckboxes(): void {
    for (const refItem of this.preSelectedGroups){
      let matchingIndex = this.rowsGroups.findIndex(item => item.id === refItem.id);
      if (matchingIndex !== -1) {
        this.viewGroupCheckboxes.get(matchingIndex).checked = true;
        this.groupCheckboxArray[matchingIndex] = true;
        this.isGroupLoaded = true;
      } else {
        this.isGroupLoaded = false;
      }
    }
}

The issue arises after the initial form load where checkboxes beyond index 14 become inaccessible. For instance, trying to access the checkbox for Group Y at index 24 results in an undefined error because even though the group data is present in the array, the checkboxes for those groups (index 15 to 25) are not yet visible on the user's screen.

ERROR TypeError: Cannot set properties of undefined (setting 'checked').

I have explored various solutions but it seems that the number of rows initially loaded by the data table may be based on its height limitation (as reducing the height leads to fewer initial rows being loaded). Given that the current height of the data table cannot be further adjusted, I would appreciate any insights or ideas on how to overcome this challenge. Thank you!

Answer №1

Looks like the mat-table API I implemented automatically enabled the [virtualScroll] feature. Once I disabled it, the issue was fixed.

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

Ways to turn off logging for an Angular application using MSAL

I have integrated the Microsoft Authentication Library msal into my Angular application. However, when I deploy the production version of the app, I notice these logs appearing in the console. What is the best way to turn off this logging? https://i.sstat ...

What is the process for adding outer borders to an Angular Material table?

Even though I've tried it this way, it just won't cooperate This is the template: <table mat-table [dataSource]="dataSourceListadoEstados" class="mat-table-riesgo-generico"> <ng-container matColumnDef="descripcion_estado"> ...

Troubleshoot Angular2 modules efficiently using source mapping features

Is there a way to debug Angular2 module code using TypeScript source mapping in the browser? After installing my project with the "npm install" command following the steps in https://angular.io/guide/quickstart, I noticed that while source mapping is ava ...

Upgrading to Angular 14 has caused issues with component testing that involves injecting MAT_DIALOG_DATA

After upgrading Angular from 14.1.1 to 14.2.10 and Material from 14.1.1 to 14.2.7, a peculiar issue arose when running tests with the default Karma runner. I am at a loss as to what could have caused this problem, so any advice would be appreciated. The u ...

Apply CSS styling to the shadow root

In my preact project, I am creating a Shadow DOM and injecting a style element into the Shadow root using the following code: import style from "./layout/main.css"; loader(window, defaultConfig, window.document.currentScript, (el, config) => ...

Encountered an issue while trying to install and run an Angular app: Error code TS2694 indicates that the Namespace does not have an exported member '

Attempting to construct and execute this angular project. Post building the project, with npm i I encountered some warnings, but no errors were found. After executing ng serve -o, the following errors appeared. Moreover, when accessed through the browser, ...

Tips for automatically scrolling to the top of a Material2 Angular2 dialog box

I'm currently utilizing the Dialog box feature from https://material.angular.io. Specifically, I am facing an issue where when I try to position the dialog box using: config = {'width':'200px', 'height':'400px' ...

Unable to retrieve nested objects from HTTP Response

After receiving data from a HTTP Response, I am trying to access and display it in my template. However, despite storing the data into a component variable, I am encountering issues when trying to access specific properties of the object. { "files": [ ], ...

Is it possible to convert a DynamoDB AttributeMap type into an interface?

Assume I define a TypeScript interface like this: interface IPerson { id: string, name: string } If I perform a table scan on the 'persons' table in DynamoDB, my goal is to achieve the following: const client = new AWS.DynamoDB.Documen ...

Sending error messages from server to client (leveraging Express and Backbone)

I'm struggling with passing server error messages to a client after thrashing around for a while. Here's what I have on the server side (simplified): export function get(req: express.ExpressServerRequest, res: express.ExpressServerResponse) { ...

The module 'crypto-js' or its corresponding type declarations cannot be located

I have a new project that involves generating and displaying "QR codes". In order to accomplish this, I needed to utilize a specific encoder function from the Crypto library. Crypto While attempting to use Crypto, I encountered the following error: Cannot ...

What makes using the `@input` decorator more advantageous compared to the usage of `inputs:[]`

In defining an input on a component, there are two available methods: @Component({ inputs: ['displayEntriesCount'], ... }) export class MyTable implements OnInit { displayEntriesCount: number; Alternatively, it can be done like this ...

How can one click the button within the expanded dropdown while hovering over the navigation item in Angular and Bootstrap?

Issue Description: Upon hovering over a navigation item, the dropdown container is displayed but it's not clickable. Desired Behavior: Hovering over a navigation item should display the dropdown container and allow clicking on its contents. Furthermo ...

The z-index overlapping problem in webkit browsers is a result of Angular 7 animations

I have implemented staggered animations in my Angular 7 application to bring elements to life upon page load. However, I am facing a strange z-index problem with one of my components. Here is the animation code: @Component({ selector: 'track-page& ...

Ways to trigger the keyup function on a textbox for each dynamically generated form in Angular8

When dynamically generating a form, I bind the calculateBCT function to a textbox like this: <input matInput type="text" (keyup)="calculateBCT($event)" formControlName="avgBCT">, and display the result in another textbox ...

There is an issue with the Angular Delete request functionality, however, Postman appears to be

HttpService delete<T>(url: string): Observable<T> { return this.httpClient.delete<T>(`${url}`); } SettingsService deleteTeamMember(companyId: number, userId: number): Observable<void> { return this.httpService.delete< ...

Is there a way to halt the current traversal of visitEachChild in TypeScript Transformer API?

While navigating through each child node of a parent node using visitEachChild, is there a way to stop the process when I no longer wish to visit the subsequent child nodes? For example: Parent node Node 1 Node 2 <-- My target point. Node 3 Node 4 Nod ...

Error: Bootstrap CSS missing from app created with create-react-app-ts

I have a React application that was initially set up using create-react-app-ts. Although I have included bootstrap and react-bootstrap for navigation, the CSS styling from Bootstrap does not seem to be applied properly. The links do not display any styling ...

Ways to Halt observable.timer in Angular 2

As I work on Angular2's Component, I am currently implementing the following functions: export class MypageEditComponent { ngOnInit() { this.timer = Observable.timer(100, 100); this.timer.subscribe(t => { this.setFormData(); } ...

Ways to enable Urql (typescript) to accept Vue reactive variables for queries generated using graphql-codegen

I'm currently developing a Vue project that involves using urql and graphql-codegen. When utilizing useQuery() in urql, it allows for the use of Vue reactive variables to make the query reactive and update accordingly when the variables change. The ...