Parent and Child mapping list check in groovy and grails

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
Report Content as Inappropriate

Parent and Child mapping list check in groovy and grails

My objective is to ensure parent child list check using groovy and grails

Step1: write ajax code in your gsp view page

        <meta name="layout" content="main">
        <g:set var="entityName" value="${message(code: 'WFLayer.label', default: 'WFLayer')}" />
        <g:javascript library="prototype"/> 
            document.observe('dom:loaded', function() {
                $("master").observe("change", respondToSelect);

            function respondToSelect(event)
               new Ajax.Updater("sub", "/ADHRPAY/WFLayer/getDate",
                                method:'get', parameters:
                                selectedValue : $F("master")
                                }, onSuccess: function(transport)
        <title><g:message code="default.create.label" args="[entityName]" /></title>           

in above code /ADHRPAY/WFLayer/getDate. is called /ProjectName/ControllerName/method

Step2: write method code in your groovy class

def getDate() {
               def result
        def sqlConnect = new Sql(dataSource)
            result = sqlConnect.rows("select department_name as name from departments")
        else if(params.selectedValue=='Section'){
            result = sqlConnect.rows("select section_name as name from sections")
        else if(params.selectedValue == "Employee"){
            result = sqlConnect.rows("select concat(first_name,' ',last_name) as name from employees")

Step3: write template code in your template gsp view

<g:select name="sub" value="" from="${selectResult.name}"></g:select>

Step4: Finally change your gsp page from which you are sending value to your controller using Ajax support.

<div class="fieldcontain ${hasErrors(bean: WFLayerInstance, field: 'layerType', 'error')} ">
    <label for="layerType">
        <g:message code="WFLayer.layerType.label" default="Layer Type" />
    <%-- <g:select id="master" name="master" from="${WFLayerInstance.constraints.layerType.inList}" value="${WFLayerInstance?.layerType}" valueMessagePrefix="WFLayer.layerType" noSelection="['': '']"/> --%>
    <g:select id="master" from="${WFLayerInstance.constraints.layerType.inList}" name="master" />

<div class="fieldcontain ${hasErrors(bean: WFLayerInstance, field: 'layerId', 'error')} required">
    <label for="layerId">
        <g:message code="WFLayer.layerId.label" default="Layer Name" />
    <g:select id="sub" name="sub" from="" value="${WFLayerInstance.layerId}"/>

Best way to keep your all GSP file in same folder.
Step5: Now run your application and then you will get below output(once your select layer type then layer name will be displayed).