1

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

协会:

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 

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

组织.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
  }
}

类别.groovy

class Category {

    String name
    String description

    static constraints = {
        subCategories nullable:true
    }

    static hasMany = [ subCategories: SubCategory ]
}

子类别.groovy

class SubCategory {
    String name 

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

组织控制器.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.

子类别.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>

问题: 如何获取选定的子类别并使用后续操作将它们添加到组织中。

4

1 回答 1

0

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

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

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

于 2013-10-30T13:33:29.350 回答