Knockout DateTime Picker - Default Date not Bindin

2019-04-10 22:24发布

I am using this Bootstrap DateTime Picker: http://eonasdan.github.io/bootstrap-datetimepicker/

I have created a Custom Binder for DateTime called datepicker:

ko.bindingHandlers.datepicker = {
    init: function(element, valueAccessor, allBindingsAccessor) {
        //initialize datepicker with some optional options
        var options = allBindingsAccessor().datepickerOptions || {format: 'DD/MM/YYYY HH:mm'};
        $(element).datetimepicker(options);

        //when a user changes the date, update the view model
        ko.utils.registerEventHandler(element, "dp.change", function(event) {
            var value = valueAccessor();
            if (ko.isObservable(value)) {
                value(event.date);
            }
        });
    },
    update: function(element, valueAccessor)   {
        var widget = $(element).data("datepicker");
        //when the view model is updated, update the widget
        if (widget) {
            widget.date = ko.utils.unwrapObservable(valueAccessor());
            if (widget.date) {
                widget.setValue();
            }
        }
    }
};

And I My Model is in JSON, as I am converting C# dateTime to JSON I get this Date: "/Date(1427368178393)/"

            <div class="col-md-4">
                <div class="form-group">
                    <label class="control-label">Quote Date</label>
                    <div class='input-group date dateTimes'>
                        <input type="text" class="form-control" data-bind="datepicker: QuoteDateTime" />
                        <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
                    </div>
                </div>
            </div>

But the Problem is that the first time the Page Loads even though the QuoteDateTime has a value it is not showing in the DateTimePicker.

Here is the JSFiddle:

https://jsfiddle.net/mdawood1991/ezv7qxbt/

I need the First Value to Show in the DateTime Picker.

EDIT

The thing is that my ViewModel has a List of Accommodations

This is the Console Log of my viewModel

enter image description here

I could convert every dateTime to a moment as you suggester, but then I shouldn't use the knockout Mapping Plugin: http://knockoutjs.com/documentation/plugins-mapping.html

Is this the only solution to loop through my viewModel and then converting alld Datetimes to a moment object

3条回答
Emotional °昔
2楼-- · 2019-04-10 22:53

I think you need to use the default date option.

var options = allBindingsAccessor().datepickerOptions || {
                format: 'DD/MM/YYYY HH:mm',
                defaultDate: valueAccessor()()
            };

Updated JSFiddle: https://jsfiddle.net/3jqL8s1m/

查看更多
Fickle 薄情
3楼-- · 2019-04-10 22:55

Your update function doesn't work at all. Making it work will fix your issue and cause updates to the observable to be reflected in the control.

  1. Use $(element).data("DateTimePicker")
  2. Convert the date string to a moment.js object
  3. Pass it in using date() function.

Code:

update: function(element, valueAccessor)   {
    var widget = $(element).data("DateTimePicker");

    if (widget) {
        var date = ko.utils.unwrapObservable(valueAccessor());

        if (typeof date === "string") {
            // convert c# string to momentjs object
            date = moment(date);
        }

        widget.date(date);
    }
}

Edit -- Option 2: Keep everything as a moment.js object

An issue with your code however is that a string is passed into the observable, but whenever this custom binding updates that observable it makes it a moment.js object (input -> string and output -> Moment). To fix this, I would recommend having the user pass in a moment.js object to the observable (input -> Moment and output -> Moment).

First, convert your c# string to a moment.js object when you fill your view model:

self.QuoteDateTime = ko.observable(moment(data.QuoteDateTime));

Second, change your update function to handle moment.js objects instead of strings:

update: function(element, valueAccessor)   {
    var widget = $(element).data("DateTimePicker");
    //when the view model is updated, update the widget
    if (widget) {
        var date = ko.utils.unwrapObservable(valueAccessor());
        widget.date(date);
    }
}

You can see this working just fine in this jsfiddle:

https://jsfiddle.net/ezv7qxbt/21/

查看更多
疯言疯语
4楼-- · 2019-04-10 23:03

After help From @David and @Bryant I changed my code to update viewModel with moment object date.

The Problem was that the Date was only posting back correctly after selecting a Date from the DateTime Picker. So I changed code so when Initialized I update the viewModel. And it is working now.

This is the DateTime Picker

http://eonasdan.github.io/bootstrap-datetimepicker/

I used the following datepicker binding.

JSFiddle Working: http://jsfiddle.net/mdawood1991/cL9k2sbe/

My Solution:

ko.bindingHandlers.datepicker = {
    init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
        //initialize datepicker with some optional options
        var options = {
            format: 'DD/MM/YYYY HH:mm',
            defaultDate: valueAccessor()()
        };

        if (allBindingsAccessor() !== undefined) {
            if (allBindingsAccessor().datepickerOptions !== undefined) {
                options.format = allBindingsAccessor().datepickerOptions.format !== undefined ? allBindingsAccessor().datepickerOptions.format : options.format;
            }
        }

        $(element).datetimepicker(options);

        //when a user changes the date, update the view model
        ko.utils.registerEventHandler(element, "dp.change", function (event) {
            var value = valueAccessor();
            if (ko.isObservable(value)) {
                value(event.date);
            }
        });

        var defaultVal = $(element).val();
        var value = valueAccessor();
        value(moment(defaultVal, options.format));
    },
    update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
        var thisFormat = 'DD/MM/YYYY HH:mm';

        if (allBindingsAccessor() !== undefined) {
            if (allBindingsAccessor().datepickerOptions !== undefined) {
                thisFormat = allBindingsAccessor().datepickerOptions.format !== undefined ? allBindingsAccessor().datepickerOptions.format : thisFormat;
            }
        }

        var value = valueAccessor();
        var unwrapped = ko.utils.unwrapObservable(value());

        if (unwrapped === undefined || unwrapped === null) {
            element.value = new moment(new Date());
            console.log("undefined");
        } else {
            element.value = unwrapped.format(thisFormat);
        }
    }
};
查看更多
登录 后发表回答