Skip to content
Loading
  
  •         
  •               
  •               
  •           
  •        
  •         
  •   
  •   
  •   
  •   
  •         
  •                   
  •   
  •  
    1. import { AfterViewInit, Component, OnDestroy, OnInit } from '@angular/core';  
    2. import { SharedService } from '../shared.service';  
    3. import { DataTablesModule } from 'angular-datatables';  
    4. import {Subject} from 'rxjs';  
    5.   
    6.   
    7. @Component({  
    8.   selector: 'app-users',  
    9.   templateUrl: './users.component.html',  
    10.   styleUrls: ['./users.component.sass']  
    11. })  
    12. export class UsersComponent implements OnInit,OnDestroy {  
    13.   
    14.   constructor(private service:SharedService) { }  
    15.   
    16.   UsersList:any=[];  
    17.    
    18.    
    19.   dtOptions: DataTables.Settings = {};  
    20.   dtTrigger: Subject = new Subject();  
    21.   
    22.   ngOnDestroy(): void {  
    23.     this.dtTrigger.unsubscribe();  
    24.   }  
    25.   
    26.   ngOnInit(): void {  
    27.     this.getUsers();  
    28.      
    29.     this.dtOptions= {   
    30.       pagingType: 'full_numbers', pageLength:5, lengthMenu:[5,15,25], processing: true,   
    31.        data: this.UsersList,  // notice here  
    32.             columns: [    
    33.               {title: 'CustomId', data: 'id'},    
    34.               {title: 'Customer Code', data: 'customerCode'},    
    35.               {title: 'email', data: 'email'},    
    36.               {title: 'firstName', data: 'firstName'},    
    37.               {title: 'status', data: 'status'},    
    38.               {title: 'lastName', data: 'lastName'},    
    39.   
    40.               {title: 'createDate', data: 'createDate'},    
    41.   
    42.               {title: 'updateDate', data: 'updateDate'},    
    43.               
    44.             ],    
    45.             rowCallback: (row: Node, data: any[] | object, index: number) => {    
    46.               const self = this;    
    47.               
    48.               $('td', row).unbind('click');    
    49.               $('td', row).bind('click', () =>   
    50.               {    
    51.                   
    52.               });    
    53.               return row;    
    54.             }    
    55.           };    
    56.     
    57.   
    58.       
    59.   }  
    60.   
    61.   
    62.   
    63.   getUsers()  
    64.   {  
    65.     this.service.getUser().subscribe(data=>{  
    66.       this.UsersList=data;  
    67.      
    68.        
    69.     });  
    70.   }  
    71.   
    72.     
    73. }  
     
    0
  • Sachin Singh
    0
    remove everything from tbody, no need of for loop, datatable will be auto filled as we already set its data property in component.
    1. //replace this  
    2.                
    3.                 
    4.                 
    5.             "let dataItem of UsersList">    
    6.                 {{dataItem.id}}    
    7.                 {{dataItem.customerCode}}    
    8.                 {{dataItem.email}}    
    9.     
    10.                 {{dataItem.firstName}}    
    11.                 {{dataItem.status}}    
    12.                 {{dataItem.lastName}}    
    13.     
    14.                 {{dataItem.createDate}}    
    15.                 {{dataItem.updateDate}}    
    16.                 
    17.             
    18.             
    19. //to this  
    20.                
    21.                 
    22.                 
    23.            
    24.             
     
    0
  • Rijwan Ansari
    0
    Try this way. this is quite simple and step by step.
    https://blog.logrocket.com/angular-datatables-feature-rich-tables/ 
    0
  • Satya Karki
    0
    Check this 
    https://www.itsolutionstuff.com/post/how-to-use-jquery-datatable-in-angularexample.html 
    0