简体   繁体   English

可观察数组中的KnockoutJS日期时间选择器

[英]KnockoutJS datetime picker in observable array

I am trying to create a page using knockout, the model contains an observable array. 我正在尝试使用敲除来创建页面,该模型包含一个可观察的数组。 One of the properties in each array item is a date, and I am trying to use a jquery ui datepicker. 每个数组项中的属性之一是日期,我正在尝试使用jquery ui datepicker。

I found an example of a custom bindingHandler for a datepicker in this question . 我在此问题中找到了一个用于日期选择器的自定义bindingHandler的示例。 However, when I tried to use it in my code, I get a javascript error during the change event handler. 但是,当我尝试在代码中使用它时,在change事件处理程序中出现了JavaScript错误。

My simplified code: 我的简化代码:

<table>
    <thead>
        <tr>
            <th>My Date</th>
        </tr>
    </thead>
    <tbody data-bind='foreach: visits'>
        <tr>
            <td><input data-bind='datepicker: MyDate' /></td>
        </tr>
     </tbody>
</table>

<script type="text/javascript">
    ko.bindingHandlers.datepicker = {
        init: function(element, valueAccessor, allBindingsAccessor) {
           $(element).datepicker({ dateFormat: 'dd/mm/yy' });

           //handle the field changing
           ko.utils.registerEventHandler(element, "change", function() {
               //get the value accessor
               var observable = valueAccessor();

               //assign the observable value - code breaks here with 'Function expected'
               observable($(element).datepicker("getDate"));
            });

            //handle disposal (if KO removes by the template binding)-
            ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
                $(element).datepicker("destroy");
            });

        },
        update: function(element, valueAccessor) {
            var value = ko.utils.unwrapObservable(valueAccessor());

            //handle date data coming via json from Microsoft
            if (String(value).indexOf('/Date(') == 0) {
                value = new Date(parseInt(value.replace(/\/Date\((.*?)\)\//gi, "$1")));
            }

            var current = $(element).datepicker("getDate");

            if (value - current !== 0) {
                $(element).datepicker("setDate", value);
            }
        }
    };

    var VisitModel = function(visits) {
        var self = this;
        self.visits = ko.observableArray(visits);

        self.getVisitsJSON = function() {
            return ko.utils.stringifyJson(self.visits);
        }
     };

     var visitModel = new VisitModel([{"MyDate":"01/01/2013"}]);
     ko.applyBindings(visitModel);
</script>

As in the comments in my code, I get an error saying 'Function expected' when I call observable($(element).datepicker("getDate")); 就像我代码中的注释一样,当我调用observable($(element).datepicker("getDate"));时,我收到一条错误消息,提示“功能预期” observable($(element).datepicker("getDate")); . I am quite new to knockoutjs and I am not sure why I am getting this error, can anyone help explain? 我对敲除js很陌生,我不确定为什么会收到此错误,有人可以帮忙解释一下吗?

You need to wrap the contents of your array into their own view models with observable properties. 您需要将数组的内容包装到具有可观察属性的自己的视图模型中。 Something like this might work: 这样的事情可能会起作用:

var VisitModel = function(visits) {
    var self = this;
    self.visits = ko.observableArray();

    for (var i = 0; i < visits.length; i++) {
        self.visits.push(new DateModel(visits[i]);
    }

    self.getVisitsJSON = function() {
        return ko.utils.stringifyJson(self.visits);
    }
 };

 var DateModel = function(date) {
     var self = this;
     self.MyDate = ko.observable(date.MyDate);
 }

 var visitModel = new VisitModel([{"MyDate":"01/01/2013"}]);
 ko.applyBindings(visitModel);

Now when you use valueAccessor you should get back the ko.observable which is a function. 现在,当您使用valueAccessor您应该返回ko.observable这是一个函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM