angular-datatables icon indicating copy to clipboard operation
angular-datatables copied to clipboard

dataTable not sending any parameters in angular 6

Open jayantsr83 opened this issue 8 years ago • 58 comments

Hi i have followed the on of your server side angular way tutorial everything is working fine except the parameters, Whenever i do click on pagination the pagination changed but onCallback first 10 data shows every time. this is because when i am hitting api it sends no parameter with it so it always return the same data. This is api which hit every time http://example.test/webapi/?io=masterpro&action=lst&value=&regex=false

as you can see it not sending any parameters with it that's why it always returns the first 10 data as i set the length of data to 10 in my api file


import { Component, OnInit } from '@angular/core';
import { adminLteConf } from '../admin-lte.conf';
import {Http} from "@angular/http";
import { HttpClient, HttpResponse } from '@angular/common/http';

import { HttpHeaders } from '@angular/common/http';
import { DataTableDirective } from 'angular-datatables';

@Component({
  selector: 'app-listmasterlog',
  templateUrl: './listmasterproducts.component.html',
  styleUrls: ['./listmasterproducts.component.css']
})
export class ListmasterproductsComponent implements OnInit {

  public data: any[];
  public filterQuery = "";
  public mfRowsOnPage = 10;
  public mfActivePage: any;
  public sortOrder = "asc";

  dtOptions: DataTables.Settings = {}; 
  metaData:any;


  constructor(private http: Http) { }

  ngOnInit(): void {
    this.dtOptions = {
      //pagingType: 'full_numbers',

      processing: true,
      serverSide: true,

      pageLength:10,
      language: {
        lengthMenu: "_MENU_",
        paginate: {
          first:'',
          last:'',
          next: '', // or '→'
          previous: '' // or '←' 
        }
      },
      dom: '<"bottom"p><"inline-flex" li><"clear">',
      ajax:(dataTablesParameters:any, callback) => {
        this.http.get(
          adminLteConf.APIURL+"?io=masterpro&action=lst",
          dataTablesParameters
        ).subscribe(resp=> {
          var resposnse = resp.json();
          this.data = resposnse['data'];
          //console.log(this.packages);

          callback({
            recordsTotal: resposnse['meta'].total,
            recordsFiltered: resposnse['meta'].pages,
            data: []
          });
        });
      }
      // columns: [
      //   { data: "RecordId"}, {data: 'PackageName'}, {data: 'Price'}
      // ],
    };



  }

}

The above is in my component file I am using angular 6 and i haven't find any tutorial which helped me with this so please show me some path or give me some solution for this. I am stuck in this stuff from last 4 or 5 days and it is very urgent to me

this is the image of my table below

screenshot_1

jayantsr83 avatar Sep 10 '18 06:09 jayantsr83

@jayantsr83 bro i have been looking for solution even more than 3 weeks no positive response

awais2080 avatar Sep 10 '18 06:09 awais2080

Well this make me more tense then :D. I am very much tired with this so what you have done to resolve this

jayantsr83 avatar Sep 10 '18 06:09 jayantsr83

@jayantsr83 it was night mare for me i solved many things related to it even dynamic columns but while integrating server side gives issue and cant find any solution till now how the founder can do that main purpose for this datatable is server side integration else its useless we can not load 5000 members records that will suck the whole system thats why we use server side to get or refresh only those records which are currently on page However, we can ask at javascript datatable or jquery datatable about angular forum

awais2080 avatar Sep 10 '18 06:09 awais2080

Okay i will ask them maybe they can help with this else this will be night mare for me also

jayantsr83 avatar Sep 10 '18 06:09 jayantsr83

Please let me know if you found any solution so i will

jayantsr83 avatar Sep 10 '18 06:09 jayantsr83

Hey @awais2080 my code is working now please tell me if you want that

jayantsr83 avatar Sep 10 '18 12:09 jayantsr83

Hi @jayantsr83 Me too facing the same problem. Could you please tell us how did you resolved?

funlabscloud avatar Sep 18 '18 06:09 funlabscloud

Under Import i have added header

const ParseHeaders = {
  headers: new HttpHeaders({
   'Content-Type'  : 'application/x-www-form-urlencoded',
  })
};`

After that i am passing header in my dtOptions post part

`
ajax:(dataTablesParameters:any, callback) => {
    this.http.post<DataTablesResponse>(
          your-url-here,
          dataTablesParameters,ParseHeaders
        ).subscribe();
}
`

jayantsr83 avatar Sep 18 '18 06:09 jayantsr83

@jayantsr83 brother how did you solve it. i just lost my hope and was busy somewhere else can you plz share me the code i will be very much thankful to you

On Tue, 18 Sep 2018 at 11:50, Jayant [email protected] wrote:

Under Import i have added header

const ParseHeaders = { headers: new HttpHeaders({ 'Content-Type' : 'application/x-www-form-urlencoded', }) };`

After that i am passing header in my dtOptions post part

ajax:(dataTablesParameters:any, callback) => { this.http.post<DataTablesResponse>( your-url-here, dataTablesParameters,ParseHeaders ).subscribe(); }

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/l-lin/angular-datatables/issues/1306#issuecomment-422275813, or mute the thread https://github.com/notifications/unsubscribe-auth/Allvaq1Jkuws6JgY5RZwuIACqlg23TMqks5ucJfRgaJpZM4WgwpS .

awais2080 avatar Sep 18 '18 07:09 awais2080

As i have paste the code above. I am declaring header before component

`const ParseHeaders = { headers: new HttpHeaders({ 'Content-Type' : 'application/x-www-form-urlencoded', }) };``

Then in ajax i am passing that header and that's it. It starts sending parameters of dataTables

ajax:(dataTablesParameters:any, callback) => {
    this.http.post<DataTablesResponse>(
          your-url-here,
          dataTablesParameters,ParseHeaders
        ).subscribe();

jayantsr83 avatar Sep 19 '18 08:09 jayantsr83

Here is the full code of it hope it will help you

import { Component, OnInit,ViewChild , AfterViewInit, OnDestroy } from '@angular/core';
import { adminLteConf } from '../admin-lte.conf';
import { Http, HttpModule } from '@angular/http';
import { HttpHeaders, HttpClient } from '@angular/common/http';
import { DataTableDirective } from 'angular-datatables';



class DataTablesResponse {
  //data: any[];
  draw: number;
  recordsFiltered: number;
  recordsTotal: number;
}

const ParseHeaders = {
  headers: new HttpHeaders({
  'Content-Type'  : 'application/x-www-form-urlencoded',
  })
};

@Component({
  selector: 'app-listpackage',
  templateUrl: './listpackage.component.html',
  styleUrls: ['./listpackage.component.css'],
  
})


export class ListpackageComponent implements OnInit {
  @ViewChild(DataTableDirective)
  datatableElement: DataTableDirective;
  public packages: any[];
  dtOptions: DataTables.Settings = {}; 
  
  constructor(private http: HttpClient) { }

  ngOnInit() {

    this.dtOptions = this.buildDtOptionpkg("Your URL here"); 
    
  } 

 
  buildDtOptionpkg (url: string): DataTables.Settings  {
   
    return{
      
      //pagingType: 'full_numbers',
      
      processing: true,
      serverSide: true,
      paging:true,
      language: {
        search: "_INPUT_",
        searchPlaceholder: "Search...",
        lengthMenu: "_MENU_",
        paginate: {
          first:'',
          last:'',
          next: '<i class="fa fa-chevron-circle-right">', // or '→'
          previous: '<i class="fa fa-chevron-circle-left">' // or '←' 
        }
      },
      dom: '<"top"f><t><"bottom"p><"inline-flex" li><"clear">',
      ajax:(dataTablesParameters:any, callback) => {
        
        this.http.post<DataTablesResponse>(
         
          url,
          dataTablesParameters,ParseHeaders
        ).subscribe(resp=> {
          var resposnse = JSON.stringify(resp);
          var respsn = JSON.parse(resposnse);
          //var resposnse = resp.json();
          this.packages = respsn.data;
          //console.log(this.packages);
          this.selectedIndex = this.packages.length;
          callback({
            recordsTotal: respsn.meta['total'],
            recordsFiltered: respsn.meta['total'],
            data: []
          });
        });
      }
    };
  } 
  
}

jayantsr83 avatar Sep 19 '18 08:09 jayantsr83

Thank you so much one thing did it rerendering data properly means if new data is retrieved does it reflect to datatable

On Wed, Sep 19, 2018, 1:10 PM Jayant [email protected] wrote:

Here is the full code of it hope it will help you

`import { Component, OnInit,ViewChild , AfterViewInit, OnDestroy } from '@angular/core'; import { adminLteConf } from '../admin-lte.conf'; import { Http, HttpModule } from '@angular/http'; import { HttpHeaders, HttpClient } from '@angular/common/http'; import { DataTableDirective } from 'angular-datatables';

class DataTablesResponse { //data: any[]; draw: number; recordsFiltered: number; recordsTotal: number; }

const ParseHeaders = { headers: new HttpHeaders({ 'Content-Type' : 'application/x-www-form-urlencoded', }) };

@component https://github.com/component({ selector: 'app-listpackage', templateUrl: './listpackage.component.html', styleUrls: ['./listpackage.component.css'],

})

export class ListpackageComponent implements OnInit { @ViewChild https://github.com/ViewChild(DataTableDirective) datatableElement: DataTableDirective; public packages: any[]; dtOptions: DataTables.Settings = {};

constructor(private http: HttpClient) { }

ngOnInit() {

this.dtOptions = this.buildDtOptionpkg("Your URL here");

}

buildDtOptionpkg (url: string): DataTables.Settings {

return{

//pagingType: 'full_numbers',

processing: true, serverSide: true, paging:true, language: { search: "INPUT", searchPlaceholder: "Search...", lengthMenu: "MENU", paginate: { first:'', last:'', next: '', // or '→' previous: '' // or '←' } }, dom: '<"top"f><"bottom"p><"inline-flex" li><"clear">', ajax:(dataTablesParameters:any, callback) => {

this.http.post<DataTablesResponse>(

  url,
  dataTablesParameters,ParseHeaders
).subscribe(resp=> {
  var resposnse = JSON.stringify(resp);
  var respsn = JSON.parse(resposnse);
  //var resposnse = resp.json();
  this.packages = respsn.data;
  //console.log(this.packages);
  this.selectedIndex = this.packages.length;
  callback({
    recordsTotal: respsn.meta['total'],
    recordsFiltered: respsn.meta['total'],
    data: []
  });
});

} };

}

} `

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/l-lin/angular-datatables/issues/1306#issuecomment-422702148, or mute the thread https://github.com/notifications/unsubscribe-auth/AllvaoTWj-LESoZT8Yc00nakBbZiVcRoks5ucfwQgaJpZM4WgwpS .

awais2080 avatar Sep 19 '18 08:09 awais2080

Yes you click on pagination or search anything or do sort by thing it will render the table and get the data according to condition from server

jayantsr83 avatar Sep 19 '18 08:09 jayantsr83

Thank you so much buddy 😍😉

On Wed, Sep 19, 2018, 1:19 PM Jayant [email protected] wrote:

Yes you click on pagination or search anything or do sort by thing it will render the table and get the data from table from server

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/l-lin/angular-datatables/issues/1306#issuecomment-422704712, or mute the thread https://github.com/notifications/unsubscribe-auth/Allvapp197elLcf-C7gyevxHwrCJB2-eks5ucf4KgaJpZM4WgwpS .

awais2080 avatar Sep 19 '18 08:09 awais2080

Thank you so much buddy … On Wed, Sep 19, 2018, 1:19 PM Jayant @.***> wrote: Yes you click on pagination or search anything or do sort by thing it will render the table and get the data from table from server — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#1306 (comment)>, or mute the thread https://github.com/notifications/unsubscribe-auth/Allvapp197elLcf-C7gyevxHwrCJB2-eks5ucf4KgaJpZM4WgwpS .

Hope it will help just try for me it works like a charm :smiley: :dancer:

jayantsr83 avatar Sep 19 '18 08:09 jayantsr83

Hi guys, what if it was a get request. The get request takes only 2 parameter

abbasShaaban avatar Sep 19 '18 08:09 abbasShaaban

Hi guys, what if it was a get request. The get request takes only 2 parameter

Hey i am not sure but i think you should do it with post else parameters of dataTables will not pass as much i experienced

jayantsr83 avatar Sep 19 '18 09:09 jayantsr83

[image: image.png] i still have rendering issue :(

On Wed, 19 Sep 2018 at 14:20, Jayant [email protected] wrote:

Hi guys, what if it was a get request. The get request takes only 2 parameter

Hey i am not sure but i think you should do it with post else parameters of dataTables will not pass as much i experienced

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/l-lin/angular-datatables/issues/1306#issuecomment-422723947, or mute the thread https://github.com/notifications/unsubscribe-auth/Allvai9pRWa1MMRBluJhg0XM4Q5XhUgbks5ucgx4gaJpZM4WgwpS .

awais2080 avatar Sep 20 '18 06:09 awais2080

I am not able to see image what the error you are getting exactly. Is it passing parameters or not?

jayantsr83 avatar Sep 20 '18 06:09 jayantsr83

Data is attained but its not rerendring in dt

On Thu, Sep 20, 2018, 11:55 AM Jayant [email protected] wrote:

I am not able to see image what the error you are getting exactly. Is it passing parameters or not?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/l-lin/angular-datatables/issues/1306#issuecomment-423062523, or mute the thread https://github.com/notifications/unsubscribe-auth/AllvarfK--xall1sgfH-VzjtiRuhGWwwks5uczv2gaJpZM4WgwpS .

awais2080 avatar Sep 20 '18 06:09 awais2080