Using Ignite-UI Slider With Angular V6

Introduction 

Slider is a very common control to use today to apply a filter on the data in web applications. Filtering product price range is one of the most common examples. However,  jQuery provides a slider by default but that is not very easy to implement. Infragistics has also launched a slider control for Angular which is very easy to implement, enhanced, and quite efficient.

Types of Slider

  1. Range Slider
  2. Continuous Slider
  3. Stepped Slider

In this tutorial, we will cover the following topics.

  1. Implement Slider in Angular.
  2. Filter the data based on a selected range.
  3. Filter the data based on the exact value of the slider.
  4. Calculate the discount amount based on the selected stepped slider value.

Note
For this demo, I am using Visual Studio Code and Angular 6.

Let's get started.

Range Slider

Let's make a new project in Angular. Open any folder in Visual Studio Code where you want to save the project and then, open VS Code terminal and paste the following command. Refer the below code to create a new blank project.

ng new IgniteUI-Slider

Note

In case you did not set up IgniteUI in Angular,  please visit here.

Now,  make a new component in your project and name it as “Product”. Refer to the below command to make this.

ng g c Product

Let's make another simple class of Product which will be used as our model class.

ng generate class Product

Let us now add some properties to our Product class, as in the code below. 

  1. export class Product {  
  2.     id: number;  
  3.     name: string;  
  4.     price: number  
  5.   
  6.     constructor(id: number, Name: string, Price: number) {  
  7.         this.id = id;  
  8.         this.name = Name;  
  9.         this.price = Price;  
  10.     }  
  11. }  

Now, let's add some data on the the Product page to bind in a simple grid. Go to the product.component.ts file and generate some raw data, as given below.

  1. import { Product } from '../product';  
  2.   
  3. export class ProductComponent implements OnInit {   
  4.   
  5.    public data = [  
  6.     new Product(1, 'Jeans1', 150),  
  7.     new Product(2, 'Jeans2', 250),  
  8.     new Product(3, 'Jeans3', 350),  
  9.     new Product(4, 'Jeans4', 450),  
  10.     new Product(5, 'Jeans5', 550),  
  11.     new Product(6, 'Jeans6', 650),  
  12.     new Product(7, 'Jeans7', 750),  
  13.     new Product(8, 'Jeans8', 850)  
  14.   ];  
  15.   
  16. }  

Add the HTML code to product.component.html file to show the data.

  1. <table width="40%">  
  2.        <tr>  
  3.            <th>  
  4.                Id.  
  5.            </th>  
  6.            <th>  
  7.                Name  
  8.            </th>  
  9.            <th>  
  10.                Price  
  11.            </th>  
  12.        </tr>  
  13.        <tr *ngFor="let p of data">  
  14.            <td>  
  15.                {{p.id}}  
  16.            </td>  
  17.            <td>  
  18.                {{p.name}}  
  19.            </td>  
  20.            <td>  
  21.                {{p.price}}  
  22.            </td>  
  23.        </tr>  
  24.    </table>  

Change the app.component.html to “<app-product></app-product>”. This is our product app selector which will render the product.html here.

Now, run this application first to check out if the data is properly bound or not. Refer to the below command to run the app.

ng serve --open

The final output will be like Figure 1.

Output
Figure 1.

Now, our data is set up. Let's add the IgniteUI Slider to our project. First, we need to add the IgniteUI CLI to the project.

Note
Follow these steps only if you’ve not set up Ignite UI for Angular before.

  1. Type the command “npm install igniteui-angular”.
  2. In case if hammerJS module does not exist, use command - “ng install hammerjs”.
  3. Change the Styles and Script array in Angular.json.
    1.  "styles": [  
    2.         "styles.css",  
    3.         "node_modules/igniteui-angular/styles/igniteui-angular.css"  
    4. ],  
    5. "scripts": ["node_modules/hammerjs/hammer.min.js"], 
  1. Import HammerJS in app.module.ts.
    1. import 'hammerjs';”  
  1. Ignite UI uses Angular Material icons. To support these icons, import Material Icon CSS in “Style.css”.
    1. @import url('https://fonts.googleapis.com/icon?family=Material+Icons');  

Now, we are ready to go for Slider Component. Let's import all necessary components to make the slider work.

  1. //Slider Require Animation  
  2. import { BrowserAnimationsModule } from "@angular/platform-browser/animations";  
  3.   
  4. //Main Slider Component  
  5. import { IgxSliderModule,  IgxInputGroupModule, } from "igniteui-angular";  
  6.   
  7. //Use ngModel in App  
  8. import { FormsModule,  ReactiveFormsModule} from '@angular/forms';  

Add all these components in import section of ngModule.

  1. imports : [  
  2.  BrowserModule,  
  3.  BrowserAnimationsModule,  
  4.  IgxSliderModule,  
  5.  IgxInputGroupModule,  
  6.  FormsModule,  
  7.  ReactiveFormsModule  

Now, go back to the product.component.ts file and import the Slider. 

  1. import { SliderType } from 'igniteui-angular';  

Declare two variables inthe class and assign your data to filter the data in initialization. We also need to create a filter function which will be called when slider changes. Also, we have to declare the minimum and maximum values for the slider which we’ll set in HTML. We get set min and max value dynamically according to our data.

  1. public sliderType = SliderType;  
  2. public Filterdata: any = {};  
  3.   
  4. ngOnInit() {  
  5. // data is same which we declare above  
  6. this.Filterdata = this.data;  
  7. }  
  8.   
  9. public MinRangeValue = Math.min.apply(Math, this.data.map(function (item) { return item.price; }));  
  10.   
  11. public MaxRangeValue = Math.max.apply(Math, this.data.map(function (item) { return item.price; }));  
  12.   
  13. SliderChange(val: any): void {  
  14.     this.Filterdata = this.data.filter(obj => obj.price >= val.lower && obj.price <= val.upper);  
  15. }  

Now, let us add the slider in product.component.html page.

  1.  <div class="base">  
  2.     <label style="font-weight: bold">Product Filter By Price</label>  
  3.     <div style="width: 40%">  
  4.         <igx-slider id="slider" [type]="sliderType.RANGE"                                                   (onValueChange)="SliderChange(priceRange)"  
  5. [minValue]="0"   
  6. [maxValue]="1000"  
  7.                                    [(ngModel)]="priceRange"                                                        [lowerBound]="MinRangeValue"  
  8. [upperBound]="MaxRangeValue"></igx-slider>  
  9.         <h4>  
  10.             Slider Lower Value is : {{priceRange.lower}}  
  11.             <br> Slider Upper Value is : {{priceRange.upper}}  
  12.         </h4>  
  13.         <div>  
  14.             <table width="40%">  
  15.                 <tr>  
  16.                     <th>  
  17.                         Id.  
  18.                     </th>  
  19.                     <th>  
  20.                         Name  
  21.                     </th>  
  22.                     <th>  
  23.                         Price  
  24.                     </th>  
  25.                 </tr>  
  26.                 <tr *ngFor="let p of Filterdata">  
  27.                     <td>  
  28.                         {{p.id}}  
  29.                     </td>  
  30.                     <td>  
  31.                         {{p.name}}  
  32.                     </td>  
  33.                     <td>  
  34.                         {{p.price}}  
  35.                     </td>  
  36.                 </tr>  
  37.             </table>  
  38.         </div>  
  39.     </div>  
  40. </div>  

Now, if you hit ng serve --open command in terminal, you will see the output like in the Figure 2. You can change the slider value to filter the data and when you leave the slider bubble, the data gets filtered. We can also show the current slider's lower and higher values on the page to just check if our data id is filtered correctly or not.

Output
Figure 2.

 

Continuous Slider

Let's add a continuous slider on the same page to filter the existing data. We will add another slider just after the Range Slider. Open the product.component.html and add the following code.

  1. <igx-slider id="slider" [minValue]="0" [maxValue]="1000" (onValueChange)= "ExactSliderChange(ExactPrice)" [isContinuous]=true [(ngModel)]="ExactPrice"> </igx-slider>  
  2. <h4>  
  3.   Exact Price is : {{ExactPrice}}  
  4. </h4>  

Add the following code to the product.component.ts to make the continuous slider work. 

  1. // Declare another variable   
  2. public ExactPrice: number;  
  3.   
  4. // add this to ngOnInit method   
  5. this.ExactPrice = this.MinRangeValue;  
  6.   
  7. // This function filter the data on slider value  
  8. ExactSliderChange(val: number): void {  
  9.     this.Filterdata = this.data.filter(obj => obj.price == val);  
  10. }  

Now, if you hit ng serve --open command in terminal, you will see the output like in the Figure 3. Do note that the continuous slider does not show the bubble on slider.

Output
Figure 3.

 

Stepped Slider

This is also called “Discrete Slider”. By default, Angular Ignite-UI is discrete and it provides a fixed value to slide in a slider. We can set how many steps we require in the slider by just setting one property. Let’s make a simple discount process where a user can enter the product value and select the slider according to the discount that he/she wants to give on a particular product and the price will be automatically calculated.

To add this, go to the product.component.ts file and add the following code. This is simple code where we set our slider value to 0-100 and set the Step property to 5; this means the slider will have 20 intervals with the value of 5.

  1. <label style="font-weight: bold">Calculate Discount</label>  
  2.     <br>  
  3.     <br>  
  4.     <label>Total Price</label>  
  5.     <input [(ngModel)]="discount.totalPrice" style="margin: 10px">  
  6.     <igx-slider [minValue]="0" [maxValue]="100" [step]="5" (onValueChange)= "DiscountSiderChange(discount)" [(ngModel)]="discount.discountInPercent"> </igx-slider>  
  7.     <h4>  
  8.       Total Discount Offered : Rs. {{totalAmountAfterDiscount}}  
  9.       <br><br>  
  10.       Total Price After Discount : Rs. {{discount.totalPrice - totalAmountAfterDiscount}}  
  11.     </h4>  

Let’s go to the product.component.ts and add the following code where we’re are declaring 2 variables which hold the discount and amount after discount. The slider change function will calculate the discount and bind it to the HTML page using two-way binding.

  1. public totalAmountAfterDiscount: any;  
  2. public discount: any = {};  
  3.      
  4. DiscountSiderChange(val: any): void {  
  5.     this.totalAmountAfterDiscount = (val.totalPrice * val.discountInPercent) / 100;  
  6.   }  

Now, if you hit ng serve --open command in terminal, you will see the output like in Figure 4.

Output
Figure 4.

 

Summary

Infragistics is working on controls since 1989. They provide controls for almost every language including Angular, JavaScript, ASP.NET, Windows Forms, Xamarin etc. These controls are very well managed and optimized by Infragistic Team. By using Angular two-way binding and IgniteUI Slider, we can easily filter the data without postback.

Here, we are using ValueChange function provided by Slider control which calls the function when we leave the slider but we can also use ngModelChange function which filters the data while the user is swiping the slider simultaneously.


Similar Articles