grails中的类别和子类别

时间:2013-10-30 12:39:25

标签: grails

我正在尝试将类别和子类别添加到当前登录的用户(即组织)。我可以向用户(组织)添加类别,但无法添加子类别。

社团:

Organization hasMany = [follows: SubCategory]
Category hasMany = [ subCategories: SubCategory ]
SubCategory belongsTo = [ category: Category]

类别和子类别结构:

Main Category
 Sub Category
 Sub Category
 Sub Category

Main Category
 Sub Category
 Sub Category
 Sub Category

Main Category
 Sub Category
 Sub Category
 Sub Category 

注意:当我选择任何主要类别时,它会显示与此类别相关的子类别列表,我想选择这些子类别并将其添加到组织。

Organization.groovy

class Organization extends Party{

    String orgName
    Person contact
    String orgSize
    boolean isVendor  = false

  static hasMany = [follows: SubCategory]

  static constraints = {
    orgName blank: false
    orgSize blank: false
  }
}

Category.groovy

class Category {

    String name
    String description

    static constraints = {
        subCategories nullable:true
    }

    static hasMany = [ subCategories: SubCategory ]
}

SubCategory.groovy

class SubCategory {
    String name 

    static hasMany = [requirements: Enquiry]
    static belongsTo = [ category: Category]
    static constraints = {
        requirements nullable:true
    }
}

OrganizationController.groovy

class OrganizationController {

  def saltSource
  def userCache
  def springSecurityService
  def mailService
  def messageSource
  def organizationService
  def subscriberService
  static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

  def index() {
    redirect(action: "list", params: params)
  }

    def list() {
      params.max = Math.min(params.max ? params.int('max') : 10, 100)
      [organizationInstanceList: Organization.list(params), organizationInstanceTotal: Organization.count()]
    }

    def profile() {
      Subscriber loggedinSubscriber = subscriberService.getLoggedinSubscriber()
      Party organization = loggedinSubscriber?.customer?.party
      Party person = organization?.contact
      [orgInstance: organization, personInstance: person]
    }

    def update() {
      def orgInstance = organizationService.getOrganization(params.id)
      orgInstance.properties = params
      orgInstance?.contact?.properties = params
      println params
      try {
          organizationService.updateOrganization(orgInstance)
      }
      catch ( CustomValidationException e){
          flash.message = e.sMessage
      }
      catch (ServiceException s ) {
          flash.message = s.message
      }
     render(view: "_org", model: [orgInstance: orgInstance])
  }

    def follow() {
      Subscriber loggedinSubscriber = subscriberService.getLoggedinSubscriber()
      Party organization = loggedinSubscriber?.customer?.party
      def subparams = params.subcategories
      println subparams
      def marketInstance = Category.get(params.abc)
      def subMarket = getsubCategories(params.subcategories)
      def follower = new Follower()
      follower.followedBy = organization
      follower.category = marketInstance
      follower.subCategory = subMarket
      follower.save(failOnError: true);
      flash.msg = "Okay. This market is now on your watchlist."
      redirect (action: "profile")
    }
}

按照以上操作控制器负责将类别和子类别添加到组织(用户)。我尝试使用以下代码

def subMarket = getsubCategories(params.subcategories)  follower.subCategory = subMarket

采取跟进行动,但获得

No signature of method: com.vproc.member.OrganizationController.getsubCategories() is applicable for argument types: ([Ljava.lang.String;) values: [[2, 1, 3]] error.

subCategory.gsp

<g:form  controller="organization" params="[temp : marketInstance?.id]" action="follow" method="post">
                <g:hiddenField name="id" name= "abc" value="${marketInstance?.id}"  />
                <g:hiddenField name="id" value="${subcategory?.id}" />
                  <div style="margin-left:200px">
                    <input type="button" class="button-inner" id="check1" value="Check All" />
                    <input type="hidden" id="isChkd" value="true" />
                    <g:each var="subcategory" in="${subCategroyInstanceList}">
                    <div>
                    <g:checkBox class="cb1-element" name="subcategories" value="${subcategory.id}"/>
                    <label for="subcategories"> ${subcategory.name}</label>
                    </div>
                  </g:each>
                  <button class="btn btn-inverse">Submit</button>
                </div>
              </g:form>

问题: 如何获取选定的子类别并使用跟随操作将其添加到组织。

1 个答案:

答案 0 :(得分:0)

您已经在所有复选框中使用了相同的name,因此在您的控制器中,您可以将其转换为列表:

def sub = params.list('subcategories')
sub.each { id ->
   ... //add to your organization.
}

这是您需要与Organization关联的ID的列表,因此如果您正在更新,则可能需要删除那些已经关联且在添加新内容之前未出现在列表中的ID的。