Setting default time for time picker

Setting default time for time picker

epoch1epoch1 Posts: 17Questions: 8Answers: 1

Another one for the mighty Allan I think.

Is there an easy way to set a default time when the time picker widget displays or initially sets the time in an input field?

At the moment it defaults to the current hour and '-' for minutes (I've got 15 min increments set). When the on chnage event is fired by a user changing the hour, the input field updates to show the selected hour and the current minute. If the current time is 09:33 and I click the timedate field and change the hour select to 10 the input field will display 10:33. I'd like my users to only be able to select either 00,15,30,45, is there a way to default the mins to 00 when the widget is displayed/input field is initially updated?

Also, is it possible to make editor fields read only so users have to use the date/time picker rather than being able to manually enter dates and times?

Cheers

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,710Questions: 1Answers: 10,103 Site admin

    Is there an easy way to set a default time when the time picker widget displays or initially sets the time in an input field?

    The field.def option should do that:

    def:   function () { return new Date(); }
    

    Also, is it possible to make editor fields read only so users have to use the date/time picker rather than being able to manually enter dates and times?

    You could bind a keydown event listener to the input element (field().input()) and simply use e.preventDefault() to stop their key press from having any effect. That would remove copy and paste support as well, unless you add logic to allow them.

    Allan

  • allanallan Posts: 61,710Questions: 1Answers: 10,103 Site admin
    Answer ✓

    Ooops - sorry I missed this bit:

    If the current time is 09:33 and I click the timedate field and change the hour select to 10 the input field will display 10:33. I'd like my users to only be able to select either 00,15,30,45, is there a way to default the mins to 00 when the widget is displayed/input field is initially updated?

    You'd need to add that rounding option into the def function. Take the current time and round it to be 0, 15, 30 or 45. Editor's date time picker won't do that rounding for you I'm afraid.

    I'm in two minds as it if it should or not! I can see that it might be useful without question, but at the same time, if you feed it "33" for example and it rounds to "30", rather might be considered to corrupt the data...

    Allan

  • epoch1epoch1 Posts: 17Questions: 8Answers: 1

    Brilliant, thank you again for all your help Allan.

  • imarderimarder Posts: 12Questions: 5Answers: 2

    Allan,
    I note that this def: option works fine when creating a new record.

    But I am seeing that def: does not work when editing an existing record. i.e. I have an existing record with a blank datetime field. I wish to have the datetime picker default to a rounded 00,15,30,45 minute when editing that blank field for the first time.
    In the editor, how can one set that default rounded date/time in the spawned datetime calendar without populating the field first?

    I am trying to do this within

    editor.on( 'open', function () {
    editor.val('scheduledpickup', rounded);
    });
    

    But it is already pre-populating the field but I do not want that yet until the calendar datetime is set.

    BTW this is what I use for rounding

    var interval = 15;
    var coeff = 1000 * 60 * interval;
    var date = new Date();  //or use any other date
    var rounded = new Date(Math.round(date.getTime() / coeff) * coeff);
    
  • allanallan Posts: 61,710Questions: 1Answers: 10,103 Site admin

    The default is more or less ignored when editing (rather than creating) since the field already has a value - even if that value is empty. Replacing an existing value with the default would be wrong, which is why it isn't going to work using the default value.

    What I would suggest is that you use initEdit to check the values and replace / update them if needed on triggering editing.

    Allan

This discussion has been closed.