在ng2-ya-table
的文档中,数据源函数以这种方式编写:
public datasource: any = (request: any): Observable<any> => {
return this.service.getUsers(request);
}
并这样使用:
<ng2-ya-table [options]="options" [columns]="columns" [datasource]="datasource" [paging]="paging">
</ng2-ya-table>
我不想以这种方式使用此功能,因为我有静态
data = [
{
name: 'Patricia',
email: 'Julianne.OConner@kory.org',
username: 'Yes',
},
{
name: 'Chelsey Dietrich',
email: 'Lucio_Hettinger@annie.ca',
username: 'No',
}
]
这是可能的,还是我有义务使可观察的类型?我尝试了很多使用静态数据,但徒劳地尝试了
public datasource: any = {
return this.data ;
}
为什么此功能不起作用?
尝试:
public datasource: any = (request: any): Observable<any> => {
return Observable.of({
recordsTotal: this.data.length,
recordsFiltered: this.data.length,
data: this.data
});
}
无论如何,您需要执行分页,分类和过滤客户端(数据源是可观察到的,以执行此操作服务器端)。例如(仅分页):
public datasource: any = (request: any): Observable<any> => {
let page = (request.start / request.length) + 1;
return Observable.of({
recordsTotal: this.data.length,
recordsFiltered: this.data.length,
data: this.data.slice(request.length * (page - 1), request.length * page)
});
}
我尝试了:
public datasource: any = (request: any): Observable<any> => {
return Observable.of(this.data);
}
,但它会引起一系列错误:
ng2yatablecomponent.html:53错误类型:无法读取属性 未定义的"长度"
如果有人可以改善这个答案,也许我们可以找到解决方案