角度材料マット - ソート機能のコードを書くのは混乱します。私の変数dataSourceを宣言している次のエラーを取得する:MatSortの実装 - 引数が割り当てられない
引数 'Observable'は 'any []'型のパラメータに代入できません。プロパティーの長さが 'Observable'タイプにありません。
私は何を試すことができますか?以下は、私のcomponent.tsのコードおよび関連service.ts
component.ts
import { Component, OnInit, ViewEncapsulation, ViewChild } from '@angular/core';
import { Location } from '@angular/common';
import { DataSource } from '@angular/cdk/collections';
import { Observable } from 'rxjs/Observable';
import { MatTableDataSource, MatSort } from '@angular/material';
import { Report } from './../../models/report';
import { RepGenService } from './../../services/rep-gen.service';
@Component({
selector: 'app-rep-gen-report',
templateUrl: './rep-gen-report.component.html',
styleUrls: ['./rep-gen-report.component.css'],
encapsulation: ViewEncapsulation.None
})
export class RepGenReportComponent implements OnInit {
reports: Report[];
dataSource = new MyDataSource(this.repGenService);
//dataSource = new MatTableDataSource(this.repGenService.getReport()); // <--- error, not working
displayedColumns = ['report_id', 'caption'];
constructor(private repGenService: RepGenService, private location: Location) { }
ngOnInit() {
this.getRepGen_Report();
}
getRepGen_Report(): void {
this.repGenService.getReport()
.subscribe(reports => this.reports = reports);
}
save(reports: Report[]) {
this.repGenService.setReport(this.reports);
}
goBack(): void {
this.location.back();
}
//@ViewChild(MatSort) sort: MatSort;
//ngAfterViewInit() {
// this.dataSource.sort = this.sort; // therefor I need the imported MatTableDataSource
//}
}
export class MyDataSource extends DataSource<any> {
constructor(private repGenService: RepGenService) {
super();
}
connect(): Observable<Report[]> {
return this.repGenService.getReport();
}
disconnect() { }
}
service.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
@Injectable()
export class RepGenService {
constructor(public http: HttpClient) { }
getReport(): Observable<any> {
return this.http.get('/api/repGen_Report')
.map(response => response);
}
setReport(reports) {
this.http.post('/api/repGen_Report/update', reports
).subscribe();
}
}
感謝を見つけることができます!
EDIT:私は
また、あなたは何をした。あなたはDataSourceレスポンス 'this.repGenService.getReport()'の中を渡しましたが、Arrayではありません。仕様:https://material.angular.io/guide/cdk-tableを参照してください。上記のエラーが表示されるのはなぜですか? ''Observable'型の引数は 'any []'型のパラメータに代入できません。プロパティーの長さが 'Observable'の型にありません。 –
このコードはどこで記述する必要がありますか? – dafna
私の答えを再フォーマットするつもりです。 –