我是grails的新手,我试图在grails中实现存档。也就是说,当我尝试从列表中删除项目时,它必须在列表中删除,但不能在数据库中删除。并且在数据库中,标记将出现在列表中的已删除项目上。
请指导我解决这个问题。
请您参考如下方法:
您将在相关域中添加一个active bool(boolean) 值字段,以便可以将相关对象标记为 Activity 或不 Activity 。您还必须在适当的域 Controller 中自定义delete Action ,以便不删除该对象,因为要删除而不是删除,您只想将active bool(boolean) 值更改为false即可。然后,在适当的域 Controller 的列表操作中,您必须在列出所有非 Activity 对象之前将其过滤掉。
更新:
请参阅下面的代码,以对我的建议进行简单说明。
//The User domain class
class User {
String username
boolean active = true
}
//The delete action of the User controller
def delete = {
def userInstance = User.get(params.id)
if (userInstance) {
//Here instead of deleting the user, we just mark the user as inactive.
userInstance?.active = false
//You may choose to change this message to something that indicates the user is
//now inactive instead of deleted since the user is not really being deleted.
flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'user.label', default: 'User'), params.id])}"
redirect(action: "list")
}
else {
flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.id])}"
redirect(action: "list")
}
}
//The list action of the User controller
def list = {
def users = User.findAll("from User as users where users.active=false")
//Instead of writing "userInstance: User.list()" I filtered out all inactive users
//and created the "users" object then wrote "userInstance: users". Build in other
//parameters as you see fit.
[userInstanceList: users]
}




