/**
* This service enables autoComplete using jQuery
* dynamically find the domain and return the results
* @author Alidad Soleimani
*/
import grails.converters.*
class AutoCompleteService {
def grailsApplication
def autocompleteAction (params) {
def domainClass = grailsApplication.getDomainClass(params.domain).clazz
def results = domainClass.createCriteria().list {
ilike params.searchField, params.term + '%'
maxResults(Integer.parseInt(params.max,10))
order(params.searchField, params.order)
}
if (results.size()< 5){
results = domainClass.createCriteria().list {
ilike params.searchField, "%${params.term}%"
maxResults(Integer.parseInt(params.max,10))
order(params.searchField, params.order)
}
}
results = results.collect { [label:it."${params.collectField}"] }.unique()
return results as JSON
}
}