In this Angular 9 tutorial, we’ll learn how to install ngx-datatable
package and build datatables in Angular application with Pagination, Sorting, Filters with examples.
Data tables are widely used in applications to show data sets with necessary features which are must to have for good user interactions like Pagination, Sorting by columns, Scrollings in horizontal and verticle directions, etc.
Application dealing with a large number of tabular data, it becomes very difficult to manage other details as well like the columns with custom templating and flexible styling.
In this post, we will discuss a very popular Datatable module ngx-datatable which is fully loaded with many required features and also very easy to use and manage. We’ll also, discuss some issues faced during sorting and tricks related to resetting or navigation of pagination from class component using the offset
 property.
Let’s get started…
# Update Angular CLI
Run the following command to install the latest version of Angular CLI tool
$ npm i @angular/cli</pre> <h3># Create a new Angular project</h3> Now by using the Angular CLI, we'll create a new Angular 9 project by running the <code>ng
command$ ng new angular-datatables</pre> Move to the project directory <pre class="wp-block-prismatic-blocks"><code class="language-javascript">$ cd angular-datatables</pre> <h3># Install Datatable Package</h3> To use Datatables in Angular project, we'll install the <code>ngx-datatable
package by running following npm command in the terminal window$ npm install @swimlane/ngx-datatable</pre> <h3># Update App Module</h3> To use <code>ngx-datatable
in the application, we need to import theNgxDatatableModule
in application module to provide its methods to implement Datatable in components. Now open the app.module.ts file, and make following changes// app.module.ts import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppComponent } from './app.component'; import { NgxDatatableModule } from '@swimlane/ngx-datatable'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, NgxDatatableModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
# Import Theme Styles
The
ngx-datatable
package comes with three awesome themes including Dark, Bootstrap and Material theme.There are two methods to import these themes in the Angular project:
Method 1) Import in the style.scss file
If you are using SCSS style of styling in Angular project then, you can simply import style files in the styles.scss file at the project root.
/* styles.scss */ /* You can add global styles to this file, and also import other style files */ @import '~@swimlane/ngx-datatable/index.css'; @import '~@swimlane/ngx-datatable/themes/material.scss'; @import '~@swimlane/ngx-datatable/themes/dark.scss'; @import '~@swimlane/ngx-datatable/themes/bootstrap.scss'; @import '~@swimlane/ngx-datatable/assets/icons.css';</pre> <h6>Method 2) Add in the angular.json file</h6> We can also add themes in the <strong>angular.json</strong> file's <code>styles
array property as shown below.... .... "styles": [ "src/styles.scss", "./node_modules/@swimlane/ngx-datatable/index.css", "./node_modules/@swimlane/ngx-datatable/themes/material.scss", "./node_modules/@swimlane/ngx-datatable/themes/dark.scss", "./node_modules/@swimlane/ngx-datatable/themes/bootstrap.scss", "./node_modules/@swimlane/ngx-datatable/assets/icons.css" ], .... ....
# How to use a theme in
ngx-datatable
?As you can see we have added all the themes at once, you can change the theme of a
ngx-datatable
by adding a specific theme class on the<ngx-datatable>
component:
class="material"
class="bootstrap"
class="dark"
Now we have configured our Angular project to use
NgxDatatables
in components.
# Adding NgxDatatable
To implement the NgxDatatable we add
ngx-datatable
component with required and optional properties in the template file.Open app.component.html file then add following HTML code with data table template with dummy data
<ngx-datatable class="material" [rows]="rows" [columnMode]="'force'" [headerHeight]="50" [footerHeight]="50" [rowHeight]="50" [scrollbarV]="true" [sorts]="[{prop: 'name', dir: 'desc'}]"> <ngx-datatable-column name="Company"> <ng-template let-row="row" ngx-datatable-cell-template> {{row.company}} </ng-template> </ngx-datatable-column> <ngx-datatable-column name="Name"> <ng-template let-row="row" ngx-datatable-cell-template> {{row.name}} </ng-template> </ngx-datatable-column> <ngx-datatable-column name="Gender"> </ngx-datatable-column> </ngx-datatable></pre> <h6></h6> <h6>Option Properties Used</h6> <div> <div><strong><code>[rows]
: Data object is passed in the [rows] property using which datatable is build.
Leave a Reply