Angular2 Directive that Duplicates a Group of <tr> Elements

How can I build a component that generates HTML based on this data and HTML code? The <head> and <tbody> sections are projected, and I understand how to project multiple elements. However, I am unsure of how to repeat the projected <tr> in the body. In Angular 1, selectively compiling elements and binding them was simple. Even manually tracking copies with a simplified clone of ngRepeat was straightforward. I have successfully created a working Angular 1 directive for this, but I am struggling to translate it to Angular 2. In Angular 1, because of how parts of the table are selectively compiled, you can apply directives to the <thead> and its children, as well as the <tbody>. Anything placed on any of the children of <tbody> will be repeated and compiled for each element in the passed array.

JSON

[
    {name: "bob", number: "555-1234"},
    {name: "fred", number: "555-1235"},
    {name: "joe", number: "555-1236"}
  ]
  

HTML

<table sortable="person in people">
    <thead>
      <tr>
        <th>Name</th>
        <th>Number</th>
      </tr>
   </thead>
   <tbody>
     <tr>
       <td>{{person.name}}</td>
       <td>{{person.number}}</td>
     </tr>
   </tbody>
 </table>

Result

<table sortable="person in people">
  <thead>
    <tr><th>Name</th><th>Number</th></tr>
  </thead>
  <tbody>
    <tr>
      <td>bob</td>
      <td>555-1234</td>
    </tr>
    <tr>
      <td>fred</td>
      <td>555-1235</td>
    </tr>
    <tr>
      <td>joe</td>
      <td>555-1236</td>
    </tr>
  </tbody>
</table>

The sortable attribute can either be [sortable] or *sortable.

Answer №1

To iterate through a list in Angular, you would use the *ngFor directive. Here is an example of how to use it:

<table>
<thead>..headers...</thead>
<tbody>
<tr *ngFor="#item of itemList">
  <td> {{ item.name }} </td>
  <td> {{ item.number }} </td>
</tr>
</tbody>
</table>

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

Using Material UI date picker with TypeScript: A Complete Guide

Well, I have to admit that I usually don't resort to putting 'any' as the type when I'm uncertain what to do, but right now, I'm starting to feel quite frustrated. I'm currently working with Material UI date picker in conjunct ...

The ngFor directive encounters issues when placed within a quotation mark or when used within a Bootstrap Tooltip Tag

.HTML File : I encountered an error saying, "Identifier 'mg' is not defined." However, {{mgr[0].value}} works <button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="bottom" ...

How to eliminate a particular validator from a form group in Angular

My goal is to eliminate the specific validator from the validator array so that I can reconfigure the controls when certain values have changed. I am aware of the traditional solution where I would need to repeatedly set validators. checked(event: MatC ...

Identify the appearance of a web component being added to the Document Object

After reading this discussion regarding a web-component I created: <my-vue-web-comp [userId]="1" id="my-component"></my-vue-web-comp> The component functions properly in Angular. How can I determine when the web component h ...

While using axios to make a GET request, I encountered errors when checking for .isSuccess in react

const searchInvoiceList = async ( plantLocation: string, invoiceType: string ) => { let dataList: InvoiceData[] = []; await axios .get(`${linkURL}inv/getControlList/${plantLocation}/${invoiceType}`) .then((response) => { dataLis ...

No elements present in TypeScript's empty set

Question for discussion: Can a type be designed in TypeScript to represent the concept of an empty set? I have experimented with defining one using union, disjoint union, intersection, and other methods... ...

Error in Angular 8: The type of '[object Object]' is an object, whereas NgFor only supports binding to Iterables such as Arrays

I recently started using Angular 8 and I'm dealing with an issue while trying to fetch data from an http get request. I am attempting to iterate through the data using *ngFor but unfortunately encountering this error. Any suggestions on how to resolv ...

What is the method for executing a custom command within the scope of a tree view item?

I am trying to implement a custom "ping" function in my VS Code Extension that will send the details of a selected treeview object. Despite looking at various examples, I have been unable to properly build and register the command. Although I can invoke th ...

The export 'ChartObject' is not available in highcharts

Trying to integrate highcharts into my Angular 4 project has been a bit challenging as I keep encountering the following error: highcharts has no exported member 'ChartObject' I have experimented with different options such as angular-highchart ...

Is it possible to create a custom range slider using Angular Material 2?

Can Angular Material 2 be used to create a custom range slider? For example, I want to display organization sizes with specific ranges like: 1-10 employees 11-50 employees 51-200 employees 201-500 employees 501-1,000 employees 1,001-5,000 employees 5,001 ...

Exploring the concept of type inheritance in TypeScript

I am working on developing various components for an app, each with its own specific structure. The general structure is defined as COMPONENT. Within this framework, there are two distinct components: HEADING and TEXT. These components should be subclasses ...

Typescript's confidential variables

Currently, I am delving into the world of Angular2 and familiarizing myself with working with classes in javascript for the first time. I'm curious about the significance of using the private parameter in the constructor, as opposed to simply writing ...

Tips for utilizing ion-img within an Ionic 3 application?

I am currently developing an app using Ionic 3 that includes a large number of images spread across different pages. Initially, I used the default HTML image tag to display these images. However, this approach caused lag in the app's performance and a ...

The most secure method for retrieving User Id in AngularFire2

I'm currently facing a dilemma in determining the most secure method to obtain an authenticated user's uid using AngularFire2. There seem to be two viable approaches available, but I am uncertain about which one offers the best security measures ...

The count of bits is not producing the anticipated result

Attempting to tackle the challenge of Counting Bits using JavaScript, which involves determining the number of set bits for all numbers from 0 to N, storing them in an array, and returning the result Let me provide an explanation Input: n = 5 ...

Using the Angular JSON pipe with angular-datatables

I am currently utilizing angular-datatables to exhibit NoSQL de-normalized data in a grid format for visualization purposes. Within my dataset, I have several intricate nested JSON objects and I intend to showcase a specific cell with formatted JSON using ...

Typescript's forEach method allows for iterating through each element in

I am currently handling graphql data that is structured like this: "userRelations": [ { "relatedUser": { "id": 4, "firstName": "Jack", "lastName": "Miller" }, "type": "FRIEND" }, { "relatedUser": ...

Switch the paper tab to a dropdown menu in Polymer.js

Can someone assist me in transforming the paper tab into a paper drop down menu in polymer JS? I want the drop-down to appear with a list of values when hovering over the Top menu. Activity Execution <paper-tab cla ...

The combination of Object.keys() and the find function

Having trouble figuring out why I'm getting an error when attempting to use ES6 .find on the following data in order to retrieve the record with id number 3. { {id:10,title:'Dairy & Eggs'} {id:7,title:'Laundry & Household'} {id ...

How can I merge these two Observables in Angular to create an array of objects?

Let's say we are working with two different datasets: student$ = from([ {id: 1, name: "Alex"}, {id: 2, name: "Marry"}, ]) address$ = from([ {id: 1, location: "Chicago", sid: 1}, {id: 2, location: &qu ...