Using a static array as datasource for mat-table

前端 未结 2 392
猫巷女王i
猫巷女王i 2020-12-21 02:36

I\'m trying to make use of the Angular Material table. I\'m trying to use the same code as the examples they have but I tin into a problem when I have to define the [d

2条回答
  •  伪装坚强ぢ
    2020-12-21 03:21

    even if you use static datas you will have to instantiate a new DataSource.

    mat-table always need a real DataSource in @Input. DataSource is an abstract class, so you have to implement a class who inherit from DataSource.

    This DataSource must have a method "connect" implemented which will return an Observable of the data you want to display.

    So in your case something like that should work ;

    // in your component
    
    interface MyDataType {
        ID: number;
        Code: string;
    }
    
    export class StaticDataSource extends DataSource {
      constructor(private staticData: MyDataType[]) {
        super();
      }
    
      connect(): Observable {
        return Observable.of(this.staticData);
      }
    
      disconnect() {}
    }
    ...
    this.staticDataSource = new StaticDataSource(data);
    
    // in your template
     
    

提交回复
热议问题