使用 Knockoutjs 显示点击的项目
我正在尝试显示有关列表中所选项目的信息并突出显示所选行。这就是我所拥有的:
http://jsfiddle.net/blankasaurus/qUydu/6/
1)当我尝试设置 CurrentDisplayThing = item
时,它没有执行我想要的操作(即使用所选项目更新 Display div)。我还尝试了 $.extend(CurrentDisplayThing, item);
我是否必须单独设置每个属性或其他内容?我的实际项目中有大约 30 个。
2)我想弄清楚的另一件事是如何突出显示我刚刚使用 Knockout 以正确方式单击的行。
PS:对于我实际所做的事情,我使用映射插件,Things
和CurrentDisplayThing
都来自我的.NET模型我我假设我在 JS Fiddle 中拥有的镜像映射将设置它的方式?
I am trying to display information about the selected item in a list as well as highlight the row that is selected. This is what I have:
http://jsfiddle.net/blankasaurus/qUydu/6/
1) When I try to set the CurrentDisplayThing = item
it doesn't do what I want it to do (which is update the Display div with the selected item). I also tried $.extend(CurrentDisplayThing, item);
Am I going to have to set each property individually or something? I have about 30 of them on my real project.
2) The other thing i wanted to figure out is how to go about highlighting the row that I just clicked the proper way using Knockout.
PS: For what I am actually doing I am using the mapping plug-in and both Things
and CurrentDisplayThing
are coming from my .NET model I pass in. I am assuming what I have in JS Fiddle mirrors the way mapping would set this up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是解决这两个问题的小提琴。
http://jsfiddle.net/johnpapa/6FCEe/
1)选中的item属性实际上是一个observable (这是一个函数)。因此该值必须像这样传递到可观察函数中:
2)突出显示该行可以通过 Knockout 中的 css 绑定来完成。 css 绑定接受带有 css 类名称(例如:selected)和表达式(例如:isSelected)的对象。在下面的示例中,当 isSelected 为 true 时,将应用该类,否则将删除该类。
检查小提琴以获得完整的示例。
Here is a fiddle that solves both problems.
http://jsfiddle.net/johnpapa/6FCEe/
1) The selected item property is actually an observable (which is a function). So the value must be passed into the observable function like this:
2) Highlighting the row can be done through the css binding in Knockout. The css binding accepts an object with the name of the css class (ex: selected) and an expression (ex: isSelected). In the example below, when isSelected is true, the class will be applied, otherwise it is removed.
Check the fiddle out for the full example.
你不能只使用像这样的代码来分配Knockout可观察量
,而且你也不能
$.extend
它们,因为最终它也会进行分配。相反,您必须使用函数语法:
同样,您在访问时必须使用函数语法:
工作示例: http:// /jsfiddle.net/HUjau/1/
You can't just assign Knockout observables using code like
And you can't
$.extend
them either, because in the end that does assignments too.Instead you have to use the function syntax:
Similarly you have to use the function syntax when accessing:
Working example: http://jsfiddle.net/HUjau/1/