gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:02:14 25 4
gpt4 key购买 nike

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

这是我的 Realm 类(class)

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"}

}

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

但是,当我尝试向员工添加技能时,它也没有做到我所说的。
例如,当我检查两项特定技能时,它会尝试运行并保存,这会将所有技能添加到员工中。

我如何告诉 Controller 仅添加已检查的内容。

这是我在 Controller 中的代码
@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>

提交此表单时,您将在 Controller 上获得一个具有以下值的参数: [skill:[_MsOffice:'', MsOffice:1, Managing:10, AgileCoach:3, _Coding:'']
要从参数获取检查值的ID,请在 Controller 上执行以下操作:
List<Long> skillIds = new ArrayList<Long>()

params.skill?.each { s ->

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

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

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

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

关于grails - 在grails中的多对多关系中使用复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29258070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com