Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * This service enables autoComplete using jQuery
  3.  * dynamically find the domain and return the results
  4.  * @author Alidad Soleimani
  5.  */
  6.  
  7. import grails.converters.*
  8.  
  9. class AutoCompleteService {
  10.     def grailsApplication
  11.     def autocompleteAction (params) {
  12.         def domainClass = grailsApplication.getDomainClass(params.domain).clazz
  13.         def results = domainClass.createCriteria().list {
  14.             ilike params.searchField, params.term + '%'
  15.             maxResults(Integer.parseInt(params.max,10))
  16.             order(params.searchField, params.order)
  17.         }
  18.         if (results.size()< 5){
  19.             results = domainClass.createCriteria().list {
  20.                 ilike params.searchField, "%${params.term}%"
  21.                 maxResults(Integer.parseInt(params.max,10))
  22.                 order(params.searchField, params.order)
  23.             }
  24.         }
  25.         results = results.collect { [label:it."${params.collectField}"] }.unique()
  26.          return results as JSON
  27.     }
  28. }