Grails 控制查询范围的scaffold

CustomerController里由scaffold生成
Java代码   收藏代码
  1. static allowedProperties = ['name''address''telephone''postcode']  
  2.   
  3. def list = {  
  4.     params.max = Math.min(params.max ? params.int('max') : 10100)  
  5.     def customerInstanceList, customerInstanceTotal  
  6.     if(params?.searchValue && allowedProperties.contains(params?.searchKey)) {  
  7.         def c = Customer.createCriteria()  
  8.         customerInstanceList = c.list(max: params.max, offset: params.offset?:0) {  
  9.             ilike(params.searchKey, "%" + params.searchValue + "%")  
  10.             eq("deleted"false)  
  11.         }  
  12.         customerInstanceTotal = customerInstanceList.totalCount  
  13.     } else {  
  14.         customerInstanceList = Customer.findAllByDeleted(false, params)  
  15.         customerInstanceTotal = Customer.countByDeleted(false)  
  16.     }  
  17.     [customerInstanceList: customerInstanceList, customerInstanceTotal: customerInstanceTotal]  
  18. }  


对应的controller-scaffold部分为:
Java代码   收藏代码
  1. <%  
  2.     excludedProps = Event.allEvents.toList() << 'id' << 'version' << 'dateCreated' << 'lastUpdated' << 'deleted' << 'itemOrder' << 'memo'  
  3.     allowedNames = domainClass.persistentProperties*.name  
  4.     props = domainClass.properties.findAll { allowedNames.contains(it.name) && !excludedProps.contains(it.name) && !Collection.isAssignableFrom(it.type) }  
  5.     Collections.sort(props, comparator.constructors[0].newInstance([domainClass] as Object[]))  
  6.     def propNames = []  
  7.     props.name.each {propNames.add "'"+it+"'"}  
  8. %>  
  9. static allowedProperties = <%=propNames %>  
  10.   
  11. def list = {  
  12.     params.max = Math.min(params.max ? params.int('max') : 10100)  
  13.     def ${propertyName}List, ${propertyName}Total  
  14.     if(params?.searchValue && allowedProperties.contains(params?.searchKey)) {  
  15.         def c = ${className}.createCriteria()  
  16.         ${propertyName}List = c.list(max: params.max, offset: params.offset?:0) {  
  17.             ilike(params.searchKey, "%" + params.searchValue + "%")  
  18.             eq("deleted"false)  
  19.         }  
  20.         ${propertyName}Total = ${propertyName}List.totalCount  
  21.     } else {  
  22.         ${propertyName}List = ${className}.findAllByDeleted(false, params)  
  23.         ${propertyName}Total = ${className}.countByDeleted(false)  
  24.     }  
  25.     [${propertyName}List: ${propertyName}List, ${propertyName}Total: ${propertyName}Total]  
  26. }  


list页面由scaffold生成
Java代码   收藏代码
  1. <g:form action="list" method="post" useToken="true">  
  2.     <g:select name="searchKey" from="${CustomerController.allowedProperties.toList() }" value="${searchKey }" optionValue="${{message(code:'customer.'+it+'.label',default:message(code:'domainProperty.'+it+'.label', default:it))}}" />  
  3.     <input type="text" name="searchValue" value="${params?.searchValue }" />  
  4. </g:form>  


对应的scaffold-list部分为:
Java代码   收藏代码
  1. <g:select name="searchKey" from="\${${domainClass.fullName}Controller.allowedProperties.toList() }" value="\${searchKey }" optionValue="\${{message(code:'${domainClass.propertyName}.'+it+'.label',default:message(code:'domainProperty.'+it+'.label', default:it))}}" />  


i18n追加
Java代码   收藏代码
  1. domainProperty.name.label=姓名  
  2. .... 

你可能感兴趣的:(Grails 控制查询范围的scaffold)