简体   繁体   English

错误TS2345:类型'UserDataSource'的参数不能分配给类型'{} []'的参数

[英]error TS2345: Argument of type 'UserDataSource' is not assignable to parameter of type '{}[]'

I need to sort the table but MatTableDataSource does not work ... 我需要对表进行排序,但MatTableDataSource不起作用......

I can't figure out how to pass an array to MatTableDataSource I need the data in the table to be displayed and sorted 我无法弄清楚如何将数组传递给MatTableDataSource我需要显示和排序表中的数据

//This is Component.ts

export class TestSortTableComponent implements OnInit {

displayedColumns = ['fullName', 'birthDate', 'phone', 'email', 'skype', 
'position', 'startDate', 'endDate', 'action'];

data: UserDataSource ;

dataSource = new MatTableDataSource(this.data);
@ViewChild(MatSort) sort: MatSort;
constructor(
private userService: UserService,
private activatedRoute: ActivatedRoute,
private tableQueryParams: TableQueryParamsService,
private httpService: UserService,
private router: Router
)  {}

ngOnInit() {  
this.data = new UserDataSource(this.userService, this.activatedRoute);
}
}

//this is html:

<table mat-table [dataSource]="dataSource" matSort class="mat-elevation- 
z8">

<ng-container matColumnDef="fullName">
<mat-header-cell *matHeaderCellDef >Full name</mat-header-cell>
<mat-cell *matCellDef="let user">{{ user.fullName }}</mat-cell>
</ng-container>

<ng-container matColumnDef="birthDate">
<mat-header-cell *matHeaderCellDef mat-sort-header>Birthday</mat-header- 
cell>
<mat-cell *matCellDef="let user">{{ user.birthDate | date }}</mat-cell>
</ng-container>

<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-header-row *matRowDef="let row; columns: displayedColumns"></mat- 
header-row>
</table>



//this is UserDataSource.ts:


export class UserDataSource implements DataSource<UserModel> {
[x: string]: any;
public usersSubject = new BehaviorSubject<PagedContentModel>({
    items: [],
    total: 0
});
private queryParamsSubscriber: Subscription;
paginator: any;
sort: any;


constructor(
    private userService: UserService,
    private activatedRoute: ActivatedRoute,

) { }

public connect(collectionViewer: CollectionViewer): 
Observable<UserModel[]> {
    this.queryParamsSubscriber = 
this.activatedRoute.queryParamMap.subscribe((paramMap) => {
        this.loadUsers(
            +paramMap.get('page'),
            paramMap.get('sortBy'),
            paramMap.get('sortOrder'),
            paramMap.get('gender'),
            paramMap.get('startDate'),
            paramMap.get('endDate'),
            paramMap.get('keyWord'),

        );
    });
    return this.usersSubject.asObservable()
        .pipe(
            map((response) => response.items)
        );
 }

 disconnect(collectionViewer: CollectionViewer) {
    this.queryParamsSubscriber.unsubscribe();
    this.usersSubject.complete();
 }

 getTotalObservable(): Observable<number> {
    return this.usersSubject.asObservable()
        .pipe(
            map((response) => response.total)
        );
 }

 private loadUsers(page = 0, sortBy = '', sortOrder = 'asc', gender, 
 startDate, endDate,keyWord) {
    const f = {
        gender,
        startDate,
        endDate,
        keyWord
    };
    this.userService.getUsers(page, sortBy, sortOrder, 
f).subscribe((response) => {
        this.usersSubject.next(response);
    });
}
}


//this is getUsers method:

getUsers(page = 0, sortBy = '', sortOrder = 'asc', filter): 
Observable<any> {
let params: HttpParams = new HttpParams()
  .set('page', page ? page.toString() : '0')
  .set('sortBy', sortBy || '')
  .set('sortOrder', sortOrder || '');

if (filter) {
  for (let key in filter) {
    let value = filter[key];
    if(value){

      params = params.set(key, value);
    }
  }
 }

 return this.http.get(`${environment.apiBaseUrl}/employees`, { params });

 }

The problem is that I can not transfer the desired list in MatTableDataSource and because of this sorting does not work. 问题是我无法在MatTableDataSource中传输所需的列表,因为这种排序不起作用。

In the official documentation Angular Material Everything looks very simple, but I ran into unknown problems that are most likely caused by UserDataSource. 在官方文档Angular Material Everything看起来非常简单,但我遇到了很可能由UserDataSource引起的未知问题。

The dataSource instanciation: dataSource实例:

dataSource = new MatTableDataSource(this.data);

sould be done after the data instanciation: 可以在data实例化后完成:

this.data = new UserDataSource(this.userService, this.activatedRoute);

I don't know if it fully solved your problem but it is a first step :) 我不知道它是否完全解决了你的问题,但这是第一步:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误TS2345:“菜单”类型的参数无法分配给类型参数 - error TS2345: Argument of type 'Menu' is not assignable to parameter of type 打字稿错误 TS2345 错误:TS2345:“缓冲区”类型的参数不可分配给“字符串”类型的参数 - Typescript error TS2345 Error: TS2345:Argument of type 'Buffer' is not assignable to parameter of type 'string' TS2345类型的参数…无法分配给类型的参数 - TS2345 Argument of type … Is not assignable to parameter of type TS2345:“HTMLElement”类型的参数不可分配给“ChartItem”类型的参数 - TS2345: Argument of type 'HTMLElement' is not assignable to parameter of type 'ChartItem' TS2345:“RolesGuard”类型的参数不可分配给“CanActivate”类型的参数 - TS2345: Argument of type 'RolesGuard' is not assignable to parameter of type 'CanActivate' TS2345:“ReactNode”类型的参数不可分配给“ReactElement”类型的参数 - TS2345: Argument of type 'ReactNode' is not assignable to parameter of type 'ReactElement' TS2345:类型X的参数不能分配给类型Y的参数 - TS2345: Argument of type X is not assignable to parameter of type Y TS2345:“字符串”类型的参数不可分配给“从不”类型的参数 - TS2345: Argument of type 'string' is not assignable to parameter of type 'never' 错误TS2345:类型为&#39;{x:number; y:数字; z:数字; }&#39;不可分配给&#39;string&#39;类型的参数 - error TS2345: Argument of type '{ x: number; y: number; z: number; }' is not assignable to parameter of type 'string' 错误TS2345:类型&#39;T&#39;的参数不能分配给&#39;object&#39;类型的参数 - error TS2345: Argument of type 'T' is not assignable to parameter of type 'object'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM