Saturday, July 23, 2011

Cascading select (drop-down) boxes in Grails

${remoteFunction(controller:'processParamAssign',action:'workCenterList',update:'workCenterCode',params:params)};
${remoteFunction(controller:'processParamAssign',action:'mfgAreaList',update:'mfgAreaLocDiv',params:params)};
${remoteFunction(controller:'processParamAssign',action:'operList',update:'operDiv',params:params)};


   def workCenterList={
        groovy.sql.Sql sql = new groovy.sql.Sql(dataSource)

        def tableNames = []
        sql.eachRow("select distinct(work_center_code) from wc_mfg_area_oper where work_center_code is not null union select 'ALL' from dual order by 1",{ row ->
                //println "Found " << row.work_center_code
                tableNames << row.work_center_code
            });
        if(!params.workCenterCode){
            render g.select(from:tableNames, name:"workCenterCode", onChange:"${remoteFunction(controller:'processParamAssign',action:'mfgAreaList',update:'mfgAreaLocDiv', params:'\'workCenterCode=\' + this.value')};${remoteFunction(controller:'processParamAssign',action:'operList',update:'operDiv', params:'\'mfgAreaLoc=\' + mfgAreaLoc.value')};")
        }else{
            render g.select(from:tableNames, name:"workCenterCode", value:params.workCenterCode , onChange:"${remoteFunction(controller:'processParamAssign',action:'mfgAreaList',update:'mfgAreaLocDiv', params:'\'workCenterCode=\' + this.value')};")
        }
    }

    def mfgAreaList={
        groovy.sql.Sql sql = new groovy.sql.Sql(dataSource)
        def tableNames = []
        String mfg = "";
        //println params
        if((params.workCenterCode=="ALL")||(!params.workCenterCode)){
            mfg="select distinct(mfg_area_loc) from wc_mfg_area_oper where mfg_area_loc is not null union select 'ALL' from dual order by 1";
        }else{
            mfg="select distinct(mfg_area_loc) from wc_mfg_area_oper where work_center_code = '${params.workCenterCode}' and mfg_area_loc is not null union select 'ALL' from dual order by 1";
        }
        //println mfg
        sql.eachRow(mfg,{ row ->tableNames << row.mfg_area_loc
            });
        if(!params.mfgAreaLoc){
            render g.select(from:tableNames, name:"mfgAreaLoc", onChange:"${remoteFunction(controller:'processParamAssign',action:'operList',update:'operDiv', params:'\'mfgAreaLoc=\' + this.value')};")
        }else{
            render g.select(from:tableNames, name:"mfgAreaLoc", value:params.mfgAreaLoc, onChange:"${remoteFunction(controller:'processParamAssign',action:'operList',update:'operDiv', params:'\'mfgAreaLoc=\' + this.value')};")
        }
    }

    def operList={
        groovy.sql.Sql sql = new groovy.sql.Sql(dataSource)
        def tableNames = []
        String oper = "";
        if(params.mfgAreaLoc=="ALL"){
            tableNames = tableNames+"ALL";
            oper="select distinct(oper) from wc_mfg_area_oper where oper is not null order by 1";
        }else{
            oper="select distinct(oper) from wc_mfg_area_oper where mfg_area_loc = '${params.mfgAreaLoc}' and mfg_area_loc is not null union select 'ALL' from dual order by 1";
        }
        sql.eachRow(oper,{ row ->
                //println "Found " << row.oper
                tableNames << row.oper
            });
        if(!params.oper){
            render g.select(from:tableNames, name:"oper")
        }else{
            render g.select(from:tableNames, name:"oper", value:params.oper)
        }
    }

No comments:

Post a Comment

Thank you for your feedback