Tuesday, October 20, 2020
therichposts@gmail.com
Angular 10Bootstrap 4.5Datatableselect2

Angular 10 Select2 Populate Datatable

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": [
  "src/styles.css",
  "node_modules/bootstrap/dist/css/bootstrap.min.css",
  "node_modules/select2/dist/css/select2.min.css", 
  "node_modules/datatables.net-dt/css/jquery.dataTables.css" 
],
"scripts": [
  "node_modules/jquery/dist/jquery.min.js", 
  "node_modules/bootstrap/dist/js/bootstrap.min.js",  
  "node_modules/datatables.net/js/jquery.dataTables.js",
  "node_modules/select2/dist/js/select2.min.js"
]

 

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

...
import {DataTablesModule} from 'angular-datatables';

import { NgSelect2Module } from 'ng-select2';

...
  imports: [
    ...
    DataTablesModule,
    NgSelect2Module
  ]
...

 

 

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

...
export class AppComponent {
  
   ...

  //Datatable settings
  dtOptions: any = {};

  //Datatable hide varaible
  showtable:boolean;

  //Select2 demo data variable
  public exampleData: any;

  ngOnInit()
  {

      //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"
          (valueChanged)="changed($event)"
          width=100%
          placeholder='Please select a Product...'
          [data]="exampleData"
          
        
          >		
      </ng-select2>
      
      <div class="tablecontainer" *ngIf="showtable">
        <table id="example" class="table table-striped table-bordered table-sm row-border hover mt-5" datatable [dtOptions]="dtOptions">
          <thead>
            <tr>
            
              <th>Energie</th>
              <th>Energie</th>
              <th>KH</th>
            </tr>
          </thead>
          <tbody>
          <tr class="table-success">
                <td>2.4 kcal</td>
                <td>10.1 kJ</td>
                <td>0.0g</td>
            </tr>
            <tr class="table-info">
              
              <td>2.4 kcal</td>
              <td>10.1 kJ</td>
              <td>0.0g</td>
            </tr>
            <tr class="table-danger">
            
              <td>2.4 kcal</td>
              <td>10.1 kJ</td>
              <td>0.0g</td>
            </tr>
          
            <tr class="table-success">
              
              <td>2.4 kcal</td>
              <td>10.1 kJ</td>
              <td>0.0g</td>
          </tr>
          
        
          </tbody>
        </table>
    </div>
    </div>
    
  </div>
  <!-- /.row -->

</div>
<!-- /.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.

Jassa

Thanks

Ajay Malhotra
the authorAjay Malhotra
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know Wordpress, Core php, Angularjs, Angular 2, Angular 6, Angular 7, Angular 8, Angular 9, Angular 10 Nodejs, laravel, Codeigniter, Shopify, Squarespace, Jquery, Google Map Api, Vuejs, Reactjs, Bigcommerce etc.

Leave a Reply

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