I am trying to add Datatables plugin (datatables.net) facility with my angualar 6 project. I am not sure how should I include the necessary css, js and other required files to my project with npm installer. After selecting my necessary options I am following the NPM Install method with these :
npm install --save datatables.net-bs4
npm install --save datatables.net-buttons-bs4
npm install --save datatables.net-colreorder-bs4
npm install --save datatables.net-responsive-bs4
npm install --save datatables.net-rowgroup-bs4
npm install --save datatables.net-scroller-bs4
After Installing, I am not sure how will I use these in my project. my project is using ngx-bootstrap (https://www.npmjs.com/package/ngx-bootstrap) for styling.
style.scss // where I am only importing my css theme from node_modules
In ngx-bootstrap the styles are component wise, and I am using those easily. So, how can I use datatables features as a component ? In another way, where should I include the css, and required js files to achieve the datatables facilities on a page?
If anyone has done it please let me know, or any suggestions will be appreciated.
Thanks.
use angular data tables in Angular 6 angular-datatables
You need to install its dependencies before getting the latest release using NPM:
npm install jquery --save
npm install datatables.net --save
npm install datatables.net-dt --save
npm install [email protected] --save
npm install @types/jquery --save-dev
npm install @types/datatables.net --save-dev
angular.json
Import the DataTablesModule in app.module.ts
import { DataTablesModule } from 'angular-datatables';
imports: [
DataTablesModule
],
my datatableslibrary.ts
import { Component, OnDestroy, OnInit } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Subject } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { DataService } from '../data.service';
@Component({
selector: 'app-datatableslibrary',
templateUrl: './datatableslibrary.component.html',
styleUrls: ['./datatableslibrary.component.css']
})
export class DatatableslibraryComponent implements OnInit, OnDestroy {
users$: any[] = [];
dtOptions: DataTables.Settings = {};
dtTrigger: Subject<any> = new Subject();
constructor(private http: HttpClient, private data: DataService) {
}
ngOnInit() {
this.dtOptions = {
pagingType: 'full_numbers',
pageLength: 5,
processing: true
};
this.data.getUsers().subscribe(data => {
this.users$ = data;
this.dtTrigger.next();
});
}
ngOnDestroy(): void {
this.dtTrigger.unsubscribe();
}
}
my datatableslibrary.component.html
<table class="table table-striped table-bordered table-sm row-border hover" datatable [dtOptions]="dtOptions"
[dtTrigger]="dtTrigger">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Website</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let user of users$">
<td>{{ user.name }}</td>
<td>{{ user.email }}</td>
<td>{{ user.website }}</td>
</tr>
</tbody>
</table>
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { ToastrManager } from 'ng6-toastr-notifications';
import { environment } from 'src/environments/environment';
import { HttpClient, HttpParams } from '@angular/common/http';
import { LoginService } from '../login.service';
declare var $;
class SiteModal {
OrderNumber: string;
ContactName: string;
EntityName: string;
ContactNo: string;
CategoryName: string;
StatusName: string;
}
class DataTablesResponse {
data: any[];
draw: number;
recordsFiltered: number;
recordsTotal: number;
}
@Component({
selector: 'app-site',
templateUrl: './site.component.html',
styleUrls: ['./site.component.scss']
})
export class SiteComponent implements OnInit {
@ViewChild('dataTable') table: ElementRef;
dataTable: any;
dtOptions: DataTables.Settings = {};
siteModal: SiteModal[];
dataTablesResponse: DataTablesResponse[];
datatable: any;
public data: Object;
public temp_var: Object = false;
constructor(
private http: HttpClient,
public toastr: ToastrManager,
private LoginService: LoginService
) { }
ngOnInit(): void {
const that = this;
this.dtOptions = {
pagingType: 'full_numbers',
pageLength: 10,
serverSide: true,
processing: true,
ordering: false,
searching: false,
ajax: (dataTablesParameters: any, callback) => {
let params = new HttpParams();
let startNumber: any;
startNumber = dataTablesParameters.start;
if (startNumber != 0) {
startNumber = startNumber + 1
} else {
startNumber = startNumber;
}
params = params.append("start", startNumber);
params = params.append("length", dataTablesParameters.length);
let param = params.toString();
setTimeout(() => {
$(".dataTables_empty").hide();
}, 500);
that.http
.post<DataTablesResponse>(
environment.apiUrl + "api/Entity/GetSiteList",
params, {}
).subscribe(resp => {
that.siteModal = resp.data;
debugger
setTimeout(() => {
$(".dataTables_empty").hide();
}, 500);
callback({
recordsTotal: resp.recordsTotal,
recordsFiltered: resp.recordsFiltered,
data: []
});
});
},
columns: [{ data: 'OrderNumber' }, { data: 'ContactName' }, { data: 'EntityName' }, { data: 'ContactNo' }, { data: 'StatusName' }, { data: 'CategoryName' }]
};
}
}
<section class="content-header ng-scope">
<h1>Site </h1>
</section>
<section class="content">
<div class="panel panel-default">
<div class="panel-body">
<div class="row">
<div class="col-md-12 table-responsive">
<table id='Clienttbl' datatable [dtOptions]="dtOptions" class="row-border hover">
<thead>
<tr>
<th>Order #</th>
<th>ContactName</th>
<th>Entity Name</th>
<th>ContactNo</th>
<th>StatusName</th>
<th>Category Name</th>
</tr>
</thead>
<tbody *ngIf="siteModal?.length != 0">
<tr *ngFor="let data of siteModal">
<td>{{ data.OrderNumber }}</td>
<td>{{ data.ContactName }}</td>
<td>{{ data.EntityName }}</td>
<td>{{ data.ContactNo }}</td>
<td>{{ data.StatusName }}</td>
<td>{{ data.CategoryName }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</section>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With