2017-09-11 66 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

如果有人能改善這個答案也許我們能夠找到一個解決方案