简体   繁体   English

从gsp向控制器传递多个值以保存很多到很多

[英]Grails many to many passing multiple values from gsp to controller to save

I have a many to many relationship between forms and events, in a list using a join table and when I display the data in edit mode I show each item in its own dropdown in a table as opposed to one multi select dropdown list. 我在表单和事件之间有很多关系,在一个使用联接表的列表中,当我在编辑模式下显示数据时,我在表中的每个下拉列表中显示了每个项目,而不是一个多选下拉列表。 I also have a little jQuery in there to add extra dropdowns in if I want to add more items, My question is, how do I then save back this set of "events", in order? 如果要添加更多项,我还在那里有一个jQuery,可以添加额外的下拉菜单。我的问题是,我该如何依次保存这组“事件”? Code is as follows: - 代码如下:-

Initial _form.gsp to load all linked events for the form: - 初始_form.gsp可以加载表单的所有链接事件:-

   <table id="eventList">
        <g:each in="${formInstance?.events}" status = "i" var="item">
            <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
                <td>
                    <label></label>
                    <g:select name="event_${i}" from="${framework.Event.list()}" required="required" optionKey="id" value="${item.id}" />
                </td>
            </tr>
        </g:each>
    </table>

Additional objects are added with this gsp 此gsp添加了其他对象

<tr class="${((newRow+1) % 2) == 0 ? 'even' : 'odd'}">
    <td>
        <label></label>
            <g:select name="event_${newRow-1}" from="${framework.Event.list()}" required="required" optionKey="id" />
    </td>
</tr>

The save button currently is as such: - 当前保存按钮如下:-

<g:actionSubmit class="save" action="update" value="${message(code: 'default.button.update.label', default: 'Update')}" />

And the Update action is unchanged from the one generated automatically from the domain setup. 而且,更新操作与域设置自动生成的更新操作相同。 How do I get the save to recognise the new fields added? 如何保存以识别添加的新字段? Currently hitting save does to the "events", even if I change the order... 当前,即使我更改了顺序,也要保存到“事件”。

Domain Classes are as such 域类就是这样

import java.util.List;

class Form {
    static constraints = {
        formDesc(blank:false,maxSize:100,unique: true)
    }

    static mapping = {
        table "form"
        version false
        columns{
            id column:"form_id"
            formDesc column:"description"
            testscenarios joinTable:[name:"lnk_scenario_form",key:'form_id']
            events joinTable:[name:"lnk_form_event",key:'form_id']
        }
    }

    String formDesc
    List events

    static hasMany = [testscenarios:TestScenario, events:Event]
    static belongsTo = fartframework.TestScenario

    String toString (){
        "${formDesc}"
    }
}

And

class Event {
       static constraints = {
           eventTypeID()
           eventOrder()
           objectID()
           testDataID()
        }

    static mapping = {
        table "event_form"
        version false
        columns{
            id column:"event_form_id"
            eventTypeID column:"event_id"
            eventOrder column:"event_order"
            testDataID column:"test_data_id"
            objectID column:"object_id"

            forms joinTable:[name:"lnk_form_event", key:'event_id']
        }
    }

    EventType eventTypeID
    Integer eventOrder
    TestData testDataID
    Object objectID

    static hasMany = [forms:Form]
    static belongsTo = fartframework.Form

    String toString (){
        "${eventTypeID}"
    }
}

Turns out this was because my link table had a primary key set on both the form_id AND event_id combination columns, which I don't care about as I WANT to be able to have duplicates in there (as its the order that's more important), removing these primary key values on the table solved the problem! 事实证明,这是因为我的链接表在form_id和event_id组合列上都设置了主键,我不在乎,因为我想在那里可以有重复项(因为顺序更重要),删除表上的这些主键值可以解决问题!

I do still have the issue that if I now add/remove a bunch of times I get problems with identical names and the issues that represents, but I can work round that one with some jquery hacking (I hope)... 我仍然有一个问题,如果我现在添加/删除很多次,我会遇到名称相同和代表问题的问题,但是我可以通过一些jquery黑客解决这个问题(我希望)...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM