KnockoutJS处理bootstrap datepickers和日期格式

时间:2013-12-02 23:50:02

标签: javascript knockout.js twitter-bootstrap-3

这是我正在使用的日期选择器绑定(我很抱歉,但我忘记了我把它拿走了所以我无法给予作者适当的信用。如果我记得,我会编辑。

ko.bindingHandlers.datepicker = {
    init: function(element, valueAccessor, allBindingsAccessor) {
      //initialize datepicker with some optional options
      var options = allBindingsAccessor().datepickerOptions || {};
      $(element).datepicker(options);

      //when a user changes the date, update the view model
      ko.utils.registerEventHandler(element, "changeDate", 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();            
            }
        }
    }
};

function Model() {
  this.date = ko.observable();
}

function VM() {
  this.model = new Model();
  this.save = function(model) {
    $.post(someEndpoint, {model: model});
  }
}

<input type='text' data-bind='datepicker: model().date, 
                              datepickerOptions: { format:"dd/mm/yyyy"}' />

这个datepicker绑定和一般的Bootstrap datepicker处理Date对象。所以我的观察结果将包含日期。

当我发布数据时,我在控制台中看到它以自然的JS toString()格式发布,这可能会根据您的区域设置而有所不同。对我来说,它的

Mon Dec 02 2013 01:00:00 GMT+0100 (ora solare Europa occidentale)

当我收到服务器端的这个值时,由于这种奇怪的格式,我无法处理它。

我当然可以在发布之前重写我的模型日期属性,就像这样

  this.save = function(model) {
    model.date(model.date().format('YYYY-MM-DD'));
    $.post(someEndpoint, {model: model});
  }

但这不是一个通用的解决方案,而且当model.date更新为字符串表示时,datepicker会触发错误,因为它需要Date。

你会怎么处理这个?

1 个答案:

答案 0 :(得分:2)

对于 Bootstrap 3.0及更高版本,这些对我有用:

仅限日期 为了显示日期,我使用了这个bindingHandler:

window.ko.bindingHandlers.datetext = {
    init: function (element, valueAccessor, allBindingsAccessor) {
        // Provide a custom text value
        var value = valueAccessor(), allBindings = allBindingsAccessor();
        var dateFormat = allBindingsAccessor.dateFormat || "M/D/YYYY";
        var strDate = window.ko.utils.unwrapObservable(value);
        if (strDate) {
            if (moment(strDate).year() > 1970) {
                var date = moment(strDate).format(dateFormat);
                $(element).text(date);
            }
            else {
                $(element).text("-");
            }
        }
    },
    update: function (element, valueAccessor, allBindingsAccessor) {
        // Provide a custom text value
        var value = valueAccessor(), allBindings = allBindingsAccessor();
        var dateFormat = allBindingsAccessor.dateFormat || "M/D/YYYY";
        var strDate = window.ko.utils.unwrapObservable(value);
        if (strDate) {
            if (moment(strDate).year() > 1970) {
                var date = moment(strDate).format(dateFormat);
                $(element).text(date);
            }
            else {
                $(element).text("-");
            }
        }
    }
};

HTML

<!-- Display the date -->
<span data-bind="datetext: StartingDate"></span>

<!-- Set the date -->
<div class="input-group date">
   <input type="text" class="form-control" data-provide="datepicker" data-bind="value: StartingDate"/>
   <span class="input-group-addon">
       <span class="glyphicon glyphicon-calendar"></span>
   </span>
</div>

您需要Moment.js。用于此的日期选择器位于here 我正在使用C#后端,并且发布到MVC控制器或Web API控制器不会导致DateTime变量出现问题。即:

this.save = function(model) {
    $.post(someEndpoint, {model: ko.toJSON(model)});
  }

日期和时间 为了显示,我使用了这个bindingHandler:

window.ko.bindingHandlers.datetimetext = {
    init: function(element, valueAccessor, allBindingsAccessor) {
        // Provide a custom text value
        var value = valueAccessor(), allBindings = allBindingsAccessor();
        var dateFormat = allBindingsAccessor.dateFormat || "M/D/YYYY h:mm a";
        var strDate = window.ko.utils.unwrapObservable(value);
        if (strDate) {
            if (moment(strDate).year() > 1970) {
                var date = moment(strDate).format(dateFormat);
                $(element).text(date);
            } else {
                $(element).text("-");
            }
        }
    },
    update: function(element, valueAccessor, allBindingsAccessor) {
        // Provide a custom text value
        var value = valueAccessor(), allBindings = allBindingsAccessor();
        var dateFormat = allBindingsAccessor.dateFormat || "M/D/YYYY h:mm a";
        var strDate = window.ko.utils.unwrapObservable(value);
        if (strDate) {
            if (moment(strDate).year() > 1970) {
                var date = moment(strDate).format(dateFormat);
                $(element).text(date);
            } else {
                $(element).text("-");
            }
        }
    }
};

为了设置日期时间,我使用这个处理程序:

window.ko.bindingHandlers.datetimepicker = {
    init: function(element, valueAccessor, allBindingsAccessor) {
        $(element).parent().datetimepicker();

        window.ko.utils.registerEventHandler($(element).parent(), "change.dp", function (event) {
            var value = valueAccessor();
            if (window.ko.isObservable(value)) {
                var thedate = $(element).parent().data("DateTimePicker").getDate();
                value(moment(thedate).toDate());
            }
        });
    },
    update: function(element, valueAccessor) {
        var widget = $(element).parent().data("DateTimePicker");
        //when the view model is updated, update the widget
        var thedate = new Date(parseInt(window.ko.utils.unwrapObservable(valueAccessor()).toString().substr(6)));
        widget.setDate(thedate);
    }
};

HTML

<!-- Display the date -->
<span data-bind="datetimetext: FromDate"></span>

<!-- Set the date -->
<a data-bind="attr:{id: Id}" class="input-group date">
   <input type="text" class="form-control" data-bind="datetimepicker: FromDate" />
   <span class="input-group-addon">
        <span class="glyphicon glyphicon-calendar"></span>
   </span>
</a>

我的服务器端方法如下所示:

    [HttpPost]
    public async Task<ActionResult> Update(MyViewModel myModel)
    {
        if (myModel.FromDate == new DateTime(0001, 01, 01))
        {
            if (!string.IsNullOrEmpty(myModel.sFrom))
            {
                myModel.sFrom = myModel.sFrom.Replace("/Date(", "").Replace(")", "").Replace("/", "").Trim();
                var ticks = Convert.ToInt64(myModel.sFrom);
                myModel.FromDate = new DateTime(1970, 1, 1).AddTicks(ticks * 10000).AddHours(-6);
            }
        }

        var item = Mapper.Map<MyClass>(myModel);
        await Task.Run(() => _myService.Save(item));
        return Json(myModel);
    }

使用与此类似的模型:

public class MyViewModel
{
    public Guid Id { get; set; }
    public DateTime FromDate { get; set; }
    public string sFrom { get; set; }
}

日期(sFrom)的字符串版本是在我发布到服务器之前在我的Javascript中生成的:

function saveModel(model) {
    model.sFrom(model.FromDate().toString());

    $.post(someendpoint, { model: ko.toJSON(model) }, function(data) {
        ko.mapping.fromJS(data, {}, model);
    });
}

同样,这会使用Moment.js,而此版本的datetimepicker为here