angular 7 datatables with observable data from server with pagination (not ajax)

angular 7 datatables with observable data from server with pagination (not ajax)

MishaBeskinMishaBeskin Posts: 2Questions: 1Answers: 0
edited July 2019 in Free community support

Hello,
I using Angular 7 and data-tables.
I want to use data-table with server-side and paging but use observable data that come from a service

For example:
the ajax call is used like that:

ngOnInit(): void {
    const that = this;
    this.dtOptions = {
      pagingType: 'full_numbers',
      responsive: true,
      serverSide: true,
      processing: true,
      
     ** ajax: (dataTablesParameters: any, callback) => {
        that.http
          .post<DataTablesResponse>(
            'https://angular-datatables-demo-server.herokuapp.com/',
            dataTablesParameters, {}
          ).**subscribe(resp => {
            that.persons = resp.data;

I need to use/using

ngOnInit(): void {
    const that = this;
    this.dtOptions = {
      pagingType: 'full_numbers',
      responsive: true,
      serverSide: true,
      processing: true,
      
      this.subscription = this.controllerService.getAdminControllers().subscribe(
      (controllers:{pagination?: Pagination, data?: Controller[]}) => {
        this.controllers = controllers.data;
        console.log(this.controllers);
        this.isWorking = false;
        this.rerender();
      });

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • MishaBeskinMishaBeskin Posts: 2Questions: 1Answers: 0

    Hi all,
    I finally found the solution to my question.
    It is simple as it looks :smile:

    So when you using Server-side data-tables with angular,
    In the.ts component file when using ajax instead make a direct call to some URL.
    Just use a service that returns the observable (in my case).

    Code snippet:

     this.dtOptions.ajax = (dataTablesParameters: any, callback) => {
      that.controllerService.getAdminControllers(this.dtOptions.pageLength, dataTablesParameters.start).subscribe(resp => {
          that.data = resp.data;
          callback({
            recordsTotal: resp.pagination.totalCount,
            recordsFiltered: resp.pagination.totalCount,
            data: []
    
          });
          console.log(that.data);
        });
    };
    
  • umeshchandra07109umeshchandra07109 Posts: 1Questions: 0Answers: 0

    How to do it for Angular 7 server side pagination with get method

This discussion has been closed.