0

I am working on an Angular5 project with PHP as backend. I am stuck at this level. The following is my code:

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' }]
};

As I observed in this code I am sending the default parameters of data tables "dataTablesParameters". How can I add my custom parameter like (entryid) alongwith "dataTablesParameters"?

Please guide.

2 Answers 2

6

You have to just declare object like:

userData = { token: '', entryid : '' };

and at the time of calling ajax, you should merge both objects using Object.assign like:

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: []
            });
          });
      },

Check this complete tutorial for you better understanding. Angular 5 Datatables using JSON data with rerender

Sign up to request clarification or add additional context in comments.

Comments

1

I believe there's no way around creating a parent object as follows:

ParentObj = { dataTablesParameters: _dataTablesParameters , entryid:_entryid }

Edit: So your code will look like this

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' }]
};

Send the parentObj instead of the dataTablesParameter you have inside your http request.

4 Comments

Could you please share a complete example?
Okay Thank you let me check.
Thank you. But still a little confused about _dataTablesParameters. How can I assign dataTablesParameters body to _dataTablesParameters?
@CodeLover Try doing this inside your component initialization . ngOnInit(): void { this.dataTablesParameters = //whatever you want it to be}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.