如何在 ng2-ya-table 中使用数据源?
How to use datasoure in ng2-ya-table?
在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(this.data);
}
但它会导致一连串错误,开头为:
Ng2YaTableComponent.html:53 ERROR TypeError: Cannot read property
'length' of undefined
如果有人可以改进这个答案,也许我们可以找到解决方案
试试:
public datasource: any = (request: any): Observable<any> => {
return Observable.of({
recordsTotal: this.data.length,
recordsFiltered: this.data.length,
data: this.data
});
}
无论如何,您需要在客户端执行分页、排序和过滤(数据源是一个 Observable,以便在服务器端执行此操作)。
例如(仅分页):
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)
});
}
在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(this.data);
}
但它会导致一连串错误,开头为:
Ng2YaTableComponent.html:53 ERROR TypeError: Cannot read property 'length' of undefined
如果有人可以改进这个答案,也许我们可以找到解决方案
试试:
public datasource: any = (request: any): Observable<any> => {
return Observable.of({
recordsTotal: this.data.length,
recordsFiltered: this.data.length,
data: this.data
});
}
无论如何,您需要在客户端执行分页、排序和过滤(数据源是一个 Observable,以便在服务器端执行此操作)。 例如(仅分页):
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)
});
}