Multiple <g:select> in one GSP and multiple params in HQL inside controller

放肆的年华 提交于 2019-12-24 04:44:30

问题


GSP

<html>
<head>
<g:javascript src="jquery-1.10.2.min.js"/>
<g:javascript src="prototype.js"/>
</head>
<body>
<form>
<g:select from="['AFFILIATES', 'SEO', 'SEM','DISPLAYADS']" name="mv"
           onchange="${remoteFunction(
            controller:'Pgtyp', 
            action:'ajaxGetMv', 
            params:'\'mv=\' + escape(this.value)',
            onSuccess: 'printpgtyp(data)')}"
></g:select>
</form>
<script>
function printpgtyp(data)
{
console.log(data);
}
</script>
</body>
</html>

CONTROLLER

package marchmock2
import grails.converters.*
import groovy.sql.Sql   


class PgtypController {



    def ajaxGetMv = {
        def pgtyp = Pgtyp.executeQuery("select p.visits,p.mv from Pgtyp p where p.mv = ?",[params.mv])
                //console.log(params.id)
        render pgtyp as JSON
    }

     def index() { 

}

} 

This worked fine.Now I want to add another of the following type:

<g:select from="['a', 'b', 'c','d']" name="mv"

And, the HQL inside the controller will change to something like this:

def pgtyp = Pgtyp.executeQuery("select p.visits,p.mv from Pgtyp p where p.mv = ? and p.br = ?",[params.mv],[params.br])

When either of the drop-downs change, I want the relevant output. Can this be done in Grails? I am still exploring and any suggestions will be appreciated

UPDATE This is what I tried inside my GSP:

<html>
<head>
<g:javascript src="jquery-1.10.2.min.js"/>
<g:javascript src="prototype.js"/>
</head>
<body>
    var a = document.getElemntByName(mv);
    <form>
<g:select from="['AFFILIATES', 'SEO', 'SEM','DISPLAYADS']" name="mv" class="mySelect"
           onchange="${remoteFunction(
            controller:'Pgtyp', 
            action:'ajaxGetMv', 
           // params:'\'mv=\' + escape(this.value)',
            params:'\'mv=\' + $(".mySelect").val()',
            onSuccess: 'printpgtyp(data)')}"
></g:select>
<g:select from="['SAFARI', 'MOZILLA', 'OTHERS']" name="br" class="mySelect"
           onchange="${remoteFunction(
            controller:'Pgtyp', 
            action:'ajaxGetMv', 
           // params:'\'mv=\' + escape(this.value)',
            params:'\'br=\' + $(".mySelect").val()',
            onSuccess: 'printpgtyp(data)')}"
></g:select>
</form>
<script>
function printpgtyp(data)
{
console.log(data);
}
</script>
</body>
</html>

And I changed my controller to this:

package marchmock2
import grails.converters.*
import groovy.sql.Sql   


class PgtypController {



    def ajaxGetMv = {
        def pgtyp = Pgtyp.executeQuery("select p.visits,p.mv from Pgtyp p where p.mv = ? and p.browser = ?",[params.mv],[params.br])
                //console.log(params.id)
        render pgtyp as JSON
    }

     def index() { 

}

} 

But, it threw an error.

Uncaught TypeError: Cannot read property 'val' of null 

回答1:


You can separate multiple parameters using &key=value. Think of URL parameters for a GET request.

Please use proper classes (mySelect1, mySelect2) for the select boxes, so you can access them easily using jQuery.

params:'\'mv=\' + $(\'.mySelect1\').find(\':selected\').text() + \'&br=\' + $(\'.mySelect2\').find(\':selected\').text()',



回答2:


This worked:

<g:select from="['DESKTOP/LAPTOP','SMARTPHONES','OTHERS','TABLETS']" name="device" id ="device"
           onchange="${remoteFunction(
           controller:'Pgtyp', 
           action:'ajaxGetMv',   
           params:'\'device=\'+this.value+\'&mv=\'+$(\'mv\').value+\'&browser=\'+$(\'browser\').value+\'&pagetype=\'+$(\'pagetype\').value+\'&platform=\'+$(\'platform\').value' ,
           onSuccess: 'printpgtyp(data)')}"
></g:select>


来源:https://stackoverflow.com/questions/24605495/multiple-gselect-in-one-gsp-and-multiple-params-in-hql-inside-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!