KnockoutJS 像 Windows 资源管理器一样选择嵌套列表
I have searched High and low for an answer to the following.
I have a list of folders and I want to be able to click on the folder and show its children.
This JSfiddle will explain it better, its nearly complete but it expands all folders
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生这种情况是因为您正在为每个文件夹循环“selectedFolder”中的所有“链接”,而不仅仅是选定的文件夹!
这也意味着,如果您选择“Folder1”,则链接位于文件夹“link1”和“link2”上;如果您选择“Folder2”,则链接位于“link3”和“link4”上!
您需要在显示链接之前检查是否选择了当前文件夹,例如:
http://jsfiddle.net/ saurus/kJYsm/
请注意我如何
使用 an
更改你的,并且我还添加了另一个 div 检查“$parent.selectedFolder”是否未定义,否则第一次运行时会发生错误。您可以通过其他方式进行操作:您可以简单地将“selectedFolder”设置为默认的虚拟文件夹,或者如果您不需要额外的标记,则可以使用模板注释。
另请注意,即使删除“with:”一切都会正常,因为现在“$data”是当前文件夹,而这正是我们所需要的。
希望这有帮助!
this happens because you are cycling all 'links' in 'selectedFolder' for every folder, not only the selected one!
this also means that the links are on BOTH folders "link1" and "link2", if you select "Folder1" OR "link3" and "link4" if you select "Folder2"!
you need to check that the current folder is selected before showing the links, something like:
http://jsfiddle.net/saurus/kJYsm/
note how I changed your
with an
and I have also added another div checking that "$parent.selectedFolder" is not undefined, without it a error occurs on first run. you can do on other ways: you can simply set 'selectedFolder' to a default dummy folder, or you can use templating comments if you don't want the extra markup.
Note also that even removing the "with:" everything works, because now "$data" is the current folder, and this is exactly what we need.
hope this helps!