Knockout -如何订阅多个下拉菜单(选择)并绑定到模型
knockout - how to subscribe for multiple dropdown menus (select) and bind to a model
javascript爱好者和开发者,
我如何将多个下拉框绑定到一个模型,并为每个下拉框单独订阅其更改事件?
我有一个基本的形式…您可以在jsFiddle: http://jsfiddle.net/2Mnr3/7/中看到它
为什么当我选择一个字段时,所有的选择字段一起更改?我怎么能单独做呢?
<div class='liveExample'>
<h2>Orders</h2>
<div id='contactsList'>
<table class='contactsEditor'>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Service</th>
</tr>
<tbody data-bind="foreach: contacts">
<tr>
<td>
<input data-bind='value: firstName' />
<div><a href='#' data-bind='click: $root.removeContact'>Delete</a></div>
</td>
<td><input data-bind='value: lastName' /></td>
<td>
<table>
<tbody data-bind="foreach: services">
<tr>
<td>
<select data-bind='options: catalog, value: $root.selectedId, optionsText: "name", optionsCaption: "Select..."'> </select>
</td>
<td>
<div data-bind="visible: $root.selectedId()">
<span data-bind='text: $root.selectedId.price'> </span>
<!--<span data-bind='text: "asd"'> </span>-->
</div>
<td>
<a href='#' data-bind='click: $root.removeService'>Delete</a></td>
</tr>
</tbody>
</table>
<a href='#' data-bind='click: $root.addService'>Add service</a>
</td>
</tr>
</tbody>
</table>
</div>
<p>
<button data-bind='click: addContact'>Add customer </button>
<button data-bind='click: save, enable: contacts().length > 0'>JSON</button>
</p>
<textarea data-bind='value: lastSavedJson' rows='5' cols='60' disabled
='disabled'> </textarea>
和Javascript代码与Knockout库:
function formatCurrency(value) {
console.log(value);
return value;
}
var serviceTypes = [
{ name: "Service One", id: "1", price: "10 USD"},
{ name: "Service Two", id: "2", price: "9 USD"},
{ name: "Service Three", id: "3", price: "25 USD"},
{ name: "Service Four", id: "4", price: "42 USD"}
];
var initialData = [
{ firstName: "John", lastName: "Carter", services: [{ catalog: serviceTypes, id: 0 }, { catalog: serviceTypes, id: 2 }]
}
];
function ContactsModel(contacts) {
var self = this;
self.contacts = ko.observableArray(ko.utils.arrayMap(contacts, function(contact) {
return { id: contact.id, firstName: contact.firstName, lastName: contact.lastName, services: ko.observableArray(contact.services) };
}));
self.serviceTypes = ko.observableArray(serviceTypes);
self.selectedId = ko.observable('1');
self.selectedId.subscribe(function(item){
return ko.utils.arrayFirst(serviceTypes, function(service) {
return service;
});
});
self.addContact = function() {
self.contacts.push({
firstName: "",
lastName: "",
services: ko.observableArray([
{
catalog: this.serviceTypes,
}])
});
};
self.removeContact = function(contact) {
self.contacts.remove(contact);
};
self.addService = function(contact) {
contact.services.push({
catalog: self.serviceTypes,
});
};
self.removeService = function(phone) {
$.each(self.contacts(), function() { this.services.remove(phone) })
};
self.save = function() {
self.lastSavedJson(JSON.stringify(ko.toJS(self.contacts), null, 2));
};
self.lastSavedJson = ko.observable("")
};
ko.applyBindings(new ContactsModel(initialData));
您共享相同的可观察对象($root.selectedId),而不是这样,每个目录都应该有自己的selectedId副本。为此,您可以使用构造函数,例如
function Catalog(serviceTypes, d) {
this.catalog = serviceTypes;
this.selectedId = ko.observable(d || null);
this.selectedId.subscribe(function (item) {
//Subscriber Handler
});
}
var initialData = [{
firstName: "John",
lastName: "Carter",
services: [new Catalog(serviceTypes, 1), new Catalog(serviceTypes, 2)]
}];
addContact和addService功能也做了修改。
self.addContact = function () {
self.contacts.push({
firstName: "",
lastName: "",
services: ko.observableArray([new Catalog(serviceTypes)])
});
};
self.addService = function (contact) {
contact.services.push(new Catalog(serviceTypes));
};
<<p> 小提琴演示/strong> 您将所有services
select
元素绑定到相同的$root.selectedId
值。这就是为什么当它们中的任何一个发生变化时,相应的subscribe
就会被发射。
需要根据contact
记录动态绑定值,例如
相关文章:
- Ember选择绑定-如何使其着火
- 由其他Ember Select填充的Ember Select不更新选择绑定
- 角度选择绑定字典
- 选择绑定到同一范围的按钮,单击时会相互触发.如何从angular.js中的两个选择列表按钮解除范围绑定
- 如何将 HTML 选择绑定到 Rivets.JS 中的模型值
- 在呈现项目后,挖空选择绑定回调的选项
- 在 angularjs 中使用选择绑定
- Knockout.js选择绑定到对象的值
- 使用angularjs、jquery、json、ajax基于下拉选择绑定html表数据
- knockoutjs选项对选项和选择绑定的适应
- 取消验证,选择绑定,不添加erroelement .css
- 从选择绑定中删除数据绑定没有更新自定义事件
- Angular选择——绑定到模型(而不是值)
- 在Angularjs中选择绑定ng-model的对象
- Angular js选择绑定
- Knockout:选择绑定返回值数组
- Emberjs选择绑定
- 使用enter键选择绑定到输入的Datalist元素
- 删除自定义选择绑定器与更新功能
- dom事件-JavaScript onchange选择绑定问题