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

Using checkboxes in many to many relationships in grails

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

这是我的域类

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>

将您的 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:'']

要从参数中获取检查值的 ID,请在您的控制器上执行以下操作:

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

  params.skill?.each { s ->

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

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

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

你可以在这里看到例子http://groovyconsole.appspot.com/script/5722903798611968