如何将日期时间选择器条目存储在可观察数组中?

How to store Date Time Picker Entries in observable array?

我正在分享 link 我想做的事情(link 中的最后一个,即日期时间

https://rsuite.github.io/multi-date-picker/

完成此操作后,我想将这些条目存储在 可观察数组

注意:我想在简单的记事本和 jQuery 日期时间选择器

中完成所有操作

function Friend() {
  $(document).ready(function() {
      $("#datetime").datetimepicker();
  });
  var self = this;
  self.dates = ko.observable();
  self.removeFriend=function() {
    obj.friends.remove(self);
  }
}
var obj = {
  friends:ko.observableArray([new Friend()])
};
obj.addFriend=function() {
  obj.friends.push(new Friend());
}
ko.applyBindings(obj);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<script src="
https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.20/jquery.datetimepicker.full.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.20/jquery.datetimepicker.min.css">
<h1>Hello JS</h1>
<ul data-bind="foreach:friends">
  <li>
    <input readonly type="text" id="datetime" data-bind="value:dates" />
    <button data-bind="click: removeFriend">Remove Employee</button>
  </li>
</ul>
<button data-bind="click: addFriend">Add Employee</button>

我对此很陌生。

你的代码有几个问题:

  1. 主要问题是您直接使用 jquery 初始化 input,而您应该将此责任委托给自定义绑定。幸运的是,其他人已经为您完成了这项工作(参见 here and 。在下面的代码中,我使用那里给出的代码)。

  2. 此外,每个新添加的员工都有相同的 id 值,所以你应该给每个人一个唯一的 id,或者干脆去掉它——这就是我在下面所做的.

  3. removeFriend应该在父对象中定义。

此外,请注意,您链接到的 website 中使用的日期选择器不是您在整个问题中引用的名为 jquery-datetimepicker 的库,因此也是我所指的。

这是工作(重新格式化)的代码:

$(document).ready(function() {
  initCustomBinding();
  initFriends();
});
function initFriends() {
  var obj = {
    friends: ko.observableArray([new Friend()]),
    addFriend: function() {
      obj.friends.push(new Friend());
    },
    removeFriend: function($data) {
      obj.friends.remove($data);
    }
  };
  ko.applyBindings(obj);
}
function Friend() {
  var self = this;
  self.dates = ko.observable();
}
function initCustomBinding() {
  ko.bindingHandlers.datetimepicker = {
    init: function (element, valueAccessor, allBindingsAccessor) {
        var $el = $(element);
        //initialize datepicker with some optional options
        var options = allBindingsAccessor().datepickerOptions || {};
        $el.datetimepicker(options);

        //handle the field changing
        ko.utils.registerEventHandler(element, "change", function () {
            var observable = valueAccessor();
            var $el = $(element);
            observable($el.datetimepicker("getDate").Value);
        });

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

    },
    update: function (element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor()),
            $el = $(element),
            current = new Date($el.val());

        if (value - current !== 0) {
            $el.datetimepicker("setDate", value);
        }
    }
  };
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<script src="
https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.20/jquery.datetimepicker.full.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.20/jquery.datetimepicker.min.css">
<h1>Hello JS</h1>
<ul data-bind="foreach:friends">
  <li>
    <input readonly type="text" data-bind="datetimepicker:dates" />
    <button data-bind="click: $root.removeFriend">Remove Employee</button>
  </li>
</ul>
<button data-bind="click: addFriend">Add Employee</button>