Home Angular 10 Angular 10 Select2 Populate Datatable

Angular 10 Select2 Populate Datatable

by therichpost
0 comment
Angular 10 Select2 Populate Datatable

Hello friends, welcome back to my blog. Today in this blog post, I am going to show you, Angular 10 Select2 Populate Datatable.

Angular 10 Select2 Datatable

Angular 10 came and if you are new then you must check below two links:

  1. Angular10 for beginners
  2. Angular10 Basic Tutorials

Friends now I proceed onwards and here is the working code snippet for Angular 10 Select2 Populate Datatable and please use carefully this to avoid the mistakes:

1. Firstly friends we need fresh angular 10 setup and for this we need to run below commands but if you already have angular 10 setup then you can avoid below commands. Secondly we should also have latest node version installed on our system:

npm install -g @angular/cli 

ng new angularselect //Create new Angular Project

cd angularselect // Go inside the Angular Project Folder

ng serve --open // Run and Open the Angular Project

http://localhost:4200/ // Working Angular Project Url


2. Now friends, here we need to run below commands into our project terminal to install bootstrap(good responsive looks), jquery, ng-select2 and datatable modules into our angular application:

npm i ng-select2

npm install angular-datatables --save

npm install @types/datatables.net --save-dev

npm install @types/jquery --save-dev  

npm install datatables.net-dt --save

npm install datatables.net --save 

npm install select2

npm install jquery --save  

npm install bootstrap --save 


3. Now friends, here we need to add below  into our angular.json file:

"styles": [
"scripts": [


4. Now add below code into your src/app/app.module.ts file:

import {DataTablesModule} from 'angular-datatables';

import { NgSelect2Module } from 'ng-select2';

  imports: [



5. Now add below code into your src/app/app.component.ts file:

export class AppComponent {

  //Datatable settings
  dtOptions: any = {};

  //Datatable hide varaible

  //Select2 demo data variable
  public exampleData: any;


      //Select2 demo data
      this.exampleData = [
          id: '',
          text: ''
          id: 'dil',
          text: 'Dil'
          id: 'dallet',
          text: 'Dattel'

       //Hide datatable first time page load
       this.showtable = false;
       //datatable settings
       this.dtOptions = {
        pagingType: 'full_numbers',
        pageLength: 5,
        lengthMenu : [5, 10, 25],
        processing: true
  //Show datatable on seelct2 on change
  public changed(e: any): void {
    this.showtable = true;


6. Now friends we just need to add below code into src/app/app.component.html file to get final out on the web browser:

<!-- Page Content -->
<div class="container mt-5" style="min-height: 520px;">

  <!-- Heading Row -->
  <div class="row align-items-center my-5 mt-5">
    <div class="col-lg-12">
      <h1 class="font-weight-light mb-5">TheRichPost.com</h1>

      <ng-select2 id="myselect"
          placeholder='Please select a Product...'
      <div class="tablecontainer" *ngIf="showtable">
        <table id="example" class="table table-striped table-bordered table-sm row-border hover mt-5" datatable [dtOptions]="dtOptions">
          <tr class="table-success">
                <td>2.4 kcal</td>
                <td>10.1 kJ</td>
            <tr class="table-info">
              <td>2.4 kcal</td>
              <td>10.1 kJ</td>
            <tr class="table-danger">
              <td>2.4 kcal</td>
              <td>10.1 kJ</td>
            <tr class="table-success">
              <td>2.4 kcal</td>
              <td>10.1 kJ</td>
  <!-- /.row -->

<!-- /.container -->


Now we are done friends. If you have any kind of query, suggestion and new requirement then feel free to comment below.

Note: Friends, In this post, I just tell the basic setup and things, you can change the code according to your requirements. For better live working experience, please check the video on the top.

I will appreciate that if you will tell your views for this post. Nothing matters if your views will be good or bad because with your views, I will make my next posts more good and helpful.



You may also like

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.