Grails中有一个域类的依赖下拉列表 [英] Dependent drop-downs with one domain class in Grails
问题描述
所以,我有一个域类,它有很多字段。在GSP中有五个下拉列表准备好,数据正在任何下拉列表的onChange上被过滤,但是有一个问题。对于在下拉菜单中选择的几组组合,我们在数据库中没有数据,这些组合不是固定的。
So, I have one domain class and it has many fields.I have five drop-downs in GSP ready and the data is getting filtered on the onChange of any of the drop-downs properly but there is an issue. For a few set of combinations selected in the drop-downs, we don't have data in the database and these combinations are not fixed.
所以,我在想,有可能在第一个下拉菜单(或任何下拉菜单)进行选择后,剩下的下拉菜单-downs取决于第一个选择的值(意味着其他下拉列表仅填充数据库中数据可用的值),并且后续选项也应遵循。
So, I was thinking is it possible that once a selection is made on the first drop-down(or on any of the drop-downs) the rest of the drop-downs take up values depending on the first selection(meaning the other drop-downs be populated with only those values for which data is available in the database) and this should follow for the subsequent selections too.
此外,我看到了依赖下拉列表的例子,但是我看到的所有示例都有多个下拉列表,但是每个下拉列表都会从不同的域获取值课程
Also, I have seen examples of dependent drop-downs but all the examples that I see have multiple drop-downs but each of these drop-downs get values from different domain classes
只需一个域类,甚至可以这样做?
Is it even possible to do this with just one domain class?
这是我的索引。 gsp看起来像:
This is how my index.gsp looks like:
<html>
<head>
<g:javascript src="jquery-1.10.2.js"/>
<g:javascript src="prototype.js"/>
</head>
<body>
<form>
<g:select from="['DESKTOP/LAPTOP','SMARTPHONES','OTHERS','TABLETS']" name="device" id ="device"
onchange="${remoteFunction(
controller:'Pgtyp',
action:'ajaxGetMv',
// params:'\'mv=\' + escape(this.value)',
params:'\'device=\'+this.value+\'&mv=\'+$(\'mv\').value+\'&browser=\'+$(\'browser\').value+\'&pagetype=\'+$(\'pagetype\').value+\'&platform=\'+$(\'platform\').value' ,
onSuccess: 'printpgtyp(data)')}"
></g:select>
<g:select from="['CORESITE','MWEB']" name="platform" id ="platform"
onchange="${remoteFunction(
controller:'Pgtyp',
action:'ajaxGetMv',
// params:'\'mv=\' + escape(this.value)',
params:'\'platform=\'+this.value+\'&mv=\'+$(\'mv\').value+\'&browser=\'+$(\'browser\').value+\'&pagetype=\'+$(\'pagetype\').value' ,
onSuccess: 'printpgtyp(data)')}"
></g:select>
<g:select from="['CHECKOUT','HOMEPAGE','OTHERS', 'DEPARTMENT', 'PRODUCT','SEARCH', '(All)','SHELF']" name="pagetype" id ="pagetype"
onchange="${remoteFunction(
controller:'Pgtyp',
action:'ajaxGetMv',
// params:'\'mv=\' + escape(this.value)',
params:'\'pagetype=\'+this.value+\'&mv=\'+$(\'mv\').value+\'&browser=\'+$(\'browser\').value+\'&platform=\'+$(\'platform\').value' ,
onSuccess: 'printpgtyp(data)')}"
></g:select>
<g:select from="['INTERNET EXPLORER','GOGGLE CHROME','SAFARI', 'MOZILLA', 'OTHERS']" name="browser" id ="browser"
onchange="${remoteFunction(
controller:'Pgtyp',
action:'ajaxGetMv',
// params:'\'mv=\' + escape(this.value)',
params:'\'browser=\'+this.value+\'&mv=\'+$(\'mv\').value+\'&pagetype=\'+$(\'pagetype\').value+\'&platform=\'+$(\'platform\').value' ,
onSuccess: 'printpgtyp(data)')}"
></g:select>
<g:select from="['AFFILIATES', 'CSE','DISPLAYADS','EMAIL','MOBILEWEB','OTHERS','ORGANIC','SEO', 'SEM']" name="mv" id = "mv"
onchange="${remoteFunction(
controller:'Pgtyp',
action:'ajaxGetMv',
// params:'\'mv=\' + escape(this.value)',
params:'\'mv=\'+this.value+\'&browser=\'+$(\'browser\').value+\'&pagetype=\'+$(\'pagetype\').value+\'&platform=\'+$(\'platform\').value' ,
onSuccess: 'printpgtyp(data)')}"
></g:select>
</form>
<script>
function printpgtyp(data)
{
console.log(data)
}
</script>
</body>
</html>
推荐答案
为了回答你的问题,你需要什么要做的是产生1 g:select元素与onChange调用本地java脚本而不是一个remoteFunction,因为你猜测它自己的remoteFunction将只是更新一个Div,你可以尝试使用半冒号并将多个远程函数传递给一个onchange ,不是我曾经试过的东西。
In order to answer your question, what you would need to do is produce 1 g:select element with onChange that calls local java script rather than a remoteFunction since as you guessed it a remoteFunction on its own will just update one Div, you could possibly try using semi colons and passing multiple remotefunctions to one onchange, not something I ever tried.
所以要回答你的问题,请参考:
So to answer your question please also refer to :
由于这可能是最明智的答案
Since this probably has the most sensible answer
您的gsp .. 。
<g:select from="['DESKTOP/LAPTOP','SMARTPHONES','OTHERS','TABLETS']" name="device" id ="device" onchange="myMultiUpdater(this.value);"></g:select>
<div id="selectbox1">
</div>
<div id="selectbox2">
</div>
<div id="selectbox3">
</div>
<g:javascript>
function myMultiUpdater(id) {
$.get('${createLink(controller:"Controller1", action: "action1")}?id='+id+'',function(e){
$('#selectbox1').hide().html(e).fadeIn('slow');
}
$.get('${createLink(controller:"Controller2", action: "action1")}?id='+id+'',function(e){
$('#selectbox2').hide().html(e).fadeIn('slow');
}
}
$.get('${createLink(controller:"Controller3", action: "action1")}?id='+id+'',function(e){
$('#selectbox3').hide().html(e).fadeIn('slow');
}
}
<g:javascript>
您的控制器
controller1 {
def action1() {
def s=params.id
String domclass1= (s.substring(0,1).toUpperCase())
String domclass2=s.substring(1,s.length())
String domclass=domclass1+domclass2.toLowerCase()
Class domainClass = grailsApplication?.domainClasses.find { it.clazz.simpleName ==domclass+'Cities' }?.clazz
def cities=loadCities(domainClass)
render(template: 'cities', model: [cities: cities])
}
}
将扩展对象,而不是使用getJSON获取并传递特定的值 - 参考插件上的selectJs.gsp上面的注释。在这些选择框中,您还可以加载默认选择框,或者离开em,因为它们没有显示,直到进行选择。该方法加载整个内容 - 插件专注于提取元素并显示给定表/ domainClass
You simply would extend the objects or instead of using get use getJSON and pass specific values back - refer to the above comment on selectJs.gsp from the plugin. Inside those select boxes you could also load a default select box or leave em as they are nothing showing until a selection is made. This method loads in entire content - the plugin focuses on extracting elements and showing relevant defined elements of a given table/domainClass
的相关定义元素,并使用 g:javascript
and withing a g:javascript tag you could play around with
<g:remoteFunction controller="controller3" action="action1" onComplete="'selectbox3'" params= "\'id=\'+ id"/>
而不是
$.get('${createLink(controller:"Controller3", action: "action1")}?id='+id+'',function(e){
$('#selectbox3').hide().html(e).fadeIn('slow');
}
由于他们会做同样的事情,顶部看起来更加简洁,更多的gsp在javascript内部查找。您的选择。
Since they will do the same the top looks tidier and more gsp looking within javascript. Your choice.
这篇关于Grails中有一个域类的依赖下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!