英文:
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 '@angular/core';
import { Employee } from '../../models/empModel';
import * as data from '../../data/employees';
@Component({
selector: 'app-employee-list',
templateUrl: './employee-list.component.html',
styleUrls: ['./employee-list.component.css'],
})
export class EmployeeListComponent {
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;
}
}
In the view:
<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>
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 '@angular/core';
import { Employee } from '../../models/empModel';
@Component({
selector: 'app-employee-table-item',
templateUrl: './employee-table-item.component.html',
styleUrls: ['./employee-table-item.component.css'],
})
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 <app-employee-table-item></app-employee-table-item>
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 <tr>
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: '[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>
答案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
<tr *ngFor="let employee of empsArray | filter: deptno">
<app-employee-table-item
[employee]="employee"
></app-employee-table-item>
</tr>
with this:
<tr
data-selector="app-employee-table-item"
*ngFor="let employee of empsArray | filter: deptno"
[employee]="employee"
></tr>
In employee-table-item.component.ts, change the selector to
selector: '[data-selector=app-employee-table-item]',
Now, instead of looking for <app-employee-table-item\>
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论