我有一个对象(Manager)
,其中包含另一个对象(ActionItems)
的列表。对于这两个对象,我使用CRUD模块,我从中覆盖Managers/show.html
,但我想在同一页面上显示属于Manager
的所有操作项。只有通过选择管理器对象才能使用ActionItems
。我该怎么做?
public class Manager extends Model{
...
public List<ActionItem> actions;
...
}
public class ActionItem extends Model{
...
}
public class Managers extends CRUD{
}
答案 0 :(得分:1)
没有简单的方法可以做到这一点。更多自定义 - 更多代码。
#{extends 'CRUD/layout.html' /}
#{set title:messages.get('crud.show.title', type.modelName) /}
<div id="crudShow" class="${type.name}">
<h2 id="crudShowTitle">&{'crud.show.title', type.modelName}</h2>
<div class="objectForm">
#{form action:@save(object._key()), enctype:'multipart/form-data'}
*{
* You must hide default output of the crud.form for the "actions" field.
* Just specify all fields of the Manager except the "actions".
}*
#{crud.form fields:['someField1', 'someField2', 'someField4'] /}
*{
* Now you can draw some list that contains all manager`s actions.
}*
<ul>
#{list items:'object.actions', as:'action'}
<li>
<a href="@{Actions.show(action.id)}">${action}</a>
</li>
#{/list}
</ul>
*{
* For adding a new action you need a something like link below.
* There we try to retieve Manager`s id in the Action`s blank
* method. Than we could try to modify blank() and intercept id for
* our purposes.
}*
<a href="@{Actions.blank(object.id)}">Add new action</a>
<p class="crudButtons">
<input type="submit" name="_save" value="&{'crud.save', type.modelName}" />
<input type="submit" name="_saveAndContinue" value="&{'crud.saveAndContinue', type.modelName}" />
</p>
#{/form}
</div>
#{form @delete(object._key())}
<p class="crudDelete">
<input type="submit" value="&{'crud.delete', type.modelName}" />
</p>
#{/form}
</div>
public class Actions extends CRUD {
// ...
/**
* Modifed blank() method, that will retieve managerId to
* Actions/blank.html, that will retieve it into modifed create()
* method.
*/
public static void blank(Long managerId) throws Exception {
ObjectType type = ObjectType.get(getControllerClass());
notFoundIfNull(type);
Constructor<?> constructor = type.entityClass.getDeclaredConstructor();
constructor.setAccessible(true);
Model object = (Model) constructor.newInstance();
try {
render(type, object, managerId);
} catch (TemplateNotFoundException e) {
render("CRUD/blank.html", type, object, managerId);
}
}
/**
* Modifed create() method.
*/
public static void create(Long managerId) throws Exception {
ObjectType type = ObjectType.get(getControllerClass());
notFoundIfNull(type);
Constructor<?> constructor = type.entityClass.getDeclaredConstructor();
constructor.setAccessible(true);
Model object = (Model) constructor.newInstance();
Binder.bindBean(params.getRootParamNode(), "object", object);
validation.valid(object);
if (validation.hasErrors()) {
renderArgs.put("error", play.i18n.Messages.get("crud.hasErrors"));
try {
render(request.controller.replace(".", "/") + "/blank.html", type, object);
} catch (TemplateNotFoundException e) {
render("CRUD/blank.html", type, object);
}
}
object._save();
/* Custom part {{{ */
Manager manager = Manager.findById(managerId);
manager.actions.add(object);
flash.success(play.i18n.Messages.get("crud.created", type.modelName));
if (params.get("_save") != null) {
Managers.show(managerId);
}
if (params.get("_saveAndAddAnother") != null) {
Actions.blank(managerId);
}
Actions.show(object._key(), managerId);
/* }}} */
}
/**
* Modifed show() method.
*/
public static void show(String id, managerId) throws Exception {
// ...
}
// Other modifed methods... if need.
// ...
}
#{extends 'CRUD/layout.html' /}
#{set title:messages.get('crud.blank.title', type.modelName) /}
<div id="crudBlank" class="${type.name}">
<h2 id="crudBlankTitle">&{'crud.blank.title', type.modelName}</h2>
<div class="objectForm">
*{
* Here we retieve managerId to modifed Actions.create().
}*
#{form action:@create(managerId), enctype:'multipart/form-data'}
#{crud.form /}
<p class="crudButtons">
<input type="submit" name="_save" value="&{'crud.save', type.modelName}" />
<input type="submit" name="_saveAndContinue" value="&{'crud.saveAndContinue', type.modelName}" />
<input type="submit" name="_saveAndAddAnother" value="&{'crud.saveAndAddAnother', type.modelName}" />
</p>
#{/form}
</div>
</div>
我在你的地方做过这样的事。