我有一个包含字段的列表
我只需要过滤特定的参数
型号:
class Doc {
static constraints = {
name()
parameter(inList: ["Список",
"Строка",
"Число"])
}
String name
List parameter
}
在index.gsp
中<label for="query">Where:</label>
<g:select value="${params.parameter}" name="parameter" from="${parameterList}" noSelection="['':'All parameter']" />
在控制器中
def index(Integer max) {
params.max = Math.min(params.max ? params.int('max') : 2, 10)
def parameterList = Doc.parameter.list()
if (params.parameter != "" ) {
def item = parameterList.find { p -> inList(p.parameter, "%${params.parameter}%")}
}
[params: params, parameterList: parameterList]
}
即,例如
有数据:
名称: a1 参数: [[list],[string]]
名称: a2 参数: [[list],[number]]
名称: a3 参数: [number]
我选择按&#34;数字&#34; 进行过滤,我只需要获得&#34; a2&#34; 和&#34; A3&#34;
答案 0 :(得分:0)
//your params data sample (not sure about this point)
def params=[
a1: ['123','234'],
a2: [123,234],
a3: 123,
a4: '123',
]
//the filter
def filter = "number"
//map from filter to filtering class
def filterClazz = [
number: Number.class,
string: CharSequence.class,
].get(filter)
//the filter logic
def filteredKeys = params.findAll{k,v->
//if value is collection then lookup if any element instance of our filter class
if (v instanceof Collection)return v.find{filterClazz.isInstance(it)}
//otherwise (not collection) just check if value is filter class
return filterClazz.isInstance(v);
}.collect{it.getKey()} //collect only key names
println filteredKeys