[英]Groovy: Accessing properties in Parent class
This has to be something really easy, but am pretty new to Grails & Groovy. 这一定很容易,但是对于Grails&Groovy来说是很新的东西。 I am writing a Grails application in version 2.4.3.
我正在用2.4.3版编写Grails应用程序。
There's a class called UserInfo 有一个叫做UserInfo的类
class UserInfo extends UserDetails {
String userID = ""
static constraints = {
userID blank:false
}
}
Now the UserDetails class: 现在,UserDetails类:
class UserDetails {
String userName = ""
String userType = "" // 'Admin' or 'Guest' or 'IT' or 'Supervisor'
...................
//Lot more Informations
....................
static constraints = {
userName blank:false
userType blank:false
}
}
And now in another controller class i want to get the User Type information. 现在在另一个控制器类中,我想获取用户类型信息。
def getUserData = {
def type = []
int count = 0
UserInfo.each {
type [i] = it.userType; // Error: No such property: operator for class: UserInfo
i++;
}
}
The goal here is to get list of UserTypes ('Admin' or 'Guest' or 'IT' or 'Supervisor') and the total numbers of each user type. 这里的目标是获取用户类型(“管理员”或“来宾”或“ IT”或“主管”)的列表以及每种用户类型的总数。
For eg, Admin = 230, Guest = 120, ......... 例如,管理员= 230,访客= 120,.........
The code above doesn't do that and in fact gives an error. 上面的代码不执行该操作,实际上会产生错误。 I want to use the data above and plot a X - Y Graph.
我想使用上面的数据并绘制X-Y图。
Ideally i should have the data in a def in an array model as shown below. 理想情况下,我应该在数组模型的def中包含数据,如下所示。
def type = []
type << [totalNum: new Integer("230"), userType: 'Admin']
type << [totalNum: new Integer("120"), userType: 'Guest']
............................................................
............................................................
Once i have populated all the data into a def , then i could use Google Visualization to draw the graph easily. 一旦将所有数据填充到def中 ,便可以使用Google Visualization轻松绘制图形。
As of now i still don't know how to extract the data from each instance of UserInfo class and then get it to a def . 到目前为止,我仍然不知道如何从UserInfo类的每个实例中提取数据,然后将其转换为def 。
Appreciate your help! 感谢您的帮助!
您在这里缺少list()
方法:
UserInfo.list().each {...}
I don't think it's possible with the syntax you're using. 我认为使用的语法是不可能的。
UserInfo.each
refers to a colosure over static members of the UserInfo class. UserInfo.each
指的是对UserInfo类的静态成员的描述。 However if you want to enumerate things, this can be done with the following syntax. 但是,如果您想枚举事物,可以使用以下语法来完成。
class UserInfo {
enum UserType {
Admin, Guest, IT
}
}
Enumerating over these then becomes 列举这些然后变成
def getUserData = {
def types = []
def count = 0
UserInfo.UserType.each {
types << it
count++
}
}
However, in grails, with your original class, you're just missing a findAll()
or list()
call for a domain object. 但是,在grails中,对于您的原始类,您只是缺少对域对象的
findAll()
或list()
调用。 This however will produce duplicates if you have more than one of each user type. 但是,如果每种用户类型均具有多个,则会产生重复项。
def getUserData = {
def types = []
def count = 0
UserInfo.findAll().each{ UserInfo it ->
types << it.userType
count++
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.