通过knockout从视图模型中显示日期

时间:2012-07-03 17:15:30

标签: knockout.js

我希望能够根据下拉列表的选择显示日期。下拉列表从MVC3中的ViewModel填充,并在页面加载时序列化为Json。

我试着玩订阅,更改等等但无济于事。当你在下面的jsfiddle链接中选择一个单位时,应该在右侧的文本框中填充事件日期作为默认值 - 用户仍然可以更改它。我们在unitDropDown可观察数组中有这些信息。在我看到其他例子中的情况时,我可能很密集,对我而言,它不会点击这么说。

我猜我必须在addEvent observable中添加一些事件?然后还以某种方式设置订阅下拉列表?

addEvent: function() {
    this.phaseUnits.push(new PhaseUnitModel('0', '1', '0', 'N', '')); // defaults
}

以下是jsFiddle中的工作示例:http://jsfiddle.net/robcube/eFAmu/

更新(修正):

function PhaseUnitModel(Id, PhaseDetailId, UnitId, eventType, eventDate, deleteFlag) {
        var self = this;
        self.Id = ko.observable(Id);
        self.PhaseDetailId = ko.observable(PhaseDetailId);
        self.UnitId = ko.observable(UnitId);
        self.eventType = ko.observable(eventType);
        self.eventDate = ko.observable(eventDate);
        self.deleteFlag = ko.observable(deleteFlag);
        self.markForDeleteText = ko.observable();

        self.UnitId.subscribe(function(newValue) {
            for (i in unitDropDown) {
                if (unitDropDown[i].Id == newValue)
                    self.eventDate(unitDropDown[i].OnlineDate);
            }
        }.bind(this));
    }

1 个答案:

答案 0 :(得分:1)

这是你在找什么?

function PhaseUnitModel(Id, PhaseDetailId, UnitId, eventType, eventDate, deleteFlag) {
    this.Id = ko.observable(Id);
    this.PhaseDetailId = ko.observable(PhaseDetailId);
    this.UnitId = ko.observable(UnitId);
    this.eventType = ko.observable(eventType);
    this.eventDate = ko.observable(eventDate);
    this.deleteFlag = ko.observable(deleteFlag);
    this.markForDeleteText = ko.observable();

    this.UnitId.subscribe(function(newValue) {
        for (i in unitDropDown) {
            if (unitDropDown[i].Id == newValue)
                this.eventDate(unitDropDown[i].OnlineDate);
        }
    }.bind(this));
}