在grails中的多对多关系中使用复选框

时间:2015-03-25 14:10:36

标签: grails gorm

我遇到使用复选框的典型问题。我所拥有的是员工和技能。每个员工都可以拥有许多技能,技能可以属于许多员工。这使它成为多对多的关系。

这是我的域类

class Skill {
    String name
    static hasMany = [users:Employee, resources:Resource]
    static belongsTo = [Employee]

    static constraints = {
        name unique: true, nullable: false, blank: false
    }

    String toString() { return name }
    String getDisplayName() { return name }
}

class Employee {

String name
String surname
Date birthdate
BigDecimal salary
Address address

static hasMany = [skills: Skill]
static belongsTo = [team:Team]

static constraints = {
    address nullable: false, blank: false
    name nullable: false, blank: false, matches: '[a-zA-Z\\-\'\\s]+'
    surname nullable: false, blank: false, matches: '[a-zA-Z\\-\'\\s]+'
    skills nullable: true, blank: true
    salary nullable: false, min: 16000.0, max: 60000.0
    team nullable: true
    birthdate max:new Date(use(TimeCategory){18.years.ago.getTime()})
}

String toString() { return "\nEmployee Information:\n Name: $name\n Surname: $surname\n " +
        "Date of Birth: $birthdate\n Salary: $salary\n"}

}

员工域工作正常,我可以创建一个没有任何问题的员工。

但是当我尝试向员工添加技能时,它并没有按照我的说法去做。 例如,当我检查两种特殊技能时,它会尝试运行并保存,它会将所有技能添加到员工中。

如何告诉控制器仅添加已检查的内容。

这是我在控制器中的代码

@Transactional
def create() {
    params.each { println("$it.key -> $it.value") }
    def employee = new Employee(params)
    def address = new Address(params)
    def skills = Skill.list()

    if (params["create"]) {
        if (address.validate()) {
            address.save()
            employee.address = address
            params.remove("_s")
            skills.each { skill -> if (params["s"] != null ) {
                employee.addToSkills(skill)
                }
            }
            if (employee.validate()) {
                employee.save()
                flash.message = "Employee has been successfully created"
                redirect(action: "index")
            }
        } else {
            flash.message = "Ooops Error!!!!"
            render(view: "create", model: [employee: employee, address: address, skill: skills])
        }
    }
    [employee: employee, address: address, skill: skills]
}

这是我的create.gsp

<div class="col-md-6">
<label>Select Your Competences:</label>
   <g:each in="${skill}" var="s" status="i">
    <br/>
    <g:checkBox name="s" value="${s.id}" checked="false"/>
     <label for="s">${s.name}</label>
 </g:each>
 </div>

1 个答案:

答案 0 :(得分:0)

将您的gsp修改为:

<label>Select Your Competences:</label>
   <g:each in="${Skill.list()}" var="skill" status="i">
    <br/>
    <g:checkBox name="s.${skill}" value="${skill.id}" checked="false"/>
     <label for="s">${skill}</label>
 </g:each>
 </div>

当您提交此表单时,您的控制器上会出现一个类似于以下值的参数:[skill:[_MsOffice:'', MsOffice:1, Managing:10, AgileCoach:3, _Coding:'']

从params获取已检查值的ID,在控制器上执行以下操作:

List<Long> skillIds = new ArrayList<Long>()

  params.skill?.each { s ->

   if(!s.getKey().startsWith("_")) {

    skillIds.add(s.getValue())
   }
  }

现在在skillIds列表中,您有一个Skill的{​​{1}} ID列表, 然后将其传递给您的服务并获取技能以添加到Employee(使用Employee或其他一些gorm操作)

您可以在此处查看示例http://groovyconsole.appspot.com/script/5722903798611968