jqueryjsongrailsgrails-domain-classgrails-controller

Grails- combine data from more than one domain and return in controller


I have a controller where i need to fetch the data from more than one domain, combine return to the view. In GSP page am using jquery ajax to call this coltroller.

I created one wrapper class which wraps the data from multiple domain and returned as json. But the json was not proper.

Did anyone faced problem like this. please share any info related to this.

class XYZController {

class XYZData {
    public ArrayList<String> date;
    public ArrayList<String> Name;
}

def getXYZData() {
    ArrayList<String> dateList = XYZDomain.executeQuery("select distinct date from XYZDomain")
    log.info(dateList);

    ArrayList<String> nameList = ABCDomain.executeQuery("select distinct Name from ABCDomain")
    log.info(nameList);

    XYZData data = new XYZData();
    data.date = dateList;
    data.Name = nameList;

    log.info(data)

    render(data as JSON)

    }
}

Thank in advance.


Solution

  • Okey, try this:

    def getData(){
    
           def returnList = [] 
           List <String>list1 = XYZDomain.executeQuery("select distinct date from XYZDomain") 
           List <String>list2 = ABCDomain.executeQuery("select distinct Name from ABCDomain")
     
           //iterating both lists
           list1.each { lst ->           
               def map = [:]
               map.date = lst
               returnList << map
           }
     
           list2.each { lst -> 
               def map = [:]
               map.name = lst
               returnList << map
           } 
    
           render returnList as JSON 
    }
    

    That should solve your problem.