Reputation: 97
I'm new to grails ,I'm trying to implement archive in grails.That is When i try to delete an item from list it must delete in list but not in database. And in database a flag would be appear on deleted item in list. Please guide me to solve this problem.
Upvotes: 1
Views: 872
Reputation: 10667
You will have add an active
boolean field to the domain in question so the object in question can be marked as active or inactive. You will also have to customize the delete action in the appropriate domain controller so the object is not deleted, because instead of deleting you just want to change the active
boolean value to false
. Then in the list action of the appropriate domain controller, you will have to filter out all inactive objects before listing them.
UPDATE:
See code below for a simple explanation on what I am suggesting.
//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]
}
Upvotes: 3