我使用mat-table列出用户所选语言的内容 . 他们还可以使用对话框面板添加新语言 . 他们添加了一种语言并返回 . 我希望我的数据源刷新以显示他们所做的更改 .
我通过从服务获取用户数据并将其传递到refresh方法中的数据源来初始化数据存储区 .
Language.component.ts
import { Component, OnInit } from '@angular/core';
import { LanguageModel, LANGUAGE_DATA } from '../../../../models/language.model';
import { LanguageAddComponent } from './language-add/language-add.component';
import { AuthService } from '../../../../services/auth.service';
import { LanguageDataSource } from './language-data-source';
import { LevelbarComponent } from '../../../../directives/levelbar/levelbar.component';
import { DataSource } from '@angular/cdk/collections';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import { MatSnackBar, MatDialog } from '@angular/material';
@Component({
selector: 'app-language',
templateUrl: './language.component.html',
styleUrls: ['./language.component.scss']
})
export class LanguageComponent implements OnInit {
displayedColumns = ['name', 'native', 'code', 'level'];
teachDS: any;
user: any;
constructor(private authService: AuthService, private dialog: MatDialog) { }
ngOnInit() {
this.refresh();
}
add() {
this.dialog.open(LanguageAddComponent, {
data: { user: this.user },
}).afterClosed().subscribe(result => {
this.refresh();
});
}
refresh() {
this.authService.getAuthenticatedUser().subscribe((res) => {
this.user = res;
this.teachDS = new LanguageDataSource(this.user.profile.languages.teach);
});
}
}
language-data-source.ts
import {MatPaginator, MatSort} from '@angular/material';
import {DataSource} from '@angular/cdk/collections';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/merge';
import 'rxjs/add/operator/map';
export class LanguageDataSource extends DataSource<any> {
constructor(private languages) {
super();
}
connect(): Observable<any> {
return Observable.of(this.languages);
}
disconnect() {
// No-op
}
}
所以我试图调用一个刷新方法,我再次从后端获取用户,然后重新初始化数据源 . 但是这不起作用,不会发生任何变化 .
14 回答
最好的方法是在Datasource实现中添加一个额外的observable .
你应该已经使用
Observable.merge
订阅可观,其中包括了paginator.page,sort.sortChange等阵列connect方法,您可以添加一个新的课题,以这一点,接下来请它时,你需要引起刷新 .这样的事情:
然后你可以调用
recordChange$.next()
来启动刷新 .当然,我会将调用包装在refresh()方法中,并从组件中的数据源实例和其他适当的技术中调用它 .
在接收到新数据后,在
refresh()
方法中使用ChangeDetectorRef触发更改检测,在构造函数中注入ChangeDetectorRef并使用detectChanges,如下所示:我不知道在创建问题时是否需要
ChangeDetectorRef
,但现在这已经足够了:所以对我来说,没有人能够很好地回答我遇到的问题,这个问题几乎与@Kay相同 . 对我来说这是关于排序,排序表不会发生垫子的变化 . 我的目的是这个答案,因为它是我通过搜索谷歌找到的唯一主题 . 我正在使用Angular 6 .
如上所述here:
因此,您只需在refresh()方法中调用renderRows()即可显示更改 .
有关集成,请参见here .
由于您使用的是MatPaginator,因此您只需要对paginator进行任何更改,这会触发数据重新加载 .
简单的技巧:
这会将页面大小更新为当前页面大小,因此基本上没有任何更改,除了私有
_emitPageEvent()
函数也被调用,触发表重新加载 .在添加或删除特定行的操作下方添加此行 .
在我的情况下(Angular 6),我继承自
MatTableDataSource
以创建MyDataSource
. Without calling 在this.data = someArray
之后数据 not displayed
class MyDataSource
这对我有用:
我认为
MatTableDataSource
对象与您传递给MatTableDataSource
构造函数的数据数组有某种联系方式 .例如:
所以,当你必须改变数据;更改原始列表
dataTable
,然后在tableDS
上通过调用_updateChangeSubscription()
方法反映表上的更改 .例如:
这是通过Angular 6与我合作的 .
这对我有用:
所以你应该将你的数据源实例声明为
MatTableDataSource
我做了一些研究,发现这个地方给了我需要的东西 - 感觉很干净,并且与从服务器刷新时的更新数据有关:https://blog.angular-university.io/angular-material-data-table/
大多数信用到上面的页面 . 下面是一个示例,说明如何在更改选择时使用mat-selector更新绑定到数据源的mat-table . 我正在使用Angular 7.很抱歉因为广泛,试图完整但简洁 - 我已尽可能多地删除了不需要的部分 . 希望能帮助别人更快地前进!
organization.model.ts:
organization.service.ts:
organizationdatasource.model.ts:
organizations.component.html:
organizations.component.scss:
organization.component.ts:
您可以使用“concat”轻松更新表的数据:
例如:
language.component.ts
language.component.html
并且,当您更新数据(language.component.ts)时:
当你使用“concat”角度检测对象的变化时(this.teachDS),你不需要使用其他东西 .
PD:它在角度6和7中对我有效,我没有尝试另一个版本 .
您可以使用数据源连接功能
像这样 . 'data'是数据表的新值