Utilizing Angular's ngFor directive to iterate through a collection of objects containing an array property

I've defined a class that looks like this:

export class TableColumn {
  columnName: string;
  isToFilter: boolean;
  isToSort: boolean;
  isLink: boolean;
  linkUrl: string;
  columnData: any[];

  constructor(
    columnName: string,
    isToFilter: boolean,
    isToSort: boolean,
    isLink: boolean,
    linkUrl: string,
    columnData: any[]
  ) {
    this.columnName = columnName;
    this.isToFilter = isToFilter;
    this.isToSort = isToSort;
    this.isLink = isLink;
    this.linkUrl = linkUrl;
    this.columnData = columnData;
  }
}

In my code, when I use the *ngFor directive to loop over an array of instances of this class like so:

 ngFor="let column of tableColumns" 

The directive not only iterates through the content of tableColumns, but also through the content of the columnData property in each instance.

I'm looking for a way to prevent the iteration through the content of the columnData property. Please assist me with a solution.

Answer №1

Are you indicating that you possess an assortment of TableColumn objects and desire to iterate over them, then within each one also iterate over the columnData array?

Just a heads up, you can simplify your class object like this which compiles to the same JavaScript. Check out this page for reference: Look at this page to see what I mean.

export class TableColumn {
    constructor(
        public columnName: string,
        public isToFilter: boolean,
        public isToSort: boolean,
        public isLink: boolean,
        public linkUrl: string,
        public columnData: any[]
    ) { }
}

If we have some sample data in our controller like this:

public tableColumnList = [
  new TableColumn('one', true, true, true, 'google.com', [1,2,3]),
  new TableColumn('two', true, true, true, 'google.com', [4,5,6, 7, 8]),
  new TableColumn('three', true, true, true, 'google.com', [9, 10, 11])
];

You can simply use nested *ngFor directives as shown below.

<div *ngFor="let tc of tableColumnList">
  <h4>{{tc.name}}</h4>
  <ul>
    <li *ngFor="let data of tc.columnData">
      {{data}}
    </li>
  </ul>
</div>

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

Building a bridge: How to integrate an existing Javascript library into Angular?

I have developed a pure javascript library that is available for installation via npm. Now, I am looking to enhance its compatibility with Angular projects by creating a seamless "wrapper" for it within Angular. My challenge lies in not knowing the exact p ...

What is the best way to maintain a div centered when the browser window shrinks smaller than the div's dimensions?

Is there a way to keep a div centered and create white space around it when the browser window is resized? I want this effect to happen whether the browser window gets larger or smaller. Currently, when the window size decreases, the left side of the div l ...

What is the alternative to using javascript onclick(this.form) in jQuery?

I have a current setup that looks like this: <form action="cart.php?action=update" method="post" name="cart" id="cart"> <input maxlength="3" value="25" onKeyup="chk_me(this.form)" /> etc.. </form> The onKeyup event triggers the chk_me ...

The p5js function pixelDensity() does not provide improved resolution on retina screens

Currently, I am working on a project that involves generating collages from sets of photos. This process includes averaging the pixels' colors of the images and then using that averaged color to plot points on the canvas. However, when I zoom in on re ...

What is the correct method for utilizing the async pipe in Angular 7 to effectively update the template?

One of my service methods is responsible for fetching dummy data and returning it as an observable. private dummySubject = new BehaviorSubject<Dummy>(null); dummy$ = this.dummySubject.asObservable(); loadDummyData(id: string): Observable<Dummy&g ...

AngularTS regex that enforces the use of a decimal point in numbers

I am working on a requirement where the input should only accept decimal characters, negative or positive. I need to use regex to make the decimal point mandatory, however it is currently allowing negative whole numbers which is not the desired behavior. I ...

Seeking a way to display a random div at a 1% rate without generating additional div elements

Searching for an answer to display only one div with a rate of 1/100. Currently, I am utilizing the following JavaScript: var random = Math.floor(Math.random() * $('.item').length); $('.item').hide().eq(random).show(); This method wor ...

.submit fails to function when the bound object has been updated via an ajax call

I managed to implement an AJAX commenting system where, upon clicking the Post Comment button, an ajax call is made instead of submitting the form traditionally. Everything was working smoothly until I tried refreshing the page with the comment submit butt ...

Having difficulty capturing events using jQuery

Trying to capture events in jQuery, here is the snippet being used: console.log(selectElement); selectElement.on('selected', function(event) { console.log('Value ' + event.selected + ' selected.'); }); Output in ...

Having trouble with deploying Node.js to Heroku? Feel like your 'git push her

After successfully running my app locally, I encountered an issue when trying to deploy it to Heroku. The deployment process just hangs indefinitely and only displays one public file on the website with an error message stating: "https://analogy-alley.hero ...

Creating a unique chrome extension that swaps out HTML and JavaScript components

Is there a possibility to create a Chrome extension that eliminates the JavaScript and CSS from a website while it loads, replacing them with new scripts from a separate source? For example, changing 'Old script' to 'new script', or &a ...

Manipulate and scale with jQuery

I am currently utilizing the jQueryUI library with its Draggable and Resizable functionalities to resize and drag a div element. However, I am encountering some unexpected behavior where the div jumps outside of its container upon resizing. How can I resol ...

Error Encountered: Unable to locate the variable "Login"

I encountered this rejection error It all started to go wrong in "Routes.js": import React, {Component} from 'react'; import {Router, Stack, Scene} from 'react-native-router-flux'; export default class Routes extends Component<{ ...

The issue of PrimeReact AccordionTab failing to render when enclosed within a custom component

I'm encountering an issue with my specialized FilterAccordionTab component, which is an extension of PrimeReact's (V8) AccordionTab. It appears that the content is not being displayed when using this custom component. However, everything function ...

Unexpected outcomes can arise from rotating looped meshes

In my current three.js project, I am working with a total of 100 meshes which are organized into 10 different scenes: // Adding 100 meshes to 10 scenes based on an array containing 100 elements for (var i = 0; i < data.length; i++) { mesh = new ...

Instantiate a fresh Date object in JavaScript by passing in my specific parameter

Check out this code snippet: $(function () { var timestamp = 1443563590; //Tue, 29 Sep 2015 21:53:10 GMT var today2 = moment.unix(timestamp).tz('America/New_York').toString(); alert(today2); //var dateinNewYork = new Date(wh ...

Remove the list by conducting a comparison analysis

<html> <head> <title> Manipulating List Items Using JavaScript </title> <script type="text/javascript"> function appendNewNode(){ if(!document.getElementById) return; var newlisttext = document.changeform.newlist.val ...

What's preventing my Angular list from appearing?

I am currently developing an Angular project that integrates Web API and entity framework code first. One of the views in my project features a table at the bottom, which is supposed to load data from the database upon view loading. After setting breakpoin ...

Utilizing crossfilter and DC.js to perform reduce() on groupAll() operation

This task seems deceptively simple :/ I've been following the crossfilter API instructions on running a reduce operation on groupAll: https://github.com/square/crossfilter/wiki/API-Reference#groupAll_reduce However, despite my efforts, I can't ...

In TypeScript, it is possible to convert any data type into a numerical map

It appears that typescript permits the casting of any type to a map with keys of type number. Consider this example code: type NumberMap = { [key: number]: string } type AnyType = { foo: string bar: boolean[] } const anyTypeObj: AnyType = { foo: " ...