Hey there! I'm trying to set options for my Datatable and add a new field in my objects, but I need to await the completion of these dtOptions. How can I achieve this in the ngOnInit lifecycle hook?
export class MyDashboardComponent implements OnInit {
grades: any = [];
dtOptions: DataTables.Settings = {};
buffer: any = "";
constructor(private apiService: ApiService, private _auth: AuthService, private http: HttpClient) {
}
async ngOnInit(): Promise<void> {
this.dtOptions = {
order: [[0, "desc"]],
responsive: true,
pagingType: 'full_numbers',
pageLength: 2,
serverSide: true,
processing: true,
ajax: (dataTablesParameters: any, callback) => {
this.http
.post<DataTablesResponse>('http://localhost:4000/grade/readByStudentId', {
login: this._auth.getUserData().login,
dataTablesParameters
}).subscribe(resp => {
this.grades = resp.data;
callback({
recordsTotal: resp.recordsTotal,
recordsFiltered: resp.recordsFiltered,
data: []
});
});
},
columns: [{data: 'id'}, {data: 'firstName'}, {data: 'lastName'}]
};
await this.grades.forEach(grade => {
this.getTpName(grade.tp_id)
.pipe(take(1))
.subscribe(tpName => {
grade.push({tpName : tpName});
});
});
}