javascript - 如何在 Angular 9 中隐藏动态表的元素

标签 javascript html angular frontend

我有一张表,我动态生成了列和行。

这是我的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/

相关文章:

html - 在模态弹出窗口中使用 span id 更改 li 类

javascript - 使用 jquery AJAX 提交表单

javascript - Angular 应用程序中的 event.stopPropagation() 导致 jasmine 超时

javascript - 创建事件监听器以从 localStorage 中删除对象

javascript - ng build --aot : ERROR in : Unexpected directive 'TooltipComponent in 的意外结果

Angular 构建索引文件 : where does the injected style come from?

javascript - 如何将整个数组作为字符串获取?

javascript - 使用揭示模块模式从动态元素获取元素 ID

javascript - knockout JS "You cannot apply bindings multiple times to the same element"

javascript - 使用基于 html id 的 JavaScript 循环