knockout js - 如何在下拉列表上进行条件绑定
我有一个由各种 Mvc 操作填充的 Knockout 视图模型。
从第一个下拉列表中选择类别(例如水果、肉类、饮料等)。
第二个下拉列表会根据第一个选项自动填充。但是,可能有 2 个水果匹配项(例如苹果、橙子),2 个肉类匹配项(例如牛肉、羊肉)以及许多饮料选择(数百个)。
我的页面当前根据所选类别显示搜索框。
我想知道如何自动绑定 Fruit & 的第二个下拉菜单肉,或者等待并根据搜索查询的结果进行绑定。
抱歉,这很模糊 - 焦躁的客户!对于 KnockoutJ 来说非常陌生。
提前致谢。
I've got a Knockout viewmodel populated from a variety of Mvc actions.
A Category is chosen from the first dropdown (say Fruit, Meat, Drinks etc).
A second dropdown is automatically populated from the first choice. However, there may be 2 matches for fruit (say Apples, Oranges), 2 for meat (say Beef, Lamb) and many choices for drink (several hundred).
My page currently displays a search box depending on the Category chosen.
I was wondering how to automatically bind the second dropdown for Fruit & Meat, or wait and do the bind from the results of the search query.
Sorry this is vague - twitchy client! Very new to KnockoutJs.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确,您可以在视图模型中创建一个方法,将其绑定到下拉列表的更改事件。
示例方法:
您的下拉菜单可以绑定到这样的方法:
如下所述:事件绑定,
我假设这意味着当您调用该方法时
currentItem
将成为所选项目。我知道在我的示例代码中我编写了
item.toUpperCase()
但这只是假设该项目作为字符串传递。显然,这种语法必须根据您声明和填充下拉列表的方式进行更改,但本质上您仍然应该能够在视图模型中编写一个方法,然后可以绑定到更改事件,或者您喜欢的任何其他事件。If I understood you right, you could create a method in your viewmodel which you bind to the change event of the dropdown.
Example method:
Your Dropdown can bind then to the method like this:
As stated here: event-binding,
I'm assuming this means the
currentItem
will be the selected item when you are calling the method.I know in my sample code I wrote
item.toUpperCase()
but that is just assuming the item is passed as a string. This syntax obviously has to change depending on how you have declared and populated your dropdown but in essence you still should be able to write a method in your viewmodel you can bind then to the change event,..or any other event you like.