如何在Angular 8应用程序中直接在tr标签中显示每个表行的内容?

huangapple go评论77阅读模式
英文:

How can I display the contents of every table row directly in the tr tag in an Angular 8 app?

问题

我使用Angular 8开发应用程序。

employee-list 组件在表格中显示数据。

employee-list.component.ts 文件中:

public displayMode: String = 'grid';
public deptno: number = -1;
public empsArray: Employee[] = data.employees;

public removeEmployee(empno: number) {
  this.empsArray = this.empsArray.filter((item) => item.empno != empno);
}

public filterByDepartment(num: number) {
  this.deptno = num;
}

public setDisplayMode(mode: String) {
  this.displayMode = mode;
}

在视图中:

<div class="table-responsive">
  <table class="table table-striped">
    <thead>
      <tr>
        <th>Full Name</th>
        <th>Job</th>
        <th>Actions</th>
      </tr>
    </thead>
    <tbody>
      <tr *ngFor="let employee of empsArray | filter: deptno">
        <app-employee-table-item [employee]="employee"></app-employee-table-item>
      </tr>
    </tbody>
  </table>
</div>

每个表格行的内容由一个名为 employee-table-item 的(子)组件处理:

@Input() employee: Employee;

问题

由于每个表格行的内容都包装在 <app-employee-table-item></app-employee-table-item> 元素中,表格显示不正确。

我想直接在 <tr> 标签中显示每个表格行的内容,同时保持模板在一个单独的HTML文件中。

问题

<tr> 标签中直接显示每个表格行的内容的最可靠方式是什么?

英文:

I have been working an app with Angular 8.

The employee-list component displays data in a table.

In employee-list.component.ts I have:

import { Component } from &#39;@angular/core&#39;;
import { Employee } from &#39;../../models/empModel&#39;;
import * as data from &#39;../../data/employees&#39;;

@Component({
  selector: &#39;app-employee-list&#39;,
  templateUrl: &#39;./employee-list.component.html&#39;,
  styleUrls: [&#39;./employee-list.component.css&#39;],
})
export class EmployeeListComponent {
  public displayMode: String = &#39;grid&#39;;
  public deptno: number = -1;
  public empsArray: Employee[] = data.employees;

  public removeEmployee(empno: number) {
	this.empsArray = this.empsArray.filter((item) =&gt; item.empno != empno);
  }

  public filterByDepartment(num: number) {
	this.deptno = num;
  }

  public setDisplayMode(mode: String) {
	this.displayMode = mode;
  }
}

In the view:

&lt;div class=&quot;table-responsive&quot;&gt;
    &lt;table class=&quot;table table-striped&quot;&gt;
      &lt;thead&gt;
        &lt;tr&gt;
          &lt;th&gt;Full Name&lt;/th&gt;
          &lt;th&gt;Job&lt;/th&gt;
          &lt;th&gt;Actions&lt;/th&gt;
        &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
        &lt;tr *ngFor=&quot;let employee of empsArray | filter: deptno&quot;&gt;
          &lt;app-employee-table-item
            [employee]=&quot;employee&quot;
          &gt;&lt;/app-employee-table-item&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;

As can be seen above, the contents of each table row is handled by a (child)component, called employee-table-item:

import { Component, EventEmitter, Input, Output } from &#39;@angular/core&#39;;
import { Employee } from &#39;../../models/empModel&#39;;

@Component({
  selector: &#39;app-employee-table-item&#39;,
  templateUrl: &#39;./employee-table-item.component.html&#39;,
  styleUrls: [&#39;./employee-table-item.component.css&#39;],
})
export class EmployeeTableItemComponent {
  @Input() employee: Employee;
}

The problem

The table is diplsyed wrongly, due to the fact that the contents of every table row is wrapped in a &lt;app-employee-table-item&gt;&lt;/app-employee-table-item&gt; element.

I want display the contents of every table row directly in the <tr> tag, while keeping the template in a distinct HTML file.

Question

What is the most reliable way to display the contents of every table row directly in the &lt;tr&gt; tag?

答案1

得分: 3

A solution would be to use your component as a directive. It is pretty common to do that exactly in your case.

app-employee-table-item:

@Component({
  selector: '[appEmployeeTableItem],app-employee-table-item',
  templateUrl: './employee-table-item.component.html',
  styleUrls: ['./employee-table-item.component.css'],
})
export class EmployeeTableItemComponent {
  @Input('appEmployeeTableItem') employee: Employee;
}

EmployeeList.html:

  <tbody>
    <tr
      *ngFor="let employee of empsArray | filter: deptno"
      [appEmployeeTableItem]="employee"
    ></tr>
  </tbody>
英文:

A solution would be to use your component as a directive. It is pretty common to do that exactly in your case.

app-employee-table-item:

@Component({
  selector: &#39;[appEmployeeTableItem],app-employee-table-item&#39;,
  templateUrl: &#39;./employee-table-item.component.html&#39;,
  styleUrls: [&#39;./employee-table-item.component.css&#39;],
})
export class EmployeeTableItemComponent {
  @Input(&#39;appEmployeeTableItem&#39;) employee: Employee;
}

EmployeeList.html :

  &lt;tbody&gt;
    &lt;tr
      *ngFor=&quot;let employee of empsArray | filter: deptno&quot;
      [appEmployeeTableItem]=&quot;employee&quot;
    &gt;&lt;/tr&gt;
  &lt;/tbody&gt;

答案2

得分: 1

你可以使用的方法之一是更改从使用标签选择器到使用属性值选择器

employee-list.component.html中,将以下代码替换为:

<tr
    data-selector="app-employee-table-item"
    *ngFor="let employee of empsArray | filter: deptno"
    [employee]="employee"
></tr>

employee-table-item.component.ts中,将选择器更改为:

selector: '[data-selector=app-employee-table-item]',

现在,Angular不再查找<app-employee-table-item> HTML元素,而是查找具有属性data-selector值为app-employee-table-item的任何HTML元素,并用您的模板替换该元素。

您的Stackblitz,已经进行了此更改:https://stackblitz.com/edit/angular-modal-bootstrap-ke3bbc?file=src%2Fapp%2Fcomponents%2Femployee-list%2Femployee-list.component.html

英文:

An approach you can use is to fix this is to change from using a tag selector to using an attribute value selector.

In employee-list.component.html, replace

&lt;tr *ngFor=&quot;let employee of empsArray | filter: deptno&quot;&gt;
    &lt;app-employee-table-item
        [employee]=&quot;employee&quot;
    &gt;&lt;/app-employee-table-item&gt;
&lt;/tr&gt;

with this:

&lt;tr
    data-selector=&quot;app-employee-table-item&quot;
    *ngFor=&quot;let employee of empsArray | filter: deptno&quot;
    [employee]=&quot;employee&quot;
&gt;&lt;/tr&gt;

In employee-table-item.component.ts, change the selector to

selector: &#39;[data-selector=app-employee-table-item]&#39;,

Now, instead of looking for &lt;app-employee-table-item\&gt; HTML elements, Angular will look for any HTML elements with an attribute called data-selector with value of app-employee-table-item and replace that element with your template.

Your Stackblitz, forked, with this change: https://stackblitz.com/edit/angular-modal-bootstrap-ke3bbc?file=src%2Fapp%2Fcomponents%2Femployee-list%2Femployee-list.component.html

huangapple
  • 本文由 发表于 2023年5月24日 20:01:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323307.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定