首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角度材料表构件在第一次加载时不显示数据

角度材料表构件在第一次加载时不显示数据
EN

Stack Overflow用户
提问于 2021-07-16 07:08:05
回答 1查看 215关注 0票数 0

我尝试使用角度材料表来列出我使用websocket从后端服务器获得的一些信息,但它只在我单击displayedColumns进行排序时才显示元素,而不是在初始加载时显示。

下面是我的数据表组件代码:

代码语言:javascript
复制
import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { MatTable } from '@angular/material/table';
import { observable, of as observableOf } from 'rxjs';
import { Observable } from 'rxjs/internal/Observable';
import { FtpClientComponent } from '../ftp-client/ftp-client.component';
import { FtpClientWsService } from '../services/ftp/ftp-client-ws.service';
import { DataTableDataSource, DataTableItem } from './data-table-datasource';

@Component({
  selector: 'app-data-table',
  templateUrl: './data-table.component.html',
  styleUrls: ['./data-table.component.css']
})
export class DataTableComponent implements AfterViewInit{
  @ViewChild(MatPaginator) paginator!: MatPaginator;
  @ViewChild(MatSort) sort!: MatSort;
  @ViewChild(MatTable) table!: MatTable<DataTableItem>;
  dataSource: DataTableDataSource;






  /** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */


  displayedColumns = ['sftpName', 'sftpCreationDate'];

  constructor(data:FtpClientComponent) {
    this.dataSource =  new DataTableDataSource(data);

  }




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

以下是数据源类型脚本:

代码语言:javascript
复制
import { DataSource } from '@angular/cdk/collections';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { map } from 'rxjs/operators';
import { Observable, of as observableOf, merge } from 'rxjs';
import { FtpClientComponent } from '../ftp-client/ftp-client.component';

// TODO: Replace this with your own data model type
export interface DataTableItem {
  sftpName: string;
  sftpCreationDate: string;
  sftpPassword: string;
  sftpUsedSpace: string;
  sftpAllocatedSpace: string;
  sftpLastModification: string;
}

// TODO: replace this with real data from your application
// const EXAMPLE_DATA: any[] = [];

/**
 * Data source for the DataTable view. This class should
 * encapsulate all logic for fetching and manipulating the displayed data
 * (including sorting, pagination, and filtering).
 */
export class DataTableDataSource extends DataSource<DataTableItem> {
  data: any[];
  paginator: MatPaginator | undefined;
  sort: MatSort | undefined;

  constructor(data:FtpClientComponent) {
    super();
    this.data = data.receivedSftps

  }

  /**
   * Connect this data source to the table. The table will only update when
   * the returned stream emits new items.
   * @returns A stream of the items to be rendered.
   */
  connect(): Observable<DataTableItem[]> {
    if (this.paginator && this.sort) {
      // Combine everything that affects the rendered data into one update
      // stream for the data-table to consume.
      return merge(observableOf(this.data), this.paginator.page, this.sort.sortChange)
        .pipe(map(() => {
          return this.getPagedData(this.getSortedData([...this.data ]));
        }));
    } else {
      throw Error('Please set the paginator and sort on the data source before connecting.');
    }
  }

  /**
   *  Called when the table is being destroyed. Use this function, to clean up
   * any open connections or free any held resources that were set up during connect.
   */
  disconnect(): void {}

  /**
   * Paginate the data (client-side). If you're using server-side pagination,
   * this would be replaced by requesting the appropriate data from the server.
   */
  private getPagedData(data: DataTableItem[]): DataTableItem[] {
    if (this.paginator) {
      const startIndex = this.paginator.pageIndex * this.paginator.pageSize;
      return data.splice(startIndex, this.paginator.pageSize);
    } else {
      return data;
    }
  }

  /**
   * Sort the data (client-side). If you're using server-side sorting,
   * this would be replaced by requesting the appropriate data from the server.
   */
  private getSortedData(data: DataTableItem[]): DataTableItem[] {
    if (!this.sort || !this.sort.active || this.sort.direction === '') {
      return data;
    }

    return data.sort((a, b) => {
      const isAsc = this.sort?.direction === 'asc';
      switch (this.sort?.active) {
        case 'sftpName': return compare(a.sftpName, b.sftpName, isAsc);
        case 'sftpCreationDate': return compare(+a.sftpName, +b.sftpName, isAsc);
        default: return 0;
      }
    });
  }
}

/** Simple sort comparator for example ID/Name columns (for client-side sorting). */
function compare(a: string | number, b: string | number, isAsc: boolean): number {
  return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
}

而ftp-client组件只使用一个服务,该服务打开一个websocket连接来检索数据,然后将这些数据存储在列表中。

代码语言:javascript
复制
import { Component, OnInit } from '@angular/core';
import { FtpClientWsService } from '../services/ftp/ftp-client-ws.service';

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

  constructor(
    public SftpWsService: FtpClientWsService
  ) { }

  ngOnInit(): void {
    this.SftpWsService.openWebSocket();
  }


  receivedSftps = this.SftpWsService.sftps;


}

这个列表只有在我单击排序标题时才会出现,因为我是Angular的新手,所以非常欢迎任何反馈

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-25 06:17:32

在设置数据源值之前向服务中的主题添加ngOninit()的subcription解决了该问题

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68401715

复制
相关文章

相似问题

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