how to select the ages which are less than or greater than from drop down in grails and groovy
I have a requirement that should have one drop down containing some conditions on age. like less than 10days,between 10 to 30 days,between 1 month to 3 months,between 4 month to 12 months,between 1yr to 2 yr.
I have domain class containing one property age(integer).and i am calculating age form dob to current date and storing in DB.I have search criteria to search based on age in search page,So how can i display these condition vales in drop down and when i select one option how to display the result based on age.
presently i am displaying all ages in drop down form the DB, please find the code and help me in doing this, if its not clear please write the comments so that i can explain u.
this is my drop down contaning all dobs
<td><span id="availableAge" ></span></td>
This is my script to get dobs from controller with an ajax call
function generateAge(data){
var list ="<select style='width:100px' id='age' name='age'><option value=''>-Select-</option>";
var opt;
for(var i=0; i<data.ageDetails.length; i++){
opt = "<option value="+data.ageDetails[i].age+">"开发者_如何学JAVA;
opt = opt+data.ageDetails[i].age;
opt = opt+"</option>";
list = list+opt;
}
list = list+"</select>";
var listObj = document.getElementById("availableAge");
if(listObj){
listObj.innerHTML = list;
}
}
It's a bad idea to store age in DB, as it changes all the time - better stick with DOB.
As the option set is fixed, make something like an enum
for it, use its values()
to render a select
enum AgeCriteriaEnum { NONE, LESS_THAN_10, BETWEEN_10_AND_30, ... so on }
and just do a switch()
like:
AgeCriteriaEnum ageEnum = AgeCriteriaEnum.valueOf(params.ageEnum)
Date today = new Date()
Patient.withCriteria {
switch(ageEnum) {
case AgeCriteriaEnum.NONE:
break;
case AgeCriteriaEnum.LESS_THAN_10:
ge('dob', today-10)
break;
case AgeCriteriaEnum.BETWEEN_10_AND_30:
lt('dob', today-10)
ge('dob', today-30)
break;
//... so on
}
}
精彩评论