2017-09-11 63 views
1

ng2-ya-table文档的数据源功能,这样写的:如何在ng2-ya表中使用datasoure?

public datasource: any = (request: any): Observable<any> => { 
return this.service.getUsers(request); 
} 

而且像这样使用:

​​

我不希望因为我必须要使用此功能以这种方式静态

data = [ 
    { 
     name: 'Patricia', 
     email: '[email protected]', 
     username: 'Yes', 
    }, 
    { 
     name: 'Chelsey Dietrich', 
     email: '[email protected]', 
     username: 'No', 
    } 
] 

是否有可能或者我有义务呈现可观察类型? 我试图用静态数据很多,但白白

public datasource: any = { 
    return this.data ; 
} 

为什么这个功能不工作?

+0

希望有人回答你......我对这个模块同样的问题,文件很糟糕 – TSG

回答

0

与尝试:

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) 
    }); 
} 
+0

最新版本现在支持本地数据源。 – vitocmpl

0

我想:

public datasource: any = (request: any): Observable<any> => { 
    return Observable.of(this.data); 
} 

但它会导致错误的级联开始:

Ng2YaTableComponent.html:53 ERROR TypeError: Cannot read property 'length' of undefined

如果有人能改善这个答案也许我们能够找到一个解决方案