簡體   English   中英

角材料2表

[英]Angular Material2 Table

我有一個簡單的表來顯示靜態數據,但是,通過input來顯示它有問題。

問題是如何使用item輸入將數據傳遞到material2表。

export class InvoiceItemComponent implements OnInit {
  displayedColumns = ['id', 'description', 'amount', 'taxGroup'];
  exampleDatabase = new ExampleDataBase();
  dataSource: ExampleDataSource | null;
  @Input() item;

  constructor() {}

  ngOnInit() {
    console.log(this.exampleDatabase.data);
    this.dataSource = new ExampleDataSource(this.exampleDatabase);
  }
}

export class ExampleDataBase {
  dataChange: BehaviorSubject<any> = new BehaviorSubject([]);
   get data(): InvoiceItem[] {
    return [ // works fine with static data, cannot figure out how to pass it via @Input() item which is the required data
      {
        id: 1,
        description: 'your desc here',
        amount: 20,
        taxAmount: 20,
        taxGroup: {
          id: 1,
          name: 'tax group name'
        }
      },
    ];
  }

  constructor() {
    this.dataChange.next(this.data);
  }
}

export class ExampleDataSource extends DataSource<any> {
  constructor(private _exampleDatabase: ExampleDataBase) {
    super();
  }

  /** Connect function called by the table to retrieve one stream containing the data to render. */
  connect(): Observable<InvoiceItem[]> {
    console.log(this._exampleDatabase.dataChange);

    return this._exampleDatabase.dataChange;
  }

  disconnect() {}
}

也許嘗試這樣的事情

export class InvoiceItemComponent implements OnInit {

  displayedColumns = ['id', 'description', 'amount', 'taxGroup'];

  dataSource: InvoiceItemDataSource | null;

  @Input() set items(vals: InvoiceItem[]) {
    this.itemSubject.next(vals);
  }

  /** Emits a new array of invoice items whenver `@Input() items` changes */
  itemSubject = new BehaviorSubject<InvoiceItem[]>([]);

  constructor() {}

  ngOnInit() {
    // Pass the item subject as an observable which can be connected directly
    // to the data source's `connect()`
    this.dataSource = new InvoiceItemDataSource(this.itemSubject.asObservable());
  }
}

export class InvoiceItemDataSource extends DataSource<any> {
  constructor(private data: Observable<InvoiceItem[]>) {
    super();
  }

  connect(): Observable<InvoiceItem[]> {
    return this.data;
  }

  disconnect() {}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM