【问题标题】:angular material2 data-table componentangular material2 数据表组件
【发布时间】:2018-07-13 01:39:40
【问题描述】:

我正在尝试从角度材料组件实现一个表格组件,并且一切都很好而且看起来不错,最大的问题是如何使用来自 DB 的动态数据填充表格。 我从 DB 收到了一个像本例中一样的对象数组,但我真的不知道如何迭代它并填充我的表。

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

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

提前致谢!

【问题讨论】:

  • @saikatchakrabortty 工作得很好,您能否还请清除 createNewUser 函数并删除这些常量并添加作为答案以获得积分,在这个 cmets 部分我不能接受作为最佳响应。
  • 让我这样做,:)

标签: angular typescript angular-material


【解决方案1】:

所以,根据您的要求, 让我们在你得到的时候传递数据,以数组格式

[
     {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 Line: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

【讨论】:

  • 只是一件小事,似乎是这一行的问题:dataSource: MatTableDataSource;找不到名称“用户数据”:|
  • 哎呀,感谢您的更正,更新了答案。请再检查一次。
猜你喜欢
  • 2017-08-18
  • 1970-01-01
  • 2018-01-19
  • 2018-06-27
  • 2017-07-13
  • 1970-01-01
  • 1970-01-01
  • 2017-06-17
  • 2017-09-24
相关资源
最近更新 更多