This is a migrated thread and some comments may be shown as answers.

DateTimePicker width expanding to 100% when programmatically setting the Time

1 Answer 1009 Views
Date/Time Pickers
This is a migrated thread and some comments may be shown as answers.
Nathan J Pledger
Top achievements
Rank 2
Nathan J Pledger asked on 06 Jan 2015, 05:00 PM
I have an ASP.NET Kendo/MVC DateTimePicker:

@(Html.Kendo()
                .DateTimePickerFor(m => m.ResponseRequiredBy)
                .Events(e =>
                {
                    e.Change("ResponseRequiredBy_OnChange");
                }))

We have a requirement that when a user selects a day, we set a default time within the day if not already selected. I use this code to achieve this:

function ResponseRequiredBy_OnChange() {
    if (this.value().getHours() == 0 &&
        this.value().getMinutes() == 0) {
        var newDate = new Date(this.value().getFullYear(), this.value().getMonth(), this.value().getDate(), 16, 0, 0, 0);
        $("#ResponseRequiredBy").kendoDateTimePicker({
            value: newDate,
            format: "dd/MM/yyyy h:mm tt"
        });
 
        // overcomes bug whereby Kendo expands field to full width
        $("#ResponseRequiredBy").attr("style", "");
        $(".k-widget.k-datetimepicker.k-header.k-input").attr("style", "");
    }      
}

BUT when I set the new value, the width of the picker expands to 100% for some reason (see attachments).

I have to add the commented code to fix the issue.

Why would this be happening?

Thanks

Nathan

1 Answer, 1 is accepted

Sort by
0
Kiril Nikolov
Telerik team
answered on 08 Jan 2015, 09:42 AM

Hello Nathan,

The problem is caused by the fact that you are initializing the widget more than once, in your change event calling the kendoDateTimePicker() method will initialize a DateTimePicker over the existing DateTimePicker, which can cause some unexpected behavior. If you want to change the value of the widget, you can use its value method, available via the this keyword:

function ResponseRequiredBy_OnChange() {
   this.value(your-new-value)
}

Regards,
Kiril Nikolov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Date/Time Pickers
Asked by
Nathan J Pledger
Top achievements
Rank 2
Answers by
Kiril Nikolov
Telerik team
Share this question
or