我正在使用PHP作为后端的Angular5项目。我被困在这个水平。 以下是我的代码:
this.dtOptions = {
pagingType: 'full_numbers',
pageLength: 10,
order: [0, 'desc'],
serverSide: true,
processing: true,
ajax: (dataTablesParameters: any, callback) => {
that.http
.post<DataTablesResponse>(
'http://localhost/api/webapi/',
dataTablesParameters,{ headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'token'
})}
).subscribe(resp => {
that.records = resp.data;
callback({
recordsTotal: resp.recordsTotal,
recordsFiltered: resp.recordsFiltered,
data: []
});
});
},
columns: [{ data: 'id' }, { data: 'name' }]
};
正如我在此代码中观察到的那样,我发送数据表的默认参数&#34; dataTablesParameters&#34;。如何添加我的自定义参数,如(entryid)以及&#34; dataTablesParameters&#34;?
请指导。
答案 0 :(得分:2)
你必须声明像:
这样的对象userData = { token: '', entryid : '' };
并且在调用ajax时,您应该使用Object.assign合并两个对象,如:
Object.assign(dataTablesParameters,this.userData)
ajax: (dataTablesParameters:any, callback) => {
that.http
.post<DataTablesResponse>(
'http://localhost/api/webapi/',Object.assign(dataTablesParameters,this.userData),{}).subscribe(resp => {
that.records = resp.data;
console.log(resp);
callback({
recordsTotal: resp.recordsTotal,
recordsFiltered: resp.recordsFiltered,
data: []
});
});
},
查看此完整教程,以便您更好地理解。 Angular 5 Datatables using JSON data with rerender
答案 1 :(得分:1)
我认为无法创建父对象,如下所示:
ParentObj = { dataTablesParameters: _dataTablesParameters , entryid:_entryid }
修改:所以您的代码将如下所示
declare interface parentObj : { dataTablesParameters: any , entryid: any }
this.ParentObj = { dataTablesParameters: _dataTablesParameters , entryid:_entryid }
this.dtOptions = {
pagingType: 'full_numbers',
pageLength: 10,
order: [0, 'desc'],
serverSide: true,
processing: true,
ajax: (dataTablesParameters: any, callback) => {
that.http
.post<DataTablesResponse>(
'http://localhost/api/webapi/',
parentObj,{ headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'token'
})}
).subscribe(resp => {
that.records = resp.data;
callback({
recordsTotal: resp.recordsTotal,
recordsFiltered: resp.recordsFiltered,
data: []
});
});
},
columns: [{ data: 'id' }, { data: 'name' }]
};
发送parentObj而不是http请求中的dataTablesParameter。