首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >远程dataSource不显示在带有MatTableDataSource的席表中

远程dataSource不显示在带有MatTableDataSource的席表中
EN

Stack Overflow用户
提问于 2018-10-02 12:19:24
回答 2查看 442关注 0票数 0

我试图用远程服务器上的mat-tabledataSource来实现一个组件。但数据在表中不可见。

html

代码语言:javascript
复制
    <div class="mat-elevation-z8">
  <mat-form-field>
    <input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
  </mat-form-field>

  <table mat-table [dataSource]="dataSource" matSort style="width: 100%;">

    <ng-container matColumnDef="accountType">
      <th mat-header-cell *matHeaderCellDef mat-sort-header> Account Type </th>
      <td mat-cell *matCellDef="let e"> {{e.accountType}} </td>
    </ng-container>

    <ng-container matColumnDef="id">
      <th mat-header-cell *matHeaderCellDef mat-sort-header> ID </th>
      <td mat-cell *matCellDef="let e"> {{e.id}} </td>
    </ng-container>

    <tr mat-header-row *matHeaderRowDef="columnsToDisplay"></tr>
    <tr mat-row *matRowDef="let row; columns: columnsToDisplay;"></tr>
  </table>
  <mat-paginator [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator>
</div>

ts

代码语言:javascript
复制
import { Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator, MatTableDataSource, MatSort } from '@angular/material';

import { HeadAccount } from '../../Data/HeadAccount';
import { HeadAccountsService } from '../../Services/head-accounts.service';

import { AfterViewInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-head-accounts',
  templateUrl: './head-accounts.component.html',
  styleUrls: ['./head-accounts.component.css']
})
export class HeadAccountsComponent implements OnInit {

  constructor(private haser: HeadAccountsService) { }

  ngOnInit() {
    this.gets();
  }

  ngAfterViewInit() {
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;
  }

  has: HeadAccount[];
  dataSource = new MatTableDataSource(this.has);
  columnsToDisplay: string[] = ['accountType', 'id'];
  @ViewChild(MatPaginator) paginator: MatPaginator;
  @ViewChild(MatSort) sort: MatSort;

  gets(): void {
    this.haser.gets()
      .subscribe(has => this.has = has);
  }
  applyFilter(filterValue: string) {
    this.dataSource.filter = filterValue.trim().toLowerCase();
  }
}

但是,如果我直接将dataSource更改为has (如[dataSource]="has"),则可以看到[dataSource]="has"数据,但其他功能(如pagination, sort, filter )是可见的。

在App中,MatTableModule, MatPaginatorModule, MatSortModule是导入和导出

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-08 15:55:18

将代码从ngAfterViewInit()转移到服务的.subscribe对我有用。

在片段下面工作。

代码语言:javascript
复制
this.haser.gets()
  .subscribe(has => {
    this.dataSource = new MatTableDataSource(has);
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;
  });
票数 0
EN

Stack Overflow用户

发布于 2018-10-02 23:59:46

我导入了目录库并做了一个_.castArray,我想...data也可以工作,但是我选择了提交路径。我还必须将我的排序和分页器封装在一个setTimeout中.但这可能不适用于你。

代码语言:javascript
复制
this.dataSource = new MatTableDataSource(_.castArray(data));
    setTimeout(()=> this.dataSource.sort = this.matSort);
    setTimeout(()=> this.dataSource.paginator = this.paginator);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52608201

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档