Angular TimePicker demonstration of different settings.
Loading...

Angular TimePicker: Settings

TimePicker demonstration of different settings.

A live Angular example of jqxTimePicker, the jQWidgets TimePicker component, with its complete source code below. It is one of 4 jqxTimePicker demos for Angular.

jqxTimePicker API used in this demo

Source code

To run it in your own app, install the package with npm install jqwidgets-ng, add these files to a standalone Angular CLI app, and add jqx.base.css and a theme to the styles in angular.json.

app.ts

import { Component, AfterViewInit, ViewEncapsulation, viewChild } from '@angular/core';
import { jqxTimePickerComponent } from 'jqwidgets-ng/jqxtimepicker';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { jqxButtonModule } from 'jqwidgets-ng/jqxbuttons';
import { jqxTimePickerModule } from 'jqwidgets-ng/jqxtimepicker';
import { jqxFormModule } from 'jqwidgets-ng/jqxform';

@Component({
    imports: [CommonModule, FormsModule, jqxButtonModule, jqxTimePickerModule, jqxFormModule],
    selector: 'app-root',
    styleUrl: './app.css',
    templateUrl: './app.html',
    encapsulation: ViewEncapsulation.None
})

export class App implements AfterViewInit {
    readonly timepicker = viewChild.required<jqxTimePickerComponent>("timepicker");

    size: number | string = 400;
    format: string = "24-hour";
    template: any[] = [
        {
            // a label's text is plain text; markup goes through render
            type: "label",
            render: () => "<span class='settings'>Settings</span>",
            rowHeight: "40px",
        },
        {
            type: "label",
            label: "Selection",
            rowHeight: "40px",
        },
        {
            bind: "radiobuttonSelection",
            type: "option",
            label: "Selection",
            labelPosition: "right",
            columnWidth: "100px",
            align: "left",
            optionslayout: "horizontal",
            options: [
                { label: "Hours", value: "hour" },
                { label: "Minutes", value: "minute" }
            ]
        },
        {
            type: "label",
            label: "Minute Interval",
            rowHeight: "40px",
        },
        {
            bind: "radiobuttonMinuteinterval",
            type: "option",
            label: "Minute Interval",
            labelPosition: "right",
            columnWidth: "100px",
            align: "left",
            optionslayout: "horizontal",
            options: [
                { label: "1", value: "value1" },
                { label: "5", value: "value2" },
                { label: "15", value: "value3" }
            ]
        },
        {
            name: "sethours",
            type: "button",
            text: "Set hours to 20",
            height: "30px",
            rowHeight: "40px",
            columnWidth: "50%"
        },
        {
            name: "setminutes",
            type: "button",
            text: "Set minutes to 12",
            height: "30px",
            rowHeight: "40px",
            columnWidth: "50%"
        },
        {
            name: "now",
            type: "button",
            text: "Set to now",
            height: "30px",
            rowHeight: "40px",
            columnWidth: "50%"
        },
        {
            type: "label",
            render: () => "Current value: <span id=\"currenttime\"></span>",
            rowHeight: "40px",
        }
    ];

    sampleValue: any = {
        "radiobuttonSelection": "hour",
        "radiobuttonMinuteinterval": "value1"
    };

    ngAfterViewInit() {
        this.showTime(this.timepicker().value());
    }

    // the time picker raises change while it is created, before the form has drawn its label
    showTime(value: Date): void {
        const label = document.getElementById("currenttime");
        if (label) label.innerHTML = this.timeFormatting(value);
    }

    timeFormatting = (value: Date): string => {
        let hour = value.getHours();
        let minutes = value.getMinutes();
        let minutesString = minutes < 10 ? "0" + minutes : minutes;

        return "<span>" + hour + ":" + minutesString + "</span>";
    };

    change(event: any): void {
        let args = event.args;
        this.showTime(args.value);
    }

    formDataChange(event: any): void {
        let args = event.args;
        let newValue = args.value;
        let previousValue = args.previousValue;
        for (let i in newValue) {
            let newInputValue = newValue[i];
            if (previousValue && previousValue[i] !== undefined && previousValue[i] !== newValue[i]) {
                switch (i) {
                    case "radiobuttonSelection":
                        this.timepicker().selection(newInputValue);
                        break;
                    case "radiobuttonMinuteinterval":
                        let currentValue = newInputValue;
                        if (currentValue === "value1") {
                            this.timepicker().minuteInterval(1);
                        } else if (currentValue === "value2") {
                            this.timepicker().minuteInterval(5);
                        } else {
                            this.timepicker().minuteInterval(15);
                        }
                        break;
                    default:
                        break;
                }
            }
        }
    }

    buttonClick(event: any): void {
        let args = event.args;
        let name = args.name;
        switch (name) {
            case "sethours":
                this.timepicker().setHours(20);
                break
                case "setminutes":
                this.timepicker().setMinutes(12);
                break;
                case "now":
                this.timepicker().value(new Date());
                break;
            default:
                break;
        }
    }
}

app.html

<div id="timpicker">
    <jqxTimePicker #timepicker (onChange)="change($event)" [width]="size" [height]="size" [format]="format"></jqxTimePicker>
</div>

<div id="settingsForm">
    <jqxForm #myForm
        (onFormDataChange)="formDataChange($event)"
        (onButtonClick)="buttonClick($event)"
        [width]="280"
        [height]="'auto'"
        [template]="template"
        [value]="sampleValue"
    >
    </jqxForm>
</div>

Learn more

More Angular TimePicker demos