Lift 框架中的动态绑定
我是 Lift 的新手,我有一个关于在 Lift 中使用绑定、Ajax 的问题。
我想使用 Ajax 以动态方式创建三个下拉菜单。我以“地址”为例来描述我想要实现的目标。首先,我只需显示“国家/地区”菜单,默认设置为“无”。此时用户可以根据需要选择提交,并且该地址将被视为默认地址。如果没有,她可以提供准确的地址。一旦她选择了国家/地区,就会显示“州”菜单,一旦她选择了“州”,就会显示“县”菜单。
在 lift 演示示例的帮助下,我尝试创建静态菜单,如下所示。我在 .html 文件和 scala 代码中创建了三个片段
,我绑定如下:
bind("select", xhtml,
"system" -> select(Address.countries.map(s => (s,s)),
Full(country), s => country = s, "onchange" -> ajaxCall(JE.JsRaw("this.value"),s => After(200, replaceCounty(s))).toJsCmd),
"state" -> stateChoice(country) % ("id" -> "state_select"),
"county" -> countyChoice(state) % ("id" -> "county_select"),
"submit" -> submit(?("Go!"),()=>Log.info("Country: "+country+" State: "+state + " County: "+ county)
对应的replaceCounty、stateChoice、countyChoice都是在我的类中定义的。但是,当选择国家/地区时,通过 Ajax 调用仅刷新州,而不刷新县。
Q1)有没有办法根据国家/地区菜单刷新两个菜单?
Q2)如何像我之前解释的那样动态创建菜单?
I am newbie to Lift and I have a question on using bind, Ajax in Lift.
I want to create three dropdown menus using Ajax in a dynamic fashion. I use "Address" as an example to describe what I am trying to achieve. At fist, I only have to display "Country" menu with default set to "None". The user at this point can choose to submit if she wishes to and the address is taken as default. If not, she can provide the precise address. Once she selects the country, the "State" menu should get displayed, and once she selects "State", the "County" menu should be displayed.
With the help of lift demo examples, I tried to create static menus as follow. I created three snippets <select:country/>, <select:state/>, <select:county/>
in my .html file and in the scala code, I bind as follows:
bind("select", xhtml,
"system" -> select(Address.countries.map(s => (s,s)),
Full(country), s => country = s, "onchange" -> ajaxCall(JE.JsRaw("this.value"),s => After(200, replaceCounty(s))).toJsCmd),
"state" -> stateChoice(country) % ("id" -> "state_select"),
"county" -> countyChoice(state) % ("id" -> "county_select"),
"submit" -> submit(?("Go!"),()=>Log.info("Country: "+country+" State: "+state + " County: "+ county)
The corresponding replaceCounty, stateChoice, countyChoice are all defined in my class. However, when the country is selected, only the state is refreshed through Ajax call and not the county.
Q1) Is there a way to refresh both the menus based on the country menu?
Q2) How to create the menu's dynamically as I explained earlier?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个出色的示例代码可以实现此目的:
http://demo.liftweb.net/ajax- form
如果您想通过 AJAX 更新来更新多个下拉列表,您将需要返回类似以下内容的内容:
There is an excellent example code that does just this available at:
http://demo.liftweb.net/ajax-form
If you want to update multiple dropdowns as a result of an AJAX update, you'll want to return something like:
使用
SHtml.ajaxSelect
作为第一个选择,并使用静态元素作为另外两个选择。当第一个选择更改时,您将返回 javascript 以使用另一个 SHtml.ajaxSelect 调用的结果填充下一个选择。在 #ajaxSelect 的回调中,您可能希望保存已选择的值,但这是我采取的一般方法。
Use
SHtml.ajaxSelect
for your first select, and static elements for the other two. When the first select changes, you'll return javascript to populate the next select with the result of another SHtml.ajaxSelect call.In the callbacks for #ajaxSelect you'll probably want to save the values of whatever has been selected, but this is the general approach I'd take.