[英]Groovy on Grails list - not working?
Removing an element from a list isn't working, which doesn't make any sense. 从列表中删除元素不起作用,这没有任何意义。 Am I missing some special semantics peculiar to dealing with a Grails domain object list? 我是否缺少处理Grails域对象列表所特有的某些特殊语义?
In controller: 在控制器中:
def userCreate = {
def workgroupInstance = new Workgroup()
workgroupInstance.manager = authUserDomain
flash.message = User.list().toString()
def usersWithoutThisOne = User.list() - authUserDomain
flash.message = flash.message + "removed ${authUserDomain}, new list is ${usersWithoutThisOne}"
return ['workgroupInstance':workgroupInstance, 'users':usersWithoutThisOne]
}
Results in this being displayed in flash.message 结果显示在flash.message中
[boogie, toogie, choogie, cookie]removed boogie, new list is [boogie, toogie, choogie, cookie] [布吉,toogie,choogie,cookie]已删除布吉,新列表为[布吉,toogie,choogie,cookie]
Where does authUserDomain come from? authUserDomain来自哪里?
If you haven't implemented a customn .equals() on User (based on username or someother unique identified) then it may not be the same object that is returned via User.list(). 如果尚未在User上实现customn .equals()(基于用户名或其他标识的唯一身份),则它可能与通过User.list()返回的对象不同。 An element will only be removed if it matches an existing object using .equals() 只有使用.equals()匹配现有对象的元素才会被删除
如果您打算将用户从工作组中永久删除,则需要使用grails removeFrom函数来摆脱存储在具有多个关联的类中。
I am not sure about this one. 我不确定这一点。 Don't have a Groovy interpreter available right now. 现在没有可用的Groovy解释器。 But IIRC and as this article suggests, the -
on lists expects to operate on two lists, ie, 但是IIRC和本文所建议的, -
列表上的-
期望在两个列表上进行操作,即
list - other
is actually more like 实际上更像
list.removeAll(other)
(in Java terms), not the intended (以Java术语表示),而不是预期的
list.remove(other)
You might try 你可以试试
modifiedList = originalList - [elementToRemove]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.