Example of Time Duration in ASP.NET MVC TimePicker Control
The following sample demonstrates the popup list element in specific time duration. Click/Touch the TimePicker popup icon to select the desired value.
@Html.UiTimePicker("timepicker", new UiTimePickerOptions { OnItemRender = "itemRanderHandler", Value = ((DateTime)ViewData["value"]).TimeOfDay })
<style>
.control_wrapper { display: block; margin: 0 auto; max-width: 300px; padding: 5% 0; }
.tabs-wrap { padding: 0 0 10px; }
.ui-time-item span { color: #b7b1b1; }
.ui-time-item.ui-time-disabled span { height: 25px; width: 25px; font-size: 15px; }
</style>
<div class="control-section">
<div class="control_wrapper">
<div class="tabs-wrap">
<div class="wrap">
@Html.UiTimePicker("timepicker", new UiTimePickerOptions { OnItemRender = "itemRanderHandler", Value = ((DateTime)ViewData["value"]).TimeOfDay })
</div>
</div>
</div>
</div>
<script>
//initial time variable declaration
var startTime;
// The ItemRender event: args.element is the list item, args.value its time.
function itemRanderHandler(args) {
// inner element declaration for text
var span = document.createElement('span');
if (args.value.getHours() === 0 && args.value.getMinutes() === 0 && args.value.getSeconds() === 0) {
//assign the initial value to the variable
startTime = args.value;
}
//get the minutes details
var minutes = (+args.value - +startTime) / 60000;
//get the hours details
var hours = parseInt('' + (minutes / 60), 10);
var mins = (minutes % 60) / 6;
//displayed text formation for each LI element.
var minText;
if (minutes === 0 || minutes === 30) {
minText = minutes + ' mins';
} else {
minText = (mins > 0) ? ('.' + mins) : '';
}
span.innerHTML = ' (' + ((hours > 0) ? (hours + minText + ' hrs') : ('' + minText)) + ')';
//disable the specific time from the selection
if ((minutes / 60) % 3 === 0) {
//disable the time values by adding the e-disabled class.
args.element.classList.add('e-disabled');
}
//append the custom SPAN element into LI element
args.element.appendChild(span);
}
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class TimePickerController : Controller
{
// The page is Views/TimePicker/TimeDuration.cshtml; it draws what ViewData holds.
public ActionResult TimeDuration()
{
ViewData["value"] = DateTime.Now;
return View();
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
The Formatting sample illustrates, how to format, disable or append the any HTML Element to the specific list item in a TimePicker component by using itemRender event. Also, the time interval value is displayed in list items and the time value "12:00 AM" , "3:00 AM", "6:00 AM", "9:00 AM", "12:00 PM", "3:00 PM", "6:00 PM" and "9:00 PM" is disabled by adding the e-disabled class.