Instantly share code, notes, and snippets.

@amandeepmittal amandeepmittal/ Secret
Created Aug 9, 2018

What would you like to do?

Smart Tables with Angular 6

In this tutorial we are going to learn how to integrate and use ng2-smart-tables with Angular 6. The ng2-smart-table is a library that is available to us as an npm package and pre-defined components and directives for sorting, searching, filtering and displaying data in the form of a table. To start, please run the following command from your terminal window to install the latest version of angular/cli.

If you already are using angular/cli version above 6, please upgrade to the latest version. If it does not install then try in the sudo mode for unix based machines. angular/cli not only scaffolds a directory structure for a project but also take care of pre-configuration that is needed to an application. With the below command we generate our new project:

To see of everything is working fine, let us run ng serve --open. You can visit the URL http://localhost:4200/ to see our newly created project in action.


Installing rxjs-compat

Angular's ecosystem struggles because of the compatibility issues of thrid party libraries whenever a new Angular version is released. Similar is happening in Angular 6. It depends on TypeScript 2.7 and RxJS version 6. Not many third party libraries have been updated and thus, to make them work with the latest Angular version, we are going to use another third party library in our app, called rxjs-compat.

Please note that, in future, third party libraries might be compatible and do not need rxjs-compat. So check documentation of the third party library you are using with Angular 6.

Install ng2-smart-table library

We can install it as a local dependency to our Angular project.

In any Angular application, app.module.ts is the global space for registering a module. Open it and add the following:

If you get no errors and the webpack has compiled our app successfully, this mean we can continue to work.

Creating a Table

We will generate a component called table using ng g c table. The ng command will not only create a default component but also updates app.module.ts for us. You can fine our newly generated component inside src/app/. To see if it is working, we will import this table component inside app.component.ts.

Also modify the app.component.html file to render the table component.



Building the Backend

To serve data as an API to our Angular app we currently are going to create a mock backend server. Install json-server globally from npm.

Now in our src/ directory, create directory called data and inside it create a new file called db.json. We will be adding some mock data inside this file in JSON format. It will be easier to render JSON data in angular app.

To serve this data to our angular application, in a spearate terminal window, write the following command:

You will be prompted with the following success message. Our data is accessbile through the URL http://localhost:4000/employees.


Fetching Data using HTTP Client

Most front end framework do not have a built in mechanism to fetch data from remote API or a server. However, Angular leverages in this case, its HTTP Client which allows us to fetch data from the remote API, in our case the JSON server data. To make use of it, first append the app.module.ts file.

Inside the table folder, create two new files. First, Table.ts: this file works as an schema design (if you think interms of database) or an interface that let our Angular application know the type of data it is going to receive from the remote API. This will be helpful in creating a service, which is our next step.

Second, is our service file table.service.ts. Our service file contain an injectable class called TableService which will fetch the data from the remote API using HTTP client.

Importing this service in app.module.ts as a provider which will enable our Table component to access it.

Next, we modify our table.component.ts and table.component.html.

If you get a similar result like below, depending on the amount of your data, this means you successfully created the Table Service and fetched the data from a remote API using HttpClient.


Making our Table Smart

At this point, our table is just rendering as static and does not have any functionality provided by the ng2-smart-table library such as sorting, searching, etc. To make it dynamic, we need to add some configuration, which is a require step when working with ng2-smart-table library.

Create an object inside table.component.ts and call it settings. This object contain columns that are displayed on the table.

The column name contain those fields that are coming from our service. The column name do not have to be identical with the orginial key. You can add or modify the name for a column using title. Another thing we need to add is called source which gives reference to of the actual data from our service to the ng2-smart-table directive. Yes, our list piece of puzzle to make our table dynamic is a directive provided by ng2-smart-library called the same. See it action app.component.html and replace our previous code.

The settings is the same object that we defined in the app.component.ts The source is comping from Table service where we fetch data and make it available to our component using this.employees. Following is how your table will look now.


Try to play with it to explore its functionalities. Notice, how it automatically adds the compatibilites to edit and update or delete a field as well add a search bar over every column.


It also add sorting functionality.


There are various advance configuration that ng2 table library provide to us. For example, to add a multi select checkbox, we can just edit the configuration or the settings object in our table component and this feature will be added to our existing table. Append table.component.ts

And see yourself.


I hope this tutorial will be benefical in you getting started with ng2-smart-tables. You can find the complete code for this tutorial at this Github repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment