如何将选定的可观察对象附加到新添加的项目
how to attach a selected observable to a newly added item?
我是淘汰赛的新手。我已经有了这把小提琴:
- 将左侧框填充为项目列表
- 在左侧框中选择项目后,它将在右侧框中显示详细项目
看来我让它正常运行了。但我想添加更多内容。
- 加载后,选择第一个foo及其显示的详细信息
- 单击
+ Add foo
链接后,它将添加新的foo并将其高亮显示,然后右框中的编辑器将出现,然后单击save
时,它将把它添加到foo的集合中
这是负鼠吗?
下方的代码段
var data = [
{ id: 1, name: "foo1", is_active: true },
{ id: 2, name: "foo2", is_active: true },
{ id: 3, name: "foo3", is_active: true },
{ id: 4, name: "foo4", is_active: false },
];
var FooBar = function (selected) {
this.id = ko.observable("");
this.name = ko.observable("");
this.is_active = ko.observable(true);
this.status_text = ko.computed(function () {
return this.is_active() === true ? "Active" : "Inactive";
}, this);
this.is_selected = ko.computed(function () {
return selected() === this;
}, this);
};
var vm = (function () {
var foo_bars = ko.observableArray([]),
selected_foo = ko.observable(),
load = function () {
for (var i = 0; i < data.length; i++) {
foo_bars.push(new FooBar(selected_foo)
.name(data[i].name)
.is_active(data[i].is_active)
.id(data[i].id));
}
},
select_foo = function (item) {
selected_foo(item);
},
add_foo = function () {
// I have tried this but ain't working at all. Please help
// foo_bars.push(new FooBar(selected_foo));
};
return {
foo_bars: foo_bars,
load: load,
selected_foo: selected_foo,
select_foo: select_foo,
add_foo: add_foo
};
}());
vm.load();
ko.applyBindings(vm);
.box {
float: left;
width: 250px;
height: 250px;
border: 1px solid #ccc;
}
.selected {
background-color: #ffa !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div class="box">
<a href="#" data-bind="click: add_foo">+ Add foo</a>
<table>
<thead>
<tr>
<th>Name</th>
<th>Status</th>
</tr>
</thead>
<tbody data-bind="foreach: foo_bars">
<tr data-bind="click: $root.select_foo, css: { selected: is_selected }">
<td><a href="#" data-bind="text: $data.name"></a></td>
<td data-bind="text: $data.status_text"></td>
</tr>
</tbody>
</table>
</div>
<div class="box" data-bind="with: selected_foo">
Id: <span data-bind="text: id"></span><br />
Name: <input type="text" data-bind="value: name" /><br />
Status: <input type="checkbox" data-bind="checked: is_active"> <span data-bind="text: status_text"></span><br />
<button>Save</button>
</div>
演示:http://jsfiddle.net/wguhqn86/4/
load = function () {
for(var i = 0; i < data.length; i++){
foo_bars.push(new FooBar(selected_foo)
.name(data[i].name)
.is_active(data[i].is_active)
.id(data[i].id)
);
}
selected_foo(foo_bars()[0]); // default select first foo
},
add_foo = function() {
var count = foo_bars().length + 1;
var newItem = new FooBar(selected_foo).name('')
.is_active(true)
.id(count)
foo_bars.push(newItem);
selected_foo(newItem);
};
您添加的新对象没有指定名称和id,这可能就是它没有显示的原因。除此之外,添加新对象后,您将需要选择它,然后它才能显示为左侧面板中的选定对象。下面是我对你的添加方法的代码定义
add_foo = function() {
var new_foo=new FooBar(selected_foo)
.name("foo"+(foo_bars().length+1));
select_foo(new_foo);
foo_bars.push(new_foo);
};
完整的工作小提琴可以在这里找到http://jsfiddle.net/euq48em4/1/
相关文章:
- TinyMCE在新添加的文本区域
- 在Javascript中,当使用array.prush方法向列表中添加项目时,如何对其进行排序
- 在不向上滚动的情况下向列表框中添加项目
- 提交评论不适用于新添加的帖子
- jQuery:更改后的输入值不用于新添加
- 为图像滑块jquery添加项目符号功能
- 动态表行,将单击事件委派给新添加的元素
- 如何重置新添加的表单输入
- 超时后无法删除新添加的 HTML 元素
- 如何在数组中以ng个重复间隔应用新添加的对象
- 在Javascript中使用JQueryMobile向列表添加项目
- ng只重复添加项目一次
- jQuery无法在新添加的空跨度内设置光标
- 制作从上一个已知点到新添加点(d3)的路径(线)的动画
- jQuery UI Accordian.我需要激活新添加的项目
- 嵌套可排序新添加的项目不会移动
- 如何将选定的可观察对象附加到新添加的项目
- 如何防止栅格自动选择新添加的项目,保留当前选择
- JS:比较数组和添加新的项目或删除不再在数组中的项目
- Javascript动画fadeIn对新添加的项目不起作用