Grails 控制查询范围的scaffold

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


对应的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]asObject[]))
  6. defpropNames=[]
  7. props.name.each{propNames.add"'"+it+"'"}
  8. %>
  9. staticallowedProperties=<%=propNames%>
  10. deflist={
  11. params.max=Math.min(params.max?params.int('max'):10,100)
  12. def${propertyName}List,${propertyName}Total
  13. if(params?.searchValue&&allowedProperties.contains(params?.searchKey)){
  14. defc=${className}.createCriteria()
  15. ${propertyName}List=c.list(max:params.max,offset:params.offset?:0){
  16. ilike(params.searchKey,"%"+params.searchValue+"%")
  17. eq("deleted",false)
  18. }
  19. ${propertyName}Total=${propertyName}List.totalCount
  20. }else{
  21. ${propertyName}List=${className}.findAllByDeleted(false,params)
  22. ${propertyName}Total=${className}.countByDeleted(false)
  23. }
  24. [${propertyName}List:${propertyName}List,${propertyName}Total:${propertyName}Total]
  25. }


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


对应的scaffold-list部分为:
Java代码 收藏代码
  1. <g:selectname="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. ....
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值