我有一张表,我动态生成了列和行。
这是我的table.component.html
<table id="tabella" class="table table-striped table-hover">
<thead class="thead-dark">
<tr>
<th *ngFor="let header of _object.keys(utenti[0]); let i = index"> {{header}}</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let row of utenti">
<th *ngFor="let utente of _object.keys(row)">{{row[utente]}}</th>
</tr>
</tbody>
</table>
这是我的table.component.ts
import { Component, OnInit } from '@angular/core';
import { GetUtentiService } from '../../services/getutenti.service';
@Component({
selector: 'app-utente',
templateUrl: 'utente.component.html',
styleUrls: ['./utente.component.css']
})
export class UtenteComponent implements OnInit {
utenti: any;
_object = Object;
visibile: boolean;
constructor(private _getutentiService: GetUtentiService) { }
ngOnInit() {
// Richiamo la funzione definita nel service che effettua una get sul database
this._getutentiService.getUtenti().subscribe(response => {
this.utenti = response;
}, error => {
console.log(error);
});
}
}
这是我的服务。
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class GetUtentiService {
private url: string = 'http://localhost:56493/Server/GetUtente';
constructor(private http: HttpClient) { }
getUtenti() {
return this.http.get(this.url);
}
}
现在我想控制表格中的单个单元格。我试图仅隐藏 ID 的列,但在此代码中我无法说“如果是 ID,则隐藏此列”,或者我想将日期格式化为 dd/mm/yyyy 而不是 yyyy/mm/dd hh: mm:ss 但是,我必须再次管理表格的单个单元格。
我怎样才能对我的代码进行这些更改?
最佳答案
看来你有两个问题。如果我理解正确,您需要:
- 要向 ID 单元格添加类名,如
cell-id
, - 将日期格式从
yyyy/mm/dd
更改为dd/mm/yyyy
如果准确的话,你能试试这个吗:
<tbody>
<tr *ngFor="let row of utenti">
<th *ngFor="let utente of _object.keys(row)"
[ngClass]="{'cell-id': row[utente] === 'id'}">
{{row[utente]}}
</th>
</tr>
</tbody>
对于你的第二个问题,你试过 DatePipe 了吗? ?
关于javascript - 如何在 Angular 9 中隐藏动态表的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60604945/