Angular UI Components Documentation

Angular Slider Component

The Slider component for Angular represents a flexible Slider that lets the user select from a range of values by moving a thumb along a track. The widget is completely customizable in terms of appearance and offers numerous configuration options like mouse wheel and keyboard support, smooth or step-based slider and support for range sliders.

1. Installation

The easiest way to get started with jQWidgets UI for Angular is to use the Angular CLI Tool. To scaffold your project structure, follow its installation instructions.

npm install -g @angular/cli
ng new jqwidgets-project
cd jqwidgets-project

Install jQWidgets

jQWidgets Angular UI comes packaged with Angular CLI schematics to make creating Angular applications easier. Schematics are included with both @angular/cdk and jqwidgets-ng. Once you install the npm packages, they will be available through the Angular CLI.

Angular CLI supports the addition of packages through the ng add command. The ng add command provides faster and more user-friendly package installation. To install the jQWidgets UI for Angular package, use ng add and add the name of the NPM package:

ng add jqwidgets-ng

Alternatively, you can use the standard installation (Manual Setup)

jQWidgets UI for Angular is distributed as jqwidgets-ng NPM package

  1. Download and install the package.
    npm install jqwidgets-ng
  2. Adding CSS reference

    The following CSS file is available in ../node_modules/jqwidgets-ng/ package folder. This can be referenced in [src/styles.css] using following code.

    @import 'jqwidgets-ng/jqwidgets/styles/jqx.base.css';

    Another way to achieve the same is to edit the angular.json file and in the styles add the style.

    "styles": [
    	"node_modules/jqwidgets-ng/jqwidgets/styles/jqx.base.css"
    ]
    

2. Add the HTML for jQWidgets component in src/app/app.component.html

app.component.html


<div style="width: 550px">
  <div style="height: 180px;">
    <div style="float: left">
      <span style="font-style: italic;">Red</span>
      <jqxSlider [theme]="'fluent'" #redSlider (onChange)='redSliderOnChange()'
                 [height]='60' [min]='0' [max]='255' [tickLabelFormatFunction]='tickLabelFormatFunction'
                 [value]='0' [step]='25.5' [mode]='"fixed"' [tooltipFormatFunction]='tooltipFormatFunction'
                 [showTickLabels]='true' [tooltip]='true' [ticksFrequency]='25.5'>
      </jqxSlider>
      <span style="font-style: italic;">Green</span>
      <jqxSlider [theme]="'fluent'" #greenSlider (onChange)='greenSliderOnChange()'
                 [height]='60' [min]='0' [max]='255' [tickLabelFormatFunction]='tickLabelFormatFunction'
                 [value]='0' [step]='25.5' [mode]='"fixed"' [tooltipFormatFunction]='tooltipFormatFunction'
                 [showTickLabels]='true' [tooltip]='true' [ticksFrequency]='25.5'>
      </jqxSlider>
      <span style="font-style: italic;">Blue</span>
      <jqxSlider [theme]="'fluent'" #blueSlider (onChange)='blueSliderOnChange()'
                 [height]='60' [min]='0' [max]='255' [tickLabelFormatFunction]='tickLabelFormatFunction'
                 [value]='255' [step]='25.5' [mode]='"fixed"' [tooltipFormatFunction]='tooltipFormatFunction'
                 [showTickLabels]='true' [tooltip]='true' [ticksFrequency]='25.5'>
      </jqxSlider>
    </div>
    <div class="jqx-rc-all colorBlock" id="colorBlock">
      <div id="colorLabel"></div>
    </div>
  </div>
  <jqxCheckBox [theme]="'fluent'" (onChange)='checkBoxOnChange($event)'
               [width]='200' [checked]='false'>
    Smooth Thumb Drag
  </jqxCheckBox>
</div>	

3. Setup Component Logic

app.component.ts


import { Component, ViewChild, ViewEncapsulation } from '@angular/core';


import { jqxSliderModule, jqxSliderComponent } from 'jqwidgets-ng/jqxslider';
@Component({
    selector: 'app-root',
    imports: [jqxSliderModule],
    standalone: true,
    templateUrl: './app.component.html',
    styleUrls: ['app.component.css'],
    encapsulation: ViewEncapsulation.None
})

export class AppComponent {
    @ViewChild('redSlider') redSlider: jqxSliderComponent;
    @ViewChild('greenSlider') greenSlider: jqxSliderComponent;
    @ViewChild('blueSlider') blueSlider: jqxSliderComponent;

    ngAfterViewInit(): void {
        this.setColor();
    }

    redSliderOnChange(): void {
        this.setColor();
    };

    greenSliderOnChange(): void {
        this.setColor();
    };

    blueSliderOnChange(): void {
        this.setColor();
    };

    checkBoxOnChange(event: any): void {
        let checked = event.args.checked;
        let value = 'default';
        if (!checked) {
            value = 'fixed';
        }

        this.redSlider.mode(value);
        this.greenSlider.mode(value);
        this.blueSlider.mode(value);
    };

    setColor(): void {
        let red = this.fixHex(Math.round(this.redSlider.value()).toString(16)),
            green = this.fixHex(Math.round(this.greenSlider.value()).toString(16)),
            blue = this.fixHex(Math.round(this.blueSlider.value()).toString(16));
        document.getElementById('colorBlock').style.backgroundColor = '#' + red + green + blue;
        document.getElementById('colorLabel').innerHTML = ('#' + red + green + blue).toUpperCase();
        let color = this.getTextElement({ r: parseInt(red, 16), g: parseInt(green, 16), b: parseInt(blue, 16) });
        document.getElementById('colorLabel').style.color = color;
    };

    fixHex(hex: any): any {
        return (hex.length < 2) ? '0' + hex : hex;
    };

    getTextElement(color: any): any {
        let nThreshold = 105;
        let bgDelta = (color.r * 0.299) + (color.g * 0.587) + (color.b * 0.114);
        let foreColor = (255 - bgDelta < nThreshold) ? 'Black' : 'White';
        return foreColor;
    };

    tickLabelFormatFunction: any = (value) => {
        if (value == 0) return value;
        if (value == 255) return value;
        return "";
    };

    tooltipFormatFunction: any = (value) => {
        if (this.redSlider.mode() === 'default') {
            if (value < 10) return new Number(value.toPrecision(3));
            if (value > 10 && value < 100) return new Number(value.toPrecision(4));
            else return new Number(value.toPrecision(5));
        }
        else {
            if (value < 10) return new Number(value.toPrecision(3));
            if (value > 10 && value < 256) return new Number(value.toPrecision(4));
        }
    };
}

Summary

jQWidgets UI for Angular provides an easy way to integrate robust UI components into your Angular project. By using either the ng add command or manual setup, you can quickly get started. Once the setup is complete, you can add the desired jQWidgets components and configure them in your Angular components to match your application requirements.

Slider API

API Reference of the jQWidgets Slider component for Angular: Slider API