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 boost the efficiency of your Ionic 4 app development process

I have created an Ionic 4 app with over 50 screens, including components and popups. The build and live reload process is taking a lot of time, especially for minor UI changes. Is there a way to speed up the development process? Here are my Environment Se ...

getting TypeScript configured with webpack

I am currently using Typescript to develop a back-end API utilizing graphql and express. To manage the project development and building process, I have implemented webpack. As part of my setup, I am employing raw-loader in order to load graphql schemas an ...

The process of invoking another component's method in Angular 2

Is there a way to call a function from one component in Angular 2 into another? I have two components and I need to invoke a method defined in the other component. The components do not have a parent-child relationship, as the overview component is a rout ...

The 'Access-Control-Allow-Origin' header can be found on the resource that was requested

I'm facing an issue while trying to connect my .net core API to an Angular application. Whenever I attempt to do so, I encounter the following error: Access to XMLHttpRequest at 'https://localhost:44378/api/recloadprime' from origin 'h ...

Address NPM vulnerabilities through manual fixes

I downloaded a repository and ran an npm install, but encountered an error at the end. Now, every time I run npm audit, I receive the following message: found 18 vulnerabilities (5 low, 12 moderate, 1 high) in 15548 scanned packages 9 vulnerabilities requ ...

The object prototype can only be an instance of an Object or null; any other value will

While attempting to create a codesandbox in order to replicate a bug, I encountered an additional issue. You can view my codesandbox here: https://codesandbox.io/s/vue-typescript-example-o7xsv The error message states: Object prototype may only be an ...

Experiencing a Typescript error when trying to access a property within a nested object

My current challenge involves typing an object, which seems to be error-free until I try to access a nested property and encounter the dreaded red squiggle. After some research, I came across suggestions like this: type FlagValue = string | boolean | numb ...

What is the best way to fully reload an Angular component when the route is changed?

I'm looking for a way to reload or refresh a sidebar component when the route changes. Below is the code I currently have: constructor( private auth: AuthService, private router: Router, private changeDetector: ChangeDetectorRef ) { ...

"An error in the signature index results in the failure of the

I'm encountering a coding issue that's puzzling me. The TypeScript index signature I included in my code is as follows: const ships: { [index: string]: Ship } = {}; This snippet of code contains the problematic line: recieveAttack(e: any) { ...

Properly implement Angular/Typescript to populate an array with chosen objects

Currently, I have an Angular application that is fetching JSON resources from a Spring Boot REST API. These resources consist of simple player objects with attributes like id, name, position, and value. On the UI, each object is displayed along with a "BUY ...

Unable to translate text on the loading page

Encountering a peculiar issue with the translate service. Here's how I set it up: export class AppComponent implements OnInit { constructor( private translateService: TranslateService, angulartics2GoogleAnalytics: Angulartics2GoogleAnalytics ...

Contact creation not working on non-HubSpot form popups

I'm currently experiencing an issue with my website that has non-Hubspot forms. We have successfully integrated the tracking code to generate cookies for users, track their sessions, and enable the non-Hubspot forms. However, we are facing a problem s ...

Ways to store data in the localStorage directly from a server

I'm facing an issue - how can I store data in localStorage that was received from the server? Should I use localStorage.setItem for this purpose? And how do I handle storing an array in localStorage? Or am I missing something here? import { HttpCli ...

Concerning the utilization of the <mat-toolbar> element within Angular

Is the mat-toolbar in Angular going to persist across all components and pages of the application? Will it be present in every component throughout the application? <mat-toolbar color="primary"> <mat-toolbar-row> <span>Welcome to C ...

Utilizing vue-property-decorator: Customizing the attributes of @Emit

After seeing the @Emit feature, I checked out the example on GitHub. import { Vue, Component, Emit } from 'vue-property-decorator' @Component export default class YourComponent extends Vue { count = 0 @Emit() addToCount(n ...

Define the expected argument type of a function as an arrow function

In TypeScript, is there any way to enforce the use of arrow functions as function arguments? For instance, when using a publish-subscriber model and passing a listener function to a 'server' object, the server calls this function whenever a publi ...

The subscription function in observables may result in values that are undefined

I integrated a new angular 2 library into my application called "angular2-grid". This library is located within the node_modules folder. Furthermore, I created a service as shown below: import { Injectable } from '@angular/core'; import { Htt ...

Can SystemJS, JetBrains IntelliJ, and modules be combined effectively?

Looking for some clarification on the functionality of module includes and systemJS within an Angular2 app structure. I have set up a basic Angular2 app with the following layout: -app |-lib (contains shims and node libraries) |-components |-app |-app. ...

Error in Typescript: Function not being triggered on button click

As someone who is just starting out with typescript, I've been tackling a simple code that should display an alert message in the browser when a button is clicked. I've experimented with the button and input tags, as well as using both onclick ev ...

Dynamic content within an Angular Swiper

Greetings, Experts! I am a newcomer to angular and have successfully created 3 components: Swiper YouTube frame Map display Currently, I am facing a challenge where I need to utilize the swiper component multiple times on the homepage. The first instanc ...