您的位置:首页 > 大数据 > 人工智能

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=姓名
....
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: