如何在 Knockout JS 中显示仅包含数组列表中选定项目的视图?
我在 http://jsfiddle.net/nair/bv3FG/19/ 来演示我遇到的问题。我有一个人的集合,我将它们显示在表格中,并提供编辑每一行的选项。数据在加载时正确显示。但是当我去编辑模型时我面临两个问题;
- 我需要确保当我编辑一行时,我希望编辑模板 显示但不显示表格。我正在使用可见性来切换可见性。 这似乎确实有效。
- 当我按编辑时,表单显示要编辑,但我选择的项目数据 没有显示,即使在我传递的模板中, 所选项目作为数据。
谢谢,
I have created a sample jsfiddle at http://jsfiddle.net/nair/bv3FG/19/ to demonstrate the problem I am running into. I have a collection of person, I show them in a table, with an option to edit each row. The data display properly on load. But when I go to edit model I face two problems;
- I need to make sure when I edit a row, I want the edit template to
show but not the table. I am using visibility to toggle visibility.
This does seem to work. - When I press edit, the form shows to edit but my selected item data
does not show, even though in the template I am passing, the
selected item as the data.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于,尝试立即绑定所选项目时会出错,该项目是一个空数组。
更好的策略可能是使用
with
与selecteditem
绑定来控制该部分的上下文。如果selecteditem
为空,则该区域将不会被渲染。所以,它看起来像: http://jsfiddle.net/rniemeyer/ACE2d/
然后,你不需要在表单上使用可见性,因为
with
绑定会处理它。保存后,您可以将该项目标记为空,该区域将不再可见/绑定。The issue is that it errors out trying to bind immediately against your selecteditem which is an empty array.
A better strategy is probably to use the
with
binding againstselecteditem
to control the context of that section. Ifselecteditem
is empty, then that area will not be rendered.So, it would look like: http://jsfiddle.net/rniemeyer/ACE2d/
Then, you don't need to use visibility on your form, as the
with
binding will take care of it. After saving, you can mark the item as null and the area will no longer be visible/bound.