- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在Grails中,我需要制作一个 Controller 方法,该方法将填充“州和县”下拉列表表单域,以便在选择一个州时仅将该州的县填充到“县”下拉列表中。
一位同事告诉我,这是Grails中的异步调用,但是我是Grails中的新手,我真的不知道那是什么或如何开始。任何帮助将不胜感激。
这是我的代码段:
当前使用Grails 2.43。我有两个域类(州和县),以及两个针对州和县的“选择”下拉列表。
表单元素:
<g:select name="locationState" class="form-control" from="${....State.list().sort{it.orderNumber}}">
<g:select name="locationCounty" class="form-control" from="${...State.FindByName(it.orderNumber).counties}">
class State {
static hasMany = [county:County]
String name
String value
int orderNumber = 0
static constraints = {
name nullable:false, maxSize:50, blank:false
value nullable:false, maxSize:100, blank:false
}
String toString(){
"$value"
}
static mapping = {
table 'state'
cache: 'read-write'
columns{
id column:'id'
name column:'name'
value column:'value'
orderNumber column:'order_number'
}
id generator: 'assigned'
}
}
class County {
State state
String county
static constraints = {
state nullable:false
county nullable:false, maxSize:100, blank:false
}
String toString(){
"${state.name} - $county"
}
static mapping = {
table 'county'
cache: 'read-write'
columns{
id column:'id'
county column:'county'
state column:'state_id'
}
id generator: 'assigned'
}
}
最佳答案
注释中链接的异步指南适用于进行程序化异步调用。例如,如果您有两个计算量大的方法调用(或需要网络I / O的方法调用),则可以使用线程(大致)并行运行它们。 Grails提供了许多不同的帮助程序,以使这种异步编程变得非常容易。
但是,这不太可能是您执行GORM查询所需要的。您要填充第二个选择框。您可以通过以下两种方法来完成此操作:选择状态后重新加载页面,或使用JavaScript填充框。我假设您想做后者。 Grails确实提供了工具(例如<g:remoteFunction />标记)来处理此问题,而无需编写您自己的JavaScript,但是Grails AJAX库已被弃用,因此不建议使用。
相反,您应该只编写自己的JavaScript。我将向您展示一种使用jQuery的技术:
在您的 View 中,初始化两个选择,但第二个应初始化为空。我们还将为它们提供ID,以使其更易于从jQuery中进行选择:
<g:select name="locationState"
class="form-control"
from="${....State.list().sort{it.orderNumber}}"
id="location-state" />
<g:select name="locationCounty"
class="form-control"
from="${[]}"
id="location-county" />
def loadCountiesByState() {
def state = params.state
def counties = State.findByValue(state).counties
render g.select(name: 'locationCounty', class: 'form-control',
from: counties, id: 'location-county')
}
/app-name/controller-name/loadCountiesByState?state=CA
即可测试此部分。我不知道您的数据是如何建模的,因此您可能需要更改
State.findByValue(state)
部分以适合您的需求。
<script type="text/javascript">
$(function() {
$('#location-sate').change(function() {
var state = $(this).val();
$.ajax({
url: '/app-name/controller-name/loadCountiesByState',
date: { state: state },
success: function(data) {
$('#location-county').replaceWith(data);
}
});
});
});
</script>
关于grails - 异步调用Controller-适用于州和县,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27723149/
我有国家、地区、县、城镇数据,目前我正在决定 2 种模式设计(如果有更好的,请告诉我)。 我首先想到的 国家 身份证 姓名 地区 身份证 国家/地区 姓名 县 身份证 地区编号 姓名 城镇 身份证 县
我有两个数据集 df_state 和 df_city。 df_state 有一个数值的总和(net_value_x)。 df_city 在城市级别对数值 (value_x) 进行了分割 这只是德克萨斯
我有一个美国邮政编码及其对应的州、市和县的数据库。它以平面文件的形式提供,我正在尝试规范化数据并准确找出哪些实体依赖于哪些实体。 我遇到的一个问题是,有些城市似乎存在于多个县。我的印象是,在美国,存在
他们使用什么流程或 API 在他们的网站上提供此功能?如果它太复杂而无法解释,请您朝着正确的方向努力,以便也能够提供此功能。谢谢。 最佳答案 研究地理编码、地理标记和 GIS http://en.wi
R新手在这里。 如果我第一次使用 map('state') , 那么我如何使用 map('county', ylim=..., xlim=...)但像这样抵消它: 现在,我正在使用 imagemagi
以下查询适用于一种分类法 SELECT * FROM bb_posts AS p LEFT JOIN bb_term_relationships AS r ON (p.ID = r.object_id
利用 Mike Bostocks 美国各县区 block :https://bl.ocks.org/mbostock/4122298 。 目标是创建一个鼠标移动函数,在事件期间鼠标移动将突出显示所有选
在我的 MVC 应用程序中,我想显示用户访问网站的国家/地区、地区和城市。我怎样才能获取它们? 最佳答案 有几种方法。一种是要求用户与远程服务器共享他的位置。 https://developer.mo
我是一名优秀的程序员,十分优秀!