Showing an array in tabular form using Angular

I have a single-element list that appears as shown below

view image of array and contents

My goal is to present the data in a table with headers for Author Name and Title. However, my current code displays all the titles in one row instead of creating separate rows for each Title and Author Name. How can I fix this issue?

See the table created using the code below

<table class='table table-striped' aria-labelledby="tableLabel" *ngIf="authorWithBooks">
  <thead>
    <tr>
      <th>Author Name</th>
      <th>Title</th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let authorWithBook of authorWithBooks">
      <td>{{ authorWithBook.authorName }}</td>
      <td>{{ authorWithBook.bookNameList }}</td>
    </tr>
  </tbody>
</table>

Answer №1

To efficiently display author and book title data, utilize two loops: one for each author and the second for each book title. The creation of tr elements will occur within the second loop, while the first loop can be enclosed in an <ng-container>.

Here is an example code snippet:


   <ng-container *ngFor="let authorWithBook of authorWithBooks">
    <tr *ngFor="let bookName of authorWithBook.bookNameList">
      <td>{{ authorWithBook.authorName }}</td>
      <td>{{ bookName }}</td>
    </tr>
    </ng-container>

Refer to this functioning StackBlitz link for a live demonstration.

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

Leverage the VTTCue Object in an Angular2 project using Typescript

I am looking to dynamically load subtitles onto a video. let subs:TextTrack = video.addTextTrack('subtitles'); for (let dataSrt of dataSrts) { let cue: any = new VTTCue( dataSrt['startTime'], da ...

Is the contact form confirmation message displaying too prominently?

I am currently troubleshooting two different forms on a website I'm developing, and I am unsure of the exact cause of the issue. Form 1: Form 2: When attempting to submit Form 1 without entering any information, an error message is displayed exactl ...

Are you looking to create a dynamic quiz platform with Angular?

Within my program, there are two choices available: one is to host the quiz and the other is to join the quiz. Upon hosting a quiz, a random code will be created and must be shared so that participants can join the quiz. Which Angular concept should be u ...

Angular2: Utilizing filter and search functionalities for an unordered list

Is there a way to implement filtering for an unordered list in Angular using an input field? This specific component creates an unordered list upon page load by fetching data from a JSON file and using the *ngFor directive along with a service. Below is t ...

The website is experiencing crashes in IE11 whenever the developer tools are closed

I'm experiencing an issue with my website where it crashes internet explorer if the developer tools are not opened. I have checked for console.log calls as a possible cause, but that doesn't seem to be the problem here. The error is not just di ...

When navigating to a new URL in Angular 7, the browser will automatically redirect to the home component

Currently, I'm developing a project using Angular 7.2.0. One issue I'm facing is that when I attempt to navigate to a particular page initially, such as: http://localhost:4200/comics?sort=series The browser always gets redirected to the root: ...

The 'npm update' command seems to be failing when attempting to update dependencies within Angular2

I am looking to upgrade the outdated dependencies in my Angular 2 project. Upon running npm outdated, I identified several dependencies that need updating. However, when I attempt to update them using the npm update command, it does not seem to work as exp ...

``There seems to be an issue with the Angular Material Table dataSource, as it is not binding correctly or updating

When examining the served page: <table _ngcontent-c6="" class="mat-elevation-z8 mat-table" fxfill="" mat-table="" matsort="" role="grid" ng-reflect-data-source="[object Object]"> In View: table matSort fxfill mat-table [dataSource]="dataSou ...

Trigger a click (or selection) on a specific dropdown option using jQuery

I am attempting to initiate a click function upon page load using jQuery on a dropdown option within Chosen, in order to activate a subsequent action. Unfortunately, I have not been successful in getting it to work. I have tried the following methods: jQ ...

Include the button beneath the Rating section using JQuery or AJAX

I am having trouble adding buttons after the result.date in my code. Placing the buttons between td tags is causing an [object Object] error to show up. $.ajax({ type: 'GET', url: 'someUrl.php', data: {op : "demo"}, da ...

Insert the characteristics of the object into the header of the table, and populate the rows of the table

I have created an HTML table that displays specific object properties when the mouse hovers over a designated object. For example, hovering over the dog object will display the dog's name. I want to expand this functionality to also show the cat' ...

Enhance the validation of multiple fields in Angular through asynchronous AbstractControl in groupForm

Ensuring the validity of a component is crucial. A valid component is defined by its unique brand, designation, type, reference, supplierId, and familyId. The challenge arises when one or more of these attributes are not unique. For example, if I'm ed ...

Displaying JavaScript Countdown in PHP Table

I have a database table with multiple fields and I am looking to create a countdown using the integer value from one of the fields (in minutes). How can I loop through and display the countdown for each row in a PHP table utilizing these values, with the a ...

"Applying CSS styles to highlighted text using jQuery - how can I do it

Struggling to style selected text with CSS? Here's what I've tried so far without success in Firefox. $(document).keyup(function(){ savedRange = selection.getRangeAt(0); $(savedRange).wrap('<span style="color:red"></span>' ...

Increase the div id using jQuery

I've got this code snippet here and, oh boy, am I a newbie. How can I increase the number in the div using a jQuery script? if($res >= 1){ $i=1; while($row = mysqli_fetch_array($qry)){ echo "<div clas ...

Create a vibrant stroke and conclude with a circular marker beneath the content with CSS

Can you create a vibrant line with a dot underneath text using CSS, similar to the example shown in the image? The underline should begin with some spacing or padding. .underline { text-decoration: underline; text-underline-offset: 10px; displa ...

When Ruby on Rails is combined with HTML and CSS and marked as selected, it will be displayed with

I'm currently in the process of developing my cookbook app and the next feature on my agenda is the grocery list functionality. Once on the recipe page, users will have the option to click on an "Add to Grocery List" link which will redirect them to a ...

How to Fetch Questions from a Remote Source Using Dynamic Forms in Angular 2

Hey there! I'm looking to make use of the dynamic form feature found in the https://angular.io/docs/ts/latest/cookbook/dynamic-form.html. Specifically, I want to accomplish the first task mentioned in the question.service.ts file. Can anyone guide me ...

JQuery mouseover event not triggering after modification of HTML

I'm dealing with an HTML table that looks like this: <div id="recommendation"> <table id="category_selected"> <tr> <td>1</td> </tr> </table> </div> and my jQuery code is like this: $ ...

Incorporating ratings and heart icons next to every movie title

I am tasked with creating a simple Movie listing website where users can add or remove movies from their favorites list. Instead of a standard add to favorites button, I would like to use the heart icon from Bootstrap. Additionally, I want to display the r ...