Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an Angular component to display a set of html table cells to embed in other tables

I have a lot of html tables that share the same 15 columns along with custom columns that are specific for each table. I would like to create these common 15 columns as a component that takes in a set of data and shows up as the 15 td's without a wrapper. I can't figure out how to create an Angular component that doesn't show the wrapper tag in the DOM and allows me to pass in input. Below is kind of what I'd like to do:

<tr *ngFor="let item of items">
  <td>Column 1</td>
  <my-common-columns [data]="item"></my-common-columns>
  <td>Another column</td>
</tr>

Unfortunately with the above code, <my-common-columns> tag shows up in the rendered DOM, which messes up the table. I can only have td's under the tr tag.

I also tried <ng-container *ngComponentOutlet="myCommonColumns"> since ng-container's don't show up in the DOM, but I can't figure out how to pass data into it.

like image 696
Jeremy Avatar asked Mar 11 '18 19:03

Jeremy


People also ask

How to displays data in the angular HTML data table?

Displaying Data in the Angular HTML data table. We need to set up an Angular project using Angular CLI. First, install a most recent version of Angular CLI, running the following command. Run the command to create a HTML data table component. Here, we are about to create an Angular service that can be created using the HttpClient service.

How to create a table app using angular CLI?

Create the Components. I created my project using Angular CLI, this tool allows you to quickly create an angular app and generate components, services, pipes, etc. from the command line. If you are starting from scratch, generate the project by running the following command: $ ng new table-app. Then generate the table and table row components.

How to display data with angular material mat-table?

You can display data using the Angular Material mat-table component. The mat table directive contains the mat-cell, mat-header-cell directives to build a full scalable table to show the data to the users. I wrote a detailed article on how to show the data with an angular material table with pagination and filtering.

How do I create a component in angular?

The easiest way to create a component is with the Angular CLI. You can also create a component manually. To create a component using the Angular CLI: From a terminal window, navigate to the directory containing your application. Run the ng generate component <component-name> command, where <component-name> is the name of your new component.


2 Answers

import { Component, TemplateRef, ViewChild } from '@angular/core';

@Component({
  selector: 'my-common-columns',
  template: `<ng-template let-item>
  <td>inner item1:{{item}}</td>
  <td>inner item2:{{item}}</td>
  </ng-template>`
})
export class CommonComponent {
  @ViewChild(TemplateRef) template: TemplateRef<any>;
}


@Component({
  selector: 'my-app',
  template: `
  <table>
    <tr *ngFor="let item of data">
      <td>first</td>
      <ng-template [ngTemplateOutlet]="common.template"
                 [ngTemplateOutletContext]="{$implicit: item}">
      </ng-template>
      <td>last</td>
    </tr>
  </table>
<my-common-columns #common></my-common-columns>
  `
})
export class AppComponent  {
  data = [1, 2, 3, 4, 5];
}

live example

result:

result

ngComponentOutlet will show tag in HTML too. Either, you should use abstraction to display tables for complex problems.

like image 74
Ilia Volk Avatar answered Sep 17 '22 21:09

Ilia Volk


Depending on your data structure you could probably do something like:

<td *ngFor="let col of item">{{ col }}</td>
like image 43
Johan Swanepoel Avatar answered Sep 20 '22 21:09

Johan Swanepoel