我在 Durandal 项目的 html 页面中有一个 infragistics Ignite UI 组合。
我希望将其 selectedItem
属性绑定(bind)到后面 JavaScript 中的任何变量。
我该怎么做?
谢谢。
最佳答案
看看这个示例:Bind Combo With KnockoutJS (Durandal 使用 Knockout,因此您应该能够获取并重用此示例的大部分代码)。
基本上,在您的 View 模型中,您将需要一个额外的可观察对象来保存当前选择,在此示例中,“actorName”定义为:
function ViewModel(actorsList) {
var self = this;
this.actorsList = actorsList;
this.actors = ko.observableArray(self.actorsList);
// The name of the currently selected customer.
this.actorName = ko.observable(self.actors()[0].name);
}
(我已经编辑了一些,因为示例有第二个选择元素用于修改组合选择)
然后在 View 中的组合中,您希望将此额外的可观察值绑定(bind)到“text”属性,如示例中所示:
<span id="comboActors" data-bind="igCombo: {
text: actorName,
dataSource: actors,
textKey: 'name',
valueKey: 'id',
allowCustomValue : true,
enableSelectionChangedUpdate: true,
width: '200',
mode: 'dropdown',
enableClearButton: false
}"></span>
请注意 name 是您传递给 viewmodel observable 的值,并且相同的“name”属性被定义为文本键,这就是为什么当您设置文本时组合实际上会选择正确的项目。现在您需要做的就是修改该值,例如:
viewModel.actorName("Jeremy Irons");
组合将更改选择(您甚至可以使用控制台中的代码在示例中尝试此操作)。这就是基础知识,Configuring Knockout Support (igCombo) 上有一个帮助主题。 Knockout/Durandal供您查看的文档,以防您无法在项目中使用此实现。
关于javascript - durandal 中的 igCombo 绑定(bind) selectedItems,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19136865/