Angular 6数据表不显示数据

时间:2018-12-14 13:59:05

标签: angular datatable material-design

我正在使用Angular 6数据表组件。该表将呈现,但不会显示数据。我正在使用服务,并且在加载表时,我会检查chrome开发人员工具,并从服务中返回数据。

这是我的服务

import { Injectable } from '@angular/core';
import { HttpClient, HttpParams} from '@angular/common/http';
import { CoursesDataTableDataSource } from '../courses/courses-data-table/courses-data-table-datasource';

@Injectable({
  providedIn: 'root'
})

export class CoursesService {

  private serviceUrl = 'https://some-server/CoursesWebApi/api/Courses';

  constructor(private http: HttpClient) { }

  getCourses() {
    return this.http.get<CoursesDataTableDataSource[]>(this.serviceUrl);
  }

  getCourse(params: HttpParams) {
    return this.http.get<CoursesDataTableDataSource[]>(this.serviceUrl, { params: params });
  }
}

这是我的数据表组件:

import { Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator, MatSort } from '@angular/material';
import { CoursesDataTableDataSource } from './courses-data-table-datasource';
import { CoursesService } from '../../services/courses.service';

@Component({
  selector: 'app-courses-data-table',
  templateUrl: './courses-data-table.component.html',
  styleUrls: ['./courses-data-table.component.css'],
})

export class CoursesDataTableComponent implements OnInit {
  @ViewChild(MatPaginator) paginator: MatPaginator;
  @ViewChild(MatSort) sort: MatSort;
  dataSource: CoursesDataTableDataSource;

  displayedColumns = ["CourseId", "AuthorId", "Title", "CourseLength", "Category", "CourseDate"];

  constructor(private coursesService: CoursesService) { }

  ngOnInit() {
    this.dataSource = new CoursesDataTableDataSource(this.coursesService, this.paginator, this.sort)
  }
}

我的数据表源组件:

import { DataSource } from '@angular/cdk/collections';
import { MatPaginator, MatSort } from '@angular/material';
import { map } from 'rxjs/operators';
import { Observable, of as observableOf, merge } from 'rxjs';
import { CoursesService } from '../../services/courses.service';

export interface CoursesDataTableItem {
  CourseId        : number;
  AuthorId        : number;
  Title           : String;
  CourseLength    : String;
  Category        : String;
  CourseDate      : String;
}

export class CoursesDataTableDataSource extends DataSource<CoursesDataTableItem> {

  courseData: CoursesDataTableItem[];

  constructor(private coursesService: CoursesService, private paginator: MatPaginator, private sort: MatSort) {
    super();

    this.courseData = [];
  }

  connect(): Observable<CoursesDataTableItem[]> {
    const dataMutations = [
      this.coursesService.getCourses(),
      this.paginator.page,
      this.sort.sortChange
    ];

    this.paginator.length = this.courseData.length;

    return merge(...dataMutations).pipe(map(() => {
      return this.getPagedData(this.getSortedData([...this.courseData]));
    }));
  }

  disconnect() {}

  private getPagedData(courseData: CoursesDataTableItem[]) {
    const startIndex = this.paginator.pageIndex * this.paginator.pageSize;
    return courseData.splice(startIndex, this.paginator.pageSize);
  }

  private getSortedData(courseData: CoursesDataTableItem[]) {
    if (!this.sort.active || this.sort.direction === '') {
      return courseData;
    }

    return courseData.sort((a, b) => {
      const isAsc = this.sort.direction === 'asc';
      switch (this.sort.active) {
        case 'Title': return compare(a.Title, b.Title, isAsc);
        case 'CourseId': return compare(+a.CourseId, +b.CourseId, isAsc);
        default: return 0;
      }
    });
  }
}

function compare(a, b, isAsc) {
  return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
}

最后是我的html:

<div class="content">
    <h2>Courses</h2>
    <p>Total Courses: {{ dataSource.courseData.length }}</p>

    <mat-form-field>
      <input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
    </mat-form-field>

<div class="mat-elevation-z8">
  <table mat-table class="full-width-table" [dataSource]="dataSource" matSort aria-label="Elements">
    <!-- CourseId Column -->
    <ng-container matColumnDef="CourseId">
      <th mat-header-cell *matHeaderCellDef mat-sort-header>Course Id</th>
      <td mat-cell *matCellDef="let row">{{row.CourseId}}</td>
    </ng-container>

    <!-- AuthorId Column -->
    <ng-container matColumnDef="AuthorId">
      <th mat-header-cell *matHeaderCellDef mat-sort-header>Author Id</th>
      <td mat-cell *matCellDef="let row">{{row.AuthorId}}</td>
    </ng-container>

    <!-- Title Column -->
    <ng-container matColumnDef="Title">
        <th mat-header-cell *matHeaderCellDef mat-sort-header>Title</th>
        <td mat-cell *matCellDef="let row">{{row.Title}}</td>
      </ng-container>

    <!-- CourseLength Column -->
    <ng-container matColumnDef="CourseLength">
        <th mat-header-cell *matHeaderCellDef mat-sort-header>Course Length</th>
        <td mat-cell *matCellDef="let row">{{row.CourseLength}}</td>
      </ng-container>

    <!-- Category Column -->
    <ng-container matColumnDef="Category">
        <th mat-header-cell *matHeaderCellDef mat-sort-header>Category</th>
        <td mat-cell *matCellDef="let row">{{row.Category}}</td>
      </ng-container>

    <!-- CourseDate Column -->
    <ng-container matColumnDef="CourseDate">
        <th mat-header-cell *matHeaderCellDef mat-sort-header>Course Date</th>
        <td mat-cell *matCellDef="let row">{{row.CourseDate}}</td>
      </ng-container>

      <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
      <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
    </table>

  <mat-paginator #paginator
      [length]="dataSource.courseData.length"
      [pageIndex]="0"
      [pageSize]="10"
      [pageSizeOptions]="[10, 25, 50, 100, 250]">
  </mat-paginator>
</div>

以下是Google chrome开发工具中的数据的屏幕截图: enter image description here

不确定我缺少什么。控制台中没有错误,并且表呈现但为空。我查看了其他有关数据未渲染但没有相似之处的文章。

我怀疑数据没有在数据源组件的connect方法中绑定到表或courseData数组:

this.coursesService.getCourses()

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您应该在return语句的map函数中使用可观察到的结果。请查看以下代码:

connect(): Observable<CoursesDataTableItem[]> {
 const dataMutations = [
   this.coursesService.getCourses(),
   this.paginator.page,
   this.sort.sortChange
 ];

 this.paginator.length = this.courseData.length;

 return merge(...dataMutations).pipe(map((data) => {
   return this.getPagedData(this.getSortedData([...data]));
 }));

}