Grails 控制查询范围的scaffold
2013-11-16 17:41
204 查看
CustomerController里由scaffold生成
Java代码
static allowedProperties = ['name', 'address', 'telephone', 'postcode']
def list = {
params.max = Math.min(params.max ? params.int('max') : 10, 100)
def customerInstanceList, customerInstanceTotal
if(params?.searchValue && allowedProperties.contains(params?.searchKey)) {
def c = Customer.createCriteria()
customerInstanceList = c.list(max: params.max, offset: params.offset?:0) {
ilike(params.searchKey, "%" + params.searchValue + "%")
eq("deleted", false)
}
customerInstanceTotal = customerInstanceList.totalCount
} else {
customerInstanceList = Customer.findAllByDeleted(false, params)
customerInstanceTotal = Customer.countByDeleted(false)
}
[customerInstanceList: customerInstanceList, customerInstanceTotal: customerInstanceTotal]
}
对应的controller-scaffold部分为:
Java代码
<%
excludedProps = Event.allEvents.toList() << 'id' << 'version' << 'dateCreated' << 'lastUpdated' << 'deleted' << 'itemOrder' << 'memo'
allowedNames = domainClass.persistentProperties*.name
props = domainClass.properties.findAll { allowedNames.contains(it.name) && !excludedProps.contains(it.name) && !Collection.isAssignableFrom(it.type) }
Collections.sort(props, comparator.constructors[0].newInstance([domainClass] as Object[]))
def propNames = []
props.name.each {propNames.add "'"+it+"'"}
%>
static allowedProperties = <%=propNames %>
def list = {
params.max = Math.min(params.max ? params.int('max') : 10, 100)
def ${propertyName}List, ${propertyName}Total
if(params?.searchValue && allowedProperties.contains(params?.searchKey)) {
def c = ${className}.createCriteria()
${propertyName}List = c.list(max: params.max, offset: params.offset?:0) {
ilike(params.searchKey, "%" + params.searchValue + "%")
eq("deleted", false)
}
${propertyName}Total = ${propertyName}List.totalCount
} else {
${propertyName}List = ${className}.findAllByDeleted(false, params)
${propertyName}Total = ${className}.countByDeleted(false)
}
[${propertyName}List: ${propertyName}List, ${propertyName}Total: ${propertyName}Total]
}
list页面由scaffold生成
Java代码
<g:form action="list" method="post" useToken="true">
<g:select name="searchKey" from="${CustomerController.allowedProperties.toList() }" value="${searchKey }" optionValue="${{message(code:'customer.'+it+'.label',default:message(code:'domainProperty.'+it+'.label', default:it))}}" />
<input type="text" name="searchValue" value="${params?.searchValue }" />
</g:form>
对应的scaffold-list部分为:
Java代码
<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代码
domainProperty.name.label=姓名
....
Java代码
static allowedProperties = ['name', 'address', 'telephone', 'postcode']
def list = {
params.max = Math.min(params.max ? params.int('max') : 10, 100)
def customerInstanceList, customerInstanceTotal
if(params?.searchValue && allowedProperties.contains(params?.searchKey)) {
def c = Customer.createCriteria()
customerInstanceList = c.list(max: params.max, offset: params.offset?:0) {
ilike(params.searchKey, "%" + params.searchValue + "%")
eq("deleted", false)
}
customerInstanceTotal = customerInstanceList.totalCount
} else {
customerInstanceList = Customer.findAllByDeleted(false, params)
customerInstanceTotal = Customer.countByDeleted(false)
}
[customerInstanceList: customerInstanceList, customerInstanceTotal: customerInstanceTotal]
}
对应的controller-scaffold部分为:
Java代码
<%
excludedProps = Event.allEvents.toList() << 'id' << 'version' << 'dateCreated' << 'lastUpdated' << 'deleted' << 'itemOrder' << 'memo'
allowedNames = domainClass.persistentProperties*.name
props = domainClass.properties.findAll { allowedNames.contains(it.name) && !excludedProps.contains(it.name) && !Collection.isAssignableFrom(it.type) }
Collections.sort(props, comparator.constructors[0].newInstance([domainClass] as Object[]))
def propNames = []
props.name.each {propNames.add "'"+it+"'"}
%>
static allowedProperties = <%=propNames %>
def list = {
params.max = Math.min(params.max ? params.int('max') : 10, 100)
def ${propertyName}List, ${propertyName}Total
if(params?.searchValue && allowedProperties.contains(params?.searchKey)) {
def c = ${className}.createCriteria()
${propertyName}List = c.list(max: params.max, offset: params.offset?:0) {
ilike(params.searchKey, "%" + params.searchValue + "%")
eq("deleted", false)
}
${propertyName}Total = ${propertyName}List.totalCount
} else {
${propertyName}List = ${className}.findAllByDeleted(false, params)
${propertyName}Total = ${className}.countByDeleted(false)
}
[${propertyName}List: ${propertyName}List, ${propertyName}Total: ${propertyName}Total]
}
list页面由scaffold生成
Java代码
<g:form action="list" method="post" useToken="true">
<g:select name="searchKey" from="${CustomerController.allowedProperties.toList() }" value="${searchKey }" optionValue="${{message(code:'customer.'+it+'.label',default:message(code:'domainProperty.'+it+'.label', default:it))}}" />
<input type="text" name="searchValue" value="${params?.searchValue }" />
</g:form>
对应的scaffold-list部分为:
Java代码
<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代码
domainProperty.name.label=姓名
....
相关文章推荐
- Grails scaffold 的中文化。
- .NET异步操作学习之一:Async/Await中异常的处理
- 安卓main.mk解析
- waitpid()
- RAII
- Baidu云盘【文件API】接口学习 『一』
- INSTALL_FAILED_MISSING_SHARED_LIBRARY错误解决方法
- fix system hang when thumbnail or playback interlace clips(转载)
- [分享] (超详细图文)完美解决Mountain Lion10.8.3山狮或10.7.4狮子系统安装windows7 win8多分
- mail_location not set and autodetection failed 解决方案[devecot, sendmail]
- Apache启动提示:httpd: Could not determine the server's fully qualified domain name
- 使用commons-email发邮件
- OnEraseBkGnd与OnPaint
- 业界资讯: flash player 12 与Air 4.0 上线
- error C2065: “CMainFrame”: 未声明的标识符
- 如何验证所做的AIX系统备份是否可用
- UVA11625_Lines of Containers
- Codeforces 362B Petya and Staircases
- RAII惯用法:C ++资源管理的利器
- hadoop2.0 ha failover 遇到的问题