var ko = require("knockout");
var komap = require("/scripts/plugins/knockout/knockout.mapping.js");
_viewListModel = komap.fromJS(data);
ko.bindingHandlers.datePicker = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
//注册监听值改变事件
ko.utils.registerEventHandler(element, "change", function () {
var value = valueAccessor();
//转换
var string = "/Date(" + moment(element.value, "YYYY-MM-DD HH:mm").valueOf() + "+0800)/";
//转换后的值给原model
value(string);
});
},
// Update the control whenever the view model changes
update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
var value = valueAccessor();
element.value = moment(value()).format("YYYY-MM-DD HH:mm");
}
};
//绑定视图数据
ko.applyBindings({ lists: _viewListModel }, document.getElementById("listBody"));
<input type="text" data-bind="datePicker: EndDateTime" />