Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 2.02 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to map a list of objects for editing and saving
  2. public static void editTargets(@Required @Min(2011) Integer year, @Required String type, @Required Long groupId) {
  3.     RetailRegion region = RetailRegion.findById(groupId);
  4.     notFoundIfNull(region);
  5.     TargetType tType = TargetType.valueOf(type);
  6.     notFoundIfNull(tType);
  7.  
  8.     List<Target> targets = Target.findByGroupAndTypeAndYear(region, tType, year);
  9.  
  10.     if (targets != null && !targets.isEmpty()) {
  11.         render(targets, year, tType, region);
  12.     }
  13.     createTargets(year, type, groupId);
  14. }
  15.  
  16. public static void createTargets(@Required @Min(2011) Integer year, @Required String type, @Required Long groupId) {
  17.     RetailRegion region = RetailRegion.findById(groupId);
  18.     notFoundIfNull(region);
  19.     TargetType tType = TargetType.valueOf(type);
  20.     notFoundIfNull(tType);
  21.  
  22.     render(region, year, tType);
  23. }
  24.  
  25. public static void saveTargets(@Required List<Target> targets) {
  26.     notFoundIfNull(targets);
  27.  
  28.     for (Target target : targets) {
  29.         if (target != null)
  30.             target.save();
  31.     }
  32.  
  33.     flash.success("Targets have been saved.");
  34.     if (params.get("_save") != null) {
  35.         mgmt();
  36.     }
  37.     mgmt();
  38. }
  39.        
  40. #{form id:'targetsForm', method:'POST', action:@saveTargets()}
  41.   <section id="targets">
  42.     <table  width="750px" id="targetsTable">
  43.       <thead>
  44.       <tr>
  45.         <th>Code</th>
  46.         <th>January</th>
  47.       </tr>
  48.       </thead>
  49.       <tbody>
  50.         %{int i = 0;}%
  51.         #{list items:targets, as:'target'}
  52.         <tr>
  53.           #{field 'target.id'}
  54.             <input id="${field.id}" type="hidden" name="${field.name}" value="${field.value}" class="${field.errorClass}" />
  55.           #{/field}
  56.           <td class="center">${targets[i].code}</td>
  57.           <td class="center">
  58.           #{field 'target.jan'}
  59.             <input id="${field.id}" type="number" name="${field.name}" value="${field.value}" class="${field.errorClass}" />
  60.           #{/field}
  61.           </td>
  62.           ...
  63.        
  64. %{ fieldName = "target[${i}].jan" }%
  65. #{field "${fieldName}"}
  66.     ...
  67. #{/field}