我想在相关类别下显示子类别。例如:
Category
SubCategory
SubCategory
SubCategory
Category
SubCategory
SubCategory
SubCategory
Category
SubCategory
SubCategory
SubCategory
以下是与类别和SubCategory相关的域类:
Category.groovy
class Category {
String name
String description
static constraints = {
}
static hasMany = [ subCategories: SubCategory ]
}
SubCategory.groovy
class SubCategory {
String name
static hasMany = [requirements: Enquiry]
static belongsTo = [ category: Category]
static constraints = {
requirements nullable:true
}
}
ShowCreateEnquiry 我尝试编写用于获取类别和相关子类别的逻辑。
def showCreateEnquiry() {
def reqCode = Util.generateUniqueReqCode()
EnquiryCommand enquiryInstance = new EnquiryCommand();
enquiryInstance.setReqCode(reqCode) ;
def marketlist = Category.list(params.id)
def subCategoryList = marketlist?.subCategories
render ( view: "showCreateEnquiry" , model:[ "enquiryInstance": enquiryInstance, "marketlist": marketlist] )
}
现在我想要这样的事情:有人告诉我,我可以在上面的动作中创建groovy列表,在那里我可以传递类别和子类别的参数并在GSP中使用这些变量并使用2或者每个循环来迭代类别和子类别。 任何人都可以帮助我实现这一目标。
答案 0 :(得分:2)
根据我的理解,以下是解决方案:)
<强> showCreateEnquiry.gsp 强>
<g:each in="${marketlist}" var="category">
<p>${category.name}</p>
<g:each in="${category.subCategories}" var="subCategory">
<p>${subCategory.name}</p>
</g:each>
</g:each>
此外,您无需在模型中包含enquiryInstance
进行渲染。并使用CSS来设置您的视图样式。