[英]Pass value to extends plugin ng2 smart table
我已經檢查了分頁實現的文檔和源代碼(advanced-example-server.component.ts) 。
並發現它使用的ServerDataSource僅通過HTTP GET實現了分頁(_sort、_limit、_page 等參數在 URL 中公開)......因為我當前的項目需要使用 POST 將前端參數發送回-端 Restful API,
使用extends to HTTP post call工具,不知道如何在分頁請求中添加額外的參數。 我需要將 request_server 傳遞給 extendsplugin.ts
import { Observable } from 'rxjs/internal/Observable';
import { ServerDataSource } from 'ng2-smart-table';
export class PostServerDataSource extends ServerDataSource {
protected requestElements(): Observable<any> {
let httpParams = this.createRequesParams();
return this.http.post(this.conf.endPoint, request_server, { observe: 'response' });
}
}
另一個組件.ts
swiftListTable() {
const request_server = { "userType": this.currentUser.role, "authName": this.currentUser.username }
this.source = new PostServerDataSource(this.http,{endPoint: this.service.apiURL + 'swift/pagination', dataKey: 'content', pagerLimitKey:"_limit",
pagerPageKey:"_page",
sortDirKey: "pageable",
sortFieldKey: "pageable",
totalKey:'totalElements'});
}
有兩種方法可以處理它,一種方法是在查詢字符串中附加參數並附加到 url 中,例如,
this.service.apiURL + 'swift/pagination?param1=p¶m2=q'
其他方法可能是在 requestElements 和 swiftListTable 函數中處理它,如下所示。
swiftListTable() {
const request_server = {
"userType": this.currentUser.role,
"authName": this.currentUser.username
}
this.source = new PostServerDataSource(http,
{ endPoint: url, dataKey: 'content', pagerLimitKey:'_limit'}, request_server);
export class PostServerDataSource extends ServerDataSource {
params: any;
constructor(http: HttpClient, config: any, params?: any) {
super(http, config);
this.params = params;
}
protected requestElements(): Observable<any> {
let httpParams = this.createRequesParams();
if (this.params) {
let keys = Object.keys(this.params);
keys.forEach((key) => {
httpParams = httpParams.set(key, this.params[key]);
});
}
return this.http.post(this.conf.endPoint, httpParams, { observe: 'response' });
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.