|
| 1 | +--- |
| 2 | +title: Getting Started |
| 3 | +page_title: Getting Started |
| 4 | +description: "Make your first steps with the Telerik UI for {{ site.framework }} TimePicker component by following a complete step-by-step tutorial." |
| 5 | +slug: timepicker_getting_started |
| 6 | +position: 1 |
| 7 | +--- |
| 8 | + |
| 9 | +# Getting Started with the TimePicker |
| 10 | + |
| 11 | +This tutorial explains how to set up a basic Telerik UI for {{ site.framework }} TimePicker and highlights the major steps in the configuration of the component. |
| 12 | + |
| 13 | +You will initialize a TimePicker control with a number of tools. Next, you will handle some of the TimePicker events. {% if site.core %}Finally, you can run the sample code in [Telerik REPL](https://netcorerepl.telerik.com/) and continue exploring the components.{% endif %} |
| 14 | + |
| 15 | +  |
| 16 | + |
| 17 | +@[template](/_contentTemplates/core/getting-started-prerequisites.md#repl-component-gs-prerequisites) |
| 18 | + |
| 19 | +## 1. Prepare the CSHTML File |
| 20 | + |
| 21 | +@[template](/_contentTemplates/core/getting-started-directives.md#gs-adding-directives) |
| 22 | + |
| 23 | +Optionally, you can structure the document by adding the desired HTML elements like headings, divs, paragraphs, and apply some basic styles. |
| 24 | + |
| 25 | +## 2. Initialize the TimePicker |
| 26 | + |
| 27 | +Use the TimePicker HtmlHelper {% if site.core %}or TagHelper{% endif %} to add the component to a page: |
| 28 | + |
| 29 | +* The `Name()` configuration method is mandatory as its value is used for the `id` and the `name` attributes of the TimePicker element. |
| 30 | + |
| 31 | +* The `DateInput()` configuration method specifies if the TimePicker will use the [DateInput]{% slug htmlhelpers_dateinput_aspnetcore %}) component for value editing. |
| 32 | + |
| 33 | +* The `Value()` configuration method specifies the initially selected time. |
| 34 | + |
| 35 | +```HtmlHelper |
| 36 | +
|
| 37 | +@using Kendo.Mvc.UI |
| 38 | +
|
| 39 | + @(Html.Kendo().TimePicker() |
| 40 | + .Name("timepicker") |
| 41 | + .Value("10:00 AM") |
| 42 | + .DateInput() |
| 43 | + ) |
| 44 | +``` |
| 45 | + |
| 46 | +{% if site.core %} |
| 47 | +```TagHelper |
| 48 | +@addTagHelper *, Kendo.Mvc |
| 49 | +
|
| 50 | + <kendo-timepicker name="timepicker" value="new DateTime(1900, 1, 1, 10, 00, 0)" date-input="true"> |
| 51 | + </kendo-timepicker> |
| 52 | +
|
| 53 | +``` |
| 54 | +{% endif %} |
| 55 | + |
| 56 | +## 3. Handle the TimePicker Events |
| 57 | + |
| 58 | +The TimePicker [exposes various events](/api/kendo.mvc.ui.fluent/timepickereventbuilder) that you can handle and further customize the functionality of the component. In this tutorial, you will use the `Open`, `Close`, and `Change` events of the TimePicker to log a message in the Browser's console. |
| 59 | + |
| 60 | +```HtmlHelper |
| 61 | +@using Kendo.Mvc.UI |
| 62 | +
|
| 63 | + @(Html.Kendo().TimePicker() |
| 64 | + .Name("timepicker") |
| 65 | + .Value("10:00 AM") |
| 66 | + .DateInput() |
| 67 | + .Events(e => |
| 68 | + { |
| 69 | + e.Change("change").Open("open").Close("close"); |
| 70 | + }) |
| 71 | + ) |
| 72 | +
|
| 73 | +<script> |
| 74 | + function open() { |
| 75 | + console.log("Open"); |
| 76 | + } |
| 77 | +
|
| 78 | + function close() { |
| 79 | + console.log("Close"); |
| 80 | + } |
| 81 | +
|
| 82 | + function change() { |
| 83 | + console.log("Change :: " + kendo.toString(this.value(), 't')); |
| 84 | + } |
| 85 | +</script> |
| 86 | +``` |
| 87 | +{% if site.core %} |
| 88 | +```TagHelper |
| 89 | +
|
| 90 | +@addTagHelper *, Kendo.Mvc |
| 91 | +
|
| 92 | + <kendo-timepicker name="timepicker" value="new DateTime(1900, 1, 1, 10, 00, 0)" date-input="true" on-change="change" on-open="open" on-close="close"> |
| 93 | + </kendo-timepicker> |
| 94 | +
|
| 95 | +<script> |
| 96 | + function open() { |
| 97 | + console.log("Open"); |
| 98 | + } |
| 99 | +
|
| 100 | + function close() { |
| 101 | + console.log("Close"); |
| 102 | + } |
| 103 | +
|
| 104 | + function change() { |
| 105 | + console.log("Change :: " + kendo.toString(this.value(), 't')); |
| 106 | + } |
| 107 | +</script> |
| 108 | +``` |
| 109 | +{% endif %} |
| 110 | + |
| 111 | +For more examples, refer to the [demo on using the events of the TimePicker](https://demos.telerik.com/{{ site.platform }}/timepicker/events). |
| 112 | + |
| 113 | +## 4. (Optional) Reference Existing TimePicker Instances |
| 114 | + |
| 115 | +To use the [client-side API of the TimePicker](https://docs.telerik.com/kendo-ui/api/javascript/ui/timepicker) and build on top of its initial configuration, you need a reference to the TimePicker instance. Once you get a valid reference, you can call the respective API methods: |
| 116 | + |
| 117 | +1. Use the `.Name()` (`id` attribute) of the component instance to get a reference. |
| 118 | + |
| 119 | + ```script |
| 120 | + <script> |
| 121 | + var timePickerReference = $("#timepicker").data("kendoTimePicker"); // timePickerReference is a reference to the existing instance of the helper. |
| 122 | + </script> |
| 123 | + ``` |
| 124 | +
|
| 125 | +1. Use the [client-side API of the TimePicker](https://docs.telerik.com/kendo-ui/api/javascript/ui/timepicker) to control the behavior of the widget. In this example, you will use the [`enable`](https://docs.telerik.com/kendo-ui/api/javascript/ui/timepicker/methods/enable) method to disable the TimePicker. |
| 126 | +
|
| 127 | + ```script |
| 128 | + <script> |
| 129 | + $(document).ready(function () { |
| 130 | + var timepicker = $("#timepicker").data("kendoTimePicker"); |
| 131 | +
|
| 132 | + timepicker.enable(false); |
| 133 | + }) |
| 134 | + </script> |
| 135 | + ``` |
| 136 | +
|
| 137 | +For more information on referencing specific helper instances, see the [Methods and Events]({% slug methodevents_core %}) article. |
| 138 | +
|
| 139 | +{% if site.core %} |
| 140 | +
|
| 141 | +## Explore this Tutorial in REPL |
| 142 | +
|
| 143 | +You can continue experimenting with the code sample above by running it in the Telerik REPL server playground: |
| 144 | +
|
| 145 | +* [Sample code with the TimePicker HtmlHelper](https://netcorerepl.telerik.com/QxOzlvvH41DZ6oLL00) |
| 146 | +
|
| 147 | +* [Sample code with the TimePicker TagHelper](https://netcorerepl.telerik.com/mHOzlbFR41l9KNn444) |
| 148 | +{% endif %} |
| 149 | +
|
| 150 | +## Next Steps |
| 151 | +
|
| 152 | +* [Configuring the Floating Label of the TimePicker]({% slug htmlhelpers_timepicker_floatinglabel_aspnetcore %}) |
| 153 | +* [Customizing the Appearance of the TimePicker]({% slug appearance_timepicker %}) |
| 154 | +* [Using Validation with the TimePicker]({% slug htmlhelpers_timepicker_aspnetcore_validation %}) |
| 155 | +
|
| 156 | +## See Also |
| 157 | +
|
| 158 | +* [Using the API of the TimePicker for {{ site.framework }} (Demo)](https://demos.telerik.com/{{ site.platform }}/timepicker/api) |
| 159 | +* [Client-Side API of the TimePicker](https://docs.telerik.com/kendo-ui/api/javascript/ui/timepicker) |
| 160 | +* [Server-Side API of the TimePicker](/api/timepicker) |
| 161 | +* [Knowledge Base Section](/knowledge-base) |
0 commit comments