I am using a datePicker Primefaces element in order to allow the user to select a value for the hours, minutes and seconds. Currently, the hour has values between 0 and 24, the minutes are between 0 and 60 and same for the seconds.
I want to limit this possibility of choice to some static values, obtained from getting the length of a video posted on the page.
So, for example, if the video has 1 minute and 30 seconds, I want the hours part not to allow the user to press the arrow up or down, the minutes to be maximum 30 and the minutes 1.
I tried the below code, but it does not work – I manage to see the max length of the video when I enter the page, but if I move the arrows from the datePicker element, I can select anything.
What can I do?
The UI datePicker element:
<p:outputPanel styleClass="input-group timestamp-input">
<p:datePicker id="timeDatePicker"
widgetVar="widgetVarOfDatePicker"
value="#{traineeDashboard.newIncidentTimestamp}"
timeOnly="true"
showSeconds="true"
appendTo="@this">
<f:converter converterId="timeConverter"/>
</p:datePicker>
JS Code:
function setMaxTimeForDatePicker(hours, minutes, seconds) {
var timePicker = PF('widgetVarOfDatePicker');
var hoursDropdown = $('div.ui-hour-picker span:eq(1)');
var minutesDropdown = $('div.ui-minute-picker span:eq(1)');
var secondsDropdown = $('div.ui-second-picker span:eq(1)');
hoursDropdown.text(hours);
minutesDropdown.text(minutes);
secondsDropdown.text(seconds);
hoursDropdown.trigger('change');
minutesDropdown.trigger('change');
secondsDropdown.trigger('change');
}
$(function() {
var timePicker = PF('widgetVarOfDatePicker'); // Replace with your actual widgetVar
// Override the behaviors for incrementing and decrementing hours, minutes, and seconds
timePicker.cfg.onHourChange = function(newHour) {
let maxHour = 2;
let minHour = 0;
if (newHour < minHour || newHour > maxHour) {
// Set the hour back to the minimum or maximum as appropriate
this.setTime(minHour, this.currentMinute, this.currentSecond);
}
};
timePicker.cfg.onMinuteChange = function(newMinute) {
if (newMinute < minMinute || newMinute > maxMinute) {
// Set the minute back to the minimum or maximum as appropriate
this.setTime(this.currentHour, minMinute, this.currentSecond);
}
};
timePicker.cfg.onSecondChange = function(newSecond) {
if (newSecond < minSecond || newSecond > maxSecond) {
// Set the second back to the minimum or maximum as appropriate
this.setTime(this.currentHour, this.currentMinute, minSecond);
}
};
});