Angular Material 2数据表组件

标签 angular typescript angular-material

我正在尝试从 Angular Material 组件实现一个表格组件,一切都很好,看起来也不错,最大的问题是如何使用数据库中的动态数据填充表格。 我从数据库收到一个对象数组,如本例所示,但我真的不知道如何迭代它并填充我的表。

tablePopulate = [
    {id: ‘1’, name: ‘Jimmy’, progress: ’10%’, color: ‘blue’},
    {id: ‘2’, name: ‘John’, progress: ’40%’, color: ‘yellow’},
    {id: ‘3’, name: ‘Wright’, progress: ’70%’, color: ‘orange’}
  ];

这是一个表格组件的示例:

https://stackblitz.com/angular/dnbermjydavk?file=app%2Ftable-overview-example.ts

那么我怎样才能用这种数组对象填充这个表。

提前致谢!

最佳答案

所以,根据您的要求, 让我们以数组格式传递您获取的数据

[
     {id: "1", name: "Jimmy", progress: "10", color: "blue"},
     {id: "2", name: "John", progress: "40", color: "yellow"},
     {id: "3", name: "Wright", progress: "70", color: "orange"}
  ]

从 HTML 中删除 % 作为其附加,您可以将其从 table-overview-example.html 行:20 (看看 here )

在此处传递数组:MatTableDataSource() 如下所示:

import {Component, ViewChild} from '@angular/core';
import {MatPaginator, MatSort, MatTableDataSource} from '@angular/material';

/**
 * @title Data table with sorting, pagination, and filtering.
 */
@Component({
  selector: 'table-overview-example',
  styleUrls: ['table-overview-example.css'],
  templateUrl: 'table-overview-example.html',
})
export class TableOverviewExam  ple {
  displayedColumns = ['id', 'name', 'progress', 'color'];
  dataSource: MatTableDataSource<any>;

  @ViewChild(MatPaginator) paginator: MatPaginator;
  @ViewChild(MatSort) sort: MatSort;

  constructor() {


    // Assign the data to the data source for the table to render
    this.dataSource = new MatTableDataSource([
    {id: "1", name: "Jimmy", progress: "10", color: "blue"},
    {id: "2", name: "John", progress: "40", color: "yellow"},
    {id: "3", name: "Wright", progress: "70", color: "orange"}
  ]);
  }

  /**
   * Set the paginator and sort after the view init since this component will
   * be able to query its view for the initialized paginator and sort.
   */
  ngAfterViewInit() {
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;
  }

  applyFilter(filterValue: string) {
    filterValue = filterValue.trim(); // Remove whitespace
    filterValue = filterValue.toLowerCase(); // Datasource defaults to lowercase matches
    this.dataSource.filter = filterValue;
  }
}



/** Constants used to fill up our data base. */
const COLORS = ['maroon', 'red', 'orange', 'yellow', 'olive', 'green', 'purple',
  'fuchsia', 'lime', 'teal', 'aqua', 'blue', 'navy', 'black', 'gray'];
const NAMES = ['Maia', 'Asher', 'Olivia', 'Atticus', 'Amelia', 'Jack',
  'Charlotte', 'Theodore', 'Isla', 'Oliver', 'Isabella', 'Jasper',
  'Cora', 'Levi', 'Violet', 'Arthur', 'Mia', 'Thomas', 'Elizabeth'];

这是一个工作 Demo .

关于 Angular Material 2数据表组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48584622/

相关文章:

angular - ng 测试没有 Http 的提供者!错误

html - 将旋转后的 div 对齐到右侧

typescript - 如何在带有 TypeScript 的 Next.js 中将 next-auth 与每页布局一起使用?

reactjs - 当非抽象类可以正常工作时,为什么不能使用 TypeScript 抽象类的类型来创建 React 工厂?

angular-material - WebStorm @angular/material 数据表模板错误

javascript - Angular Material 的登录表单

javascript - 访问 RxJs Observable 方法调用返回的数据

javascript - angular2 http.post() 到本地 json 文件

javascript - ReactJS 与 TypeScript 未渲染到 DOM

Angular : How two open two mat-menu in one HTML?