Angular6 table using *ngFor directive to display objects within an object

Dealing with Angular 6 tables and encountering a challenge with an item in the *ngFor loop.

Here is my HTML view:

<table class="table table-bordered text-center">
  <tr>
    <th class="text-center">Cuenta</th>
    <th class="text-center">ENE 2018</th>
    <th class="text-center">Proy. Lineal</th>
    <th class="text-center">Proy. Sugerida</th>
    <th class="text-center">Proy. Comercial</th>
    <th class="text-center">Presupuesto a convenir</th>
  </tr>
  <tr *ngFor="let data of centroData; let id = index">
    <td>
      <span>{{data.id}}</span>
    </td>
    <td>
      <span>{{data.ENE2018}}</span>
    </td>
    <td>
      <span>{{data.ProyLinealCalculado}}</span>
    </td>
    <td>
      <span>{{data.ProySugeridaCalculado}}</span>
    </td>
    <td>
      <span>{{data.ProyComercialCalculado}}</span>
    </td>
    <td>
      <span>{{data.Presupuesto}}</span>
    </td>
  </tr>
</table>

This represents my array in component.ts:

centroData: Array<any> = [
    {
      id: 123333123,
      ENE2018: 1340300,
      ProyLinealCalculado: 1393939,
      ProySugeridaCalculado: 1239393,
      ProyComercialCalculado: 3039430,
      Presupuesto: null,
      subcuentas: [
        {
          id: 1,
          folio: "123333123-01",
          ENE2018: 39394,
          ProyLinealCalculado: 1393939,
          ProySugeridaCalculado: 1239393,
          ProyComercialCalculado: 3039430,
          Presupuesto: null
        },
        {
          id: 2,
          folio: "123333123-02",
          ENE2018: 39394,
          ProyLinealCalculado: 1393939,
          ProySugeridaCalculado: 1239393,
          ProyComercialCalculado: 3039430,
          Presupuesto: null
        }
      ]
    }
 ];`

In essence, I aim to include a new <tr> for 'subcuentas'. When there are multiple elements in the array, how can this be accomplished?

The solution on my mind

I understand that nesting another *ngFor inside the initial *ngFor will break the row structure of the table. How should I tackle this challenge?

Answer №1

To create this layout, I utilized the ng-container tag as shown in the code snippet below. Hopefully, this code will be helpful.

<table class="table table-bordered text-center">
  <tr>
    <th class="text-center">Cuenta</th>
    <th class="text-center">ENE 2018</th>
    <th class="text-center">Proy. Lineal</th>
    <th class="text-center">Proy. Sugerida</th>
    <th class="text-center">Proy. Comercial</th>
    <th class="text-center">Presupuesto a convenir</th>
  </tr>
  <ng-container *ngFor="let data of centroData; let id = index">
    <tr>
    <td>
      <span>{{data.id}}</span>
    </td>
    <td>
      <span>{{data.ENE2018}}</span>
    </td>
    ... (additional rows) ...
  </ng-container>

  </table>

Output:

https://i.stack.imgur.com/vhSm7.png

Details about ng-container element.

  1. Explanation of ng-container Element
  2. Additional information on using ng-container in Angular

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

encountered difficulties while attempting to install npm i due to node-gyp errors

When attempting to run npm i --force, I encounter the following error: pm ERR! code 1 npm ERR! path /Users/moblizeit/GitHub/code-repo/apps/business-card-scanner/admin/businesscardadmin/node_modules/grpc npm ERR! command failed npm ERR! command sh -c -- nod ...

Tips for utilizing the material ui auto-complete search feature

I am in search of an alternative to material-ui-search-bar because it is no longer being maintained. I have been suggested to try using Material UI's auto complete instead. However, from the examples I've seen, it seems like only text field struc ...

What is the process for validating multiple check boxes using the reactive forms module?

I thought this would be a simple task, but I'm having trouble figuring it out. There are three checkboxes and I need to ensure that the user selects only one. The validator should check if any of the other two checkboxes have been selected and preven ...

Exploring TypeScript implementation of Redux toolkit's store

I'm currently diving into the world of Redux in React + TypeScript by following the tutorials provided by Redux Toolkit. I am attempting to implement it in a sample application. My main struggle lies with typings related to the store and the mappStat ...

What is the reason behind Typescript raising an error when attempting to compare two boolean variables with different values (true and false)?

In the screenshot below, you can see that we are encountering a peculiar error when attempting to compare a boolean variable with true. This condition will always return 'false' since the types 'false' and 'true' have no over ...

What is the role of the 'type' attribute in a button element

I am in need of a customized button that can optionally receive a type attribute: export interface ButtonProps { children: React.ReactNode; onClick?: (e: React.MouseEvent<HTMLElement>) => void; type: ?? } export const Button: React.Functio ...

Using ngFor in Angular to apply a function to a variable

Imagine I have two tables: one for Books and one for Authors. The Author table contains a reference to the ID of a book. In my Angular project, I aim to iterate through the list of Authors and then iterate through the books written by each author: <di ...

Is it possible to utilize an InterleavedBufferAttribute for index values?

I am troubleshooting a code snippet that is throwing an error: const geometry = new THREE.BufferGeometry(); const indices = new THREE.InterleavedBufferAttribute(...); geometry.setIndex(indices); // this is invalid After running this code, I receive a com ...

Customizing modal window HTML in ng-bootstrapNeed to override the default modal window

Currently, I am utilizing ng-bootstrap to create my modals. I have been pondering the most effective approach to customize the modal window's HTML. The default appearance of the modal html is somewhat like this: <div role="document" class="modal- ...

The resurgence of React JS in its role as a powerful tool

Although I struggle with React JS, I couldn't find a solution for this particular issue. I'm trying to incorporate this function into my tsx file and display it on the screen within a component. function displayUsers() { const UserListComponent ...

After refreshing the page, RouterLinkActive in Angular 6 fails to work

Scenario In my application, there is a menu with various items. The selected item is distinguished by adding the selected class to it, which changes its appearance. Problem While navigating between routes works smoothly, the issue arises when the page ...

Divide a string using multiple delimiters just one time

Having trouble splitting a string with various delimiters just once? It can be tricky! For instance: test/date-2020-02-10Xinfo My goal is to create an array like this: [test,Date,2020-02-10,info] I've experimented with different approaches, such ...

Issue: AuthInterceptor Provider Not Found

I've been attempting to set up an http interceptor with the new HttpClient in Angular 4.3, but I'm continuously encountering this error message: ERROR Error: Uncaught (in promise): Error: No provider for AuthInterceptor! In my app.module.ts f ...

What is the best way to transform private variables in ReactJS into TypeScript?

During my conversion of ReactJS code to TypeScript, I encountered a scenario where private variables were being declared in the React code. However, when I converted the .jsx file to .tsx, I started receiving errors like: Property '_element' d ...

Is it feasible to bring in a Typescript file into an active ts-node REPL session?

I want to experiment with some Typescript code that I have written. Currently, I usually run ts-node my-file-name.ts to test it out. But I am interested in making this process more interactive, similar to the Python REPL where you can import modules and ...

Please exclude any files with the name npm-debug.log.XXXXXX in the .gitignore file

Is there a way to exclude this file from the repository using .gitignore? https://i.stack.imgur.com/mK84P.png I am currently working on Gitlab. I have attempted the following: https://i.stack.imgur.com/2kN21.png Appreciate any help in advance ...

Are React component properties enclosed in curly braces?

I have a new component configured like this: type customType = differentType<uniqueType1, uniqueType2, uniqueType3>; function customComponent({q}: customType) When called, it looks like this: <customComponent {...myCustomVar} />, where myCus ...

Utilizing Async/Await to Streamline Google Maps Elevation Requests

I'm struggling to run this in a sequential manner. I've experimented with various methods like using Promise.all and getting stuck in callback hell, but what I really need is to obtain elevations for each point that has a valid altitude value (no ...

Exploring Typescript's Generic Unions

I'm facing an issue where I have an Array of generic objects and want to iterate over them, but TypeScript is not allowing me to do so. Below is a snippet of the code I am working with. Any ideas on how to solve this problem? type someGeneric<T> ...

Bringing together the functionality of tap to dismiss keyboard, keyboard avoiding view, and a submit button

On my screen, there's a big image along with a text input and a button at the bottom. The screen has three main requirements: When the user taps on the input, both the input and button should be visible above the keyboard The user must be able to ta ...