击倒多个下拉菜单,添加新项目
Knockout Mutliple Dropdowns that adds new items
是否有可能让我有多个下拉菜单(不是特定数量),其中有项目和一个新项目选项,将新项目添加到下拉列表。
例如,将有~5个下拉框,用户选择项目编号。当他们选择New Item时,它会向列表中添加一个Item
这是到目前为止,我得到它,不知道我如何处理这个问题与淘汰。这真的可能吗?
<select data-bind="options: items, optionsText: 'name', optionsValue: 'id', value: selectedChoice"></select>
<br/>
<select data-bind="options: items, optionsText: 'name', optionsValue: 'id', value: selectedChoice"></select>
<br/>
<select data-bind="options: items, optionsText: 'name', optionsValue: 'id', value: selectedChoice"></select>
<br/>
var Item = function(data){
var self = this;
self.id = ko.observable(data.id);
self.name = ko.observable(data.name);
};
var viewModel = function(data) {
var self = this;
self.selectedChoice = ko.observable();
self.items = ko.observableArray([
new Item({id: "1", name: "Item 1"}),
new Item({id: "2", name: "Item 2"}),
new Item({id: "3", name: "New Item"})]);
self.sendMe = function(){
alert(ko.toJSON({ selectedItemId: this.selectedChoice()}));
};
};
ko.applyBindings(new viewModel());
https://jsfiddle.net/dqUAz/1470/实现此目的的一种方法是订阅selectedChoice可观察对象,并在'New Item'被选中时更新数组:
self.selectedChoice.subscribe(function(newValue) {
var lastItem = self.items()[self.items().length - 1];
if (newValue === lastItem.id()) {
// Add the new item
var id = self.items().length + 1;
var name = 'Item ' + self.items().length;
var item = new Item({id: id, name: name});
self.items.push(item);
// Drop and re-add the 'New Item' item so that it remains at the bottom
self.items.remove(lastItem);
self.items.push(lastItem);
// Select the newly added item
self.selectedChoice(id);
}
});
小提琴:https://jsfiddle.net/dw1284/60n7078s/2/
相关文章:
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- Ng-Repeat在末尾添加新项目
- jQuery UI Accordian.我需要激活新添加的项目
- 在 SharePoint 2010 上创建“添加新项目”按钮
- 如何将新项目添加到 srt 文件中
- 在SlickGrid的dataView的第一行添加一个新项目,就像JavaScript的unshift一样
- 当一个新项目被添加到FireBug控制台时,是否有一个jQuery事件等同于
- 嵌套可排序新添加的项目不会移动
- 如何使用javascript清除所有项目并在asp.net复选框列表中添加新项目
- 如何将选定的可观察对象附加到新添加的项目
- 如何防止栅格自动选择新添加的项目,保留当前选择
- 添加新文件时在eclipse中刷新项目
- 在knockout.js中添加具有多个属性的新项目
- 使用$resource添加新项目
- 光滑滑块-将新项目添加到中心
- 当新项目被添加到SharePoint日历时,正在运行Javascript函数
- 击倒多个下拉菜单,添加新项目
- 角和同位素-在同位素上下文中添加新项目
- jCarousel在末尾不断添加新项目
- 为什么添加新项目时我的列表金额计数不会更新