playframework 1.x - Creating optgroup in play framework from a query result -


if example have following data retreived database:

-------------------------- |  id | name | department| -------------------------- |  1  | john | hr        | -------------------------- |  2  | peter| accounting|      -------------------------- |  3  | adam | secretary |      -------------------------- 

how can create optgroup tag display result in optgroup have following:

 <select >                           <optgroup label='hr'>               <option value='1'>john</option>             </optgroup>             <optgroup label='accounting'>               <option value='2'>peter</option>             </optgroup>             <optgroup label='secretary'>               <option value='3'>adam</option>             </optgroup>  </select> 

this may not best way, represent department model , way :

user model :

public class user extends model {      public string name;      @manytoone     public department department;   } 

department model :

public class department extends model {      public string name;      public list<user> getusers() {         list<user> users = new arraylist<user>();         if (this.id != null) {             users = user.find("department.id ?", this.id).fetch();         }         return users;     }  } 

then in view can generate select box list way

<select>     #{list departments, : 'department'}         <optgroup label="${department.name}"">             #{list department.users, : 'user'}                 <option value="${user.id}">${user.name}</option>             #{/list}         </optgroup>     #{/list} </select> 

like said, perhaps there better way, (maybe using raw sql queries...)


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -