简体   繁体   中英

Dependent drop-downs with one domain class in Grails

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.

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?

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>

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 :

I want my selects dropdowns to auto populate with Ajax in Grails website

Since this probably has the most sensible answer

your 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>

Your controller

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])
        }

}

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

and withing a g:javascript tag you could play around with

<g:remoteFunction  controller="controller3"  action="action1" onComplete="'selectbox3'"  params= "\'id=\'+ id"/>

Instead of

 $.get('${createLink(controller:"Controller3", action: "action1")}?id='+id+'',function(e){
           $('#selectbox3').hide().html(e).fadeIn('slow');
          }

Since they will do the same the top looks tidier and more gsp looking within javascript. Your choice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM