【无标题】变表哥

用Angular语言实现一个可变的表格,表头表示的内容是每月的日期,可以通过按钮切换表头日期。以下是我为你生成的代码示例:

// app.component.ts
import { Component } from '@angular/core';
import { DatePipe } from '@angular/common';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  providers: [DatePipe]
})
export class AppComponent {
  title = 'angular-table';
  data = [
    {name: 'Alice', age: 25, salary: 5000},
    {name: 'Bob', age: 30, salary: 6000},
    {name: 'Charlie', age: 35, salary: 7000},
    {name: 'David', age: 40, salary: 8000},
    {name: 'Eve', age: 45, salary: 9000},
  ];
  headers = [];
  date = new Date();

  constructor(private datePipe: DatePipe) {
    this.updateHeaders();
  }

  updateHeaders() {
    this.headers = [];
    let days = new Date(this.date.getFullYear(), this.date.getMonth() + 1, 0).getDate();
    for (let i = 1; i <= days; i++) {
      this.headers.push(this.datePipe.transform(new Date(this.date.getFullYear(), this.date.getMonth(), i), 'd/M/yyyy'));
    }
  }

  prevMonth() {
    this.date.setMonth(this.date.getMonth() - 1);
    this.updateHeaders();
  }

  nextMonth() {
    this.date.setMonth(this.date.getMonth() + 1);
    this.updateHeaders();
  }
}
<!-- app.component.html -->
<h1>{{title}}</h1>
<button (click)="prevMonth()">Prev</button>
<button (click)="nextMonth()">Next</button>
<table border="1">
  <tr>
    <th>Name</th>
    <th>Age</th>
    <th>Salary</th>
    <th *ngFor="let header of headers">{{header}}</th>
  </tr>
  <tr *ngFor="let row of data">
    <td>{{row.name}}</td>
    <td>{{row.age}}</td>
    <td>{{row.salary}}</td>
    <td *ngFor="let header of headers"></td>
  </tr>
</table>
/* app.component.css */
table {
  width: 100%;
}

th, td {
  text-align: center;
}

希望这个代码示例对你有帮助。如果你有其他的需求,可以继续和我聊天。😊

源: 与必应的对话, 2023/11/15
(1) github.com. https://github.com/johandb/simple-date-picker/tree/6053de695a430141b16d94a6104abe023e2b1ed7/projects%2Fsimply-date-picker%2FREADME.md.
(2) github.com. https://github.com/sunnydan/dja_mean_timezonedisplay/tree/8d6b1f8180feac0276a51f867dfb688eb98abf42/src%2Fapp%2Fapp.component.ts.
(3) github.com. https://github.com/MarwanELAdawy/covid19Tracker/tree/dc2b667b4b088197c0bea6fda6d1e787bf5ebf6f/src%2Fapp%2Fapp.component.ts.
(4) github.com. https://github.com/Richasharma9180/NewApp/tree/ede7cfeb26f48c0107738d71059b2aace75ef0c3/src%2Fapp%2Fapp.component.ts.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值