0
エンティティのリストを編集し、後で更新または作成しようとしています。 私は私のコントローラでは:オブジェクトのリストを編集して保存する方法
public static void editTargets(@Required @Min(2011) Integer year, @Required String type, @Required Long groupId) {
RetailRegion region = RetailRegion.findById(groupId);
notFoundIfNull(region);
TargetType tType = TargetType.valueOf(type);
notFoundIfNull(tType);
List<Target> targets = Target.findByGroupAndTypeAndYear(region, tType, year);
if (targets != null && !targets.isEmpty()) {
render(targets, year, tType, region);
}
createTargets(year, type, groupId);
}
public static void createTargets(@Required @Min(2011) Integer year, @Required String type, @Required Long groupId) {
RetailRegion region = RetailRegion.findById(groupId);
notFoundIfNull(region);
TargetType tType = TargetType.valueOf(type);
notFoundIfNull(tType);
render(region, year, tType);
}
public static void saveTargets(@Required List<Target> targets) {
notFoundIfNull(targets);
for (Target target : targets) {
if (target != null)
target.save();
}
flash.success("Targets have been saved.");
if (params.get("_save") != null) {
mgmt();
}
mgmt();
}
と私のeditTargetsテンプレート:
#{form id:'targetsForm', method:'POST', action:@saveTargets()}
<section id="targets">
<table width="750px" id="targetsTable">
<thead>
<tr>
<th>Code</th>
<th>January</th>
</tr>
</thead>
<tbody>
%{int i = 0;}%
#{list items:targets, as:'target'}
<tr>
#{field 'target.id'}
<input id="${field.id}" type="hidden" name="${field.name}" value="${field.value}" class="${field.errorClass}" />
#{/field}
<td class="center">${targets[i].code}</td>
<td class="center">
#{field 'target.jan'}
<input id="${field.id}" type="number" name="${field.name}" value="${field.value}" class="${field.errorClass}" />
#{/field}
</td>
...
私が持っている問題は、私はsaveTargetsで編集した目標を地図に戻ることはできないようだということですコントローラメソッド。
コントローラは、リスト>の代わりにフィールドを各フィールド(つまり、target.jan - > String [])の文字列配列に戻します。
オブジェクトをリストにマップする方法はありますか?
私は今、#{list items:targets、 'target'}と同様の動作をしています。
フィールドがターゲットリストに正しくマップされるので、私はほとんど動作します。何らかの理由でIDだけがマップされません。これは、モデルを更新するための鍵であるため、迷惑になります。何か案が? – emt14
私は自分の答えで提供したコードを使って試しましたか?もしそうなら、何が起こったのですか? – digiarnie
関連する問題