1

我正在查看一些旧代码,并试图达到将选择的联系人实例 ID(复选框)与报告附加到将填充远程字段的选择框的目标。例如,这是传递给控制器​​的参数:

[id:1, reportsToResults:[x, y, z, ], reportsTo.id:[56, 55, 55], reportsTo:[id:[56, 55, 55]], contactList:[55, 57], _contactList:[, , , ], _select:, action:index, controller:tempController]

我理论上需要的是这样的东西:

contactList:[ , , 55, 57, ] (where empty spaces are checkboxes that are NOT selected.    
reportsTo.id:[ , , 56, 56, ] (where empty spaces are empty selection boxes.

如何传递值数组?因为我可以传递所有contactList ID 和reportsTo.id - 但我无法根据数组槽正确匹配它们或将它们捆绑在一起。

任何的想法:?

<!doctype html>
<html>
    <head>
        <meta name="layout" content="main">
    </head>
    <body>
        <div id="company-roster" class="company-roster">
        <h1>${companyName} Roster</h1>
            <g:form>
                <g:hiddenField name="id" value="${id}" />
                <div class="choicePanel">
                    <div class="choicePanel-reportsTo">
                        <label>Save all reports to additions:</label>
                        <g:actionSubmit value="Update Reports To" action="addReportsTo"/>
                    </div>
                </div>
                <script>
                    function all() {
                        $(':checkbox[name=select]').click (function () {
                          $(':checkbox[name=contactList]').prop('checked', this.checked);
                        });            
                    }
                </script>
                <div style="position:absolute; margin-top:10px; width: 1350px; height: 650px; overflow: auto;">
                <table>
                    <thead>
                        <tr>
                            <th><g:checkBox name="select" onclick="all();"/></th>
                            <g:sortableColumn property="firstName" action="roster" title="${message(code: 'contact.firstName.label', default: 'First Name')}" />
                            <g:sortableColumn property="lastName" action="roster" title="${message(code: 'contact.lastName.label', default: 'Last Name')}" />
                            <th>Reports To</th>
                        </tr>
                    </thead>
                    <tbody>
                        <g:each in="${companyRoster}" status="i" var="contactInstance">
                                <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">

                                <td><g:checkBox name="contactList" value="${contactInstance.id}" checked="${false}"/></td>
                                <td>${fieldValue(bean: contactInstance, field: "firstName")}</td>
                                <td>${fieldValue(bean: contactInstance, field: "lastName")}</td>
                                <td>
                                    Reports To: <g:remoteField action="getReportsToResults" controller="contact" id="" update="rtResult_${contactInstance.id}" paramName="search" name="reportsToResults" value="" />
                                    <br/>
                                    <g:each in ="${contactInstance?.reportsTo}" var="reportsTo" status="x">
                                        <li style="list-style-type:none;">${reportsTo}<g:link controller="contact" action="removeReportsTo" params="${[reportsToID: reportsTo.id, contactInstanceID: contactInstance.id, comp_id:id]}">Remove</g:link></li>
                                    </g:each>
                                </td>
                                <td>
                                    <div id="rtResult_${contactInstance.id}" class="rtResult_${contactInstance.id}">
                                        <g:select name="reportsTo.id" from="${rtResults}" value="" />
                                    </div>
                                </td>
                                </tr>
                        </g:each>
                    </tbody>
                </table>
                </div>
            </g:form>
        </div>
    </body>
</html>
4

1 回答 1

3

我通常通过给表单上的每个复选框一个唯一且可识别的名称以及它在hiddenField类似...中引用的相应ID来做到这一点

<g:checkBox name="contact.${i}.checkBox" checked="${false}"/>
<g:hiddenField name="contact.${i}.id" value="${contactInstance.id}" />

上面,我使用了循环中的i变量,g:each这将确保每个变量都是有序且唯一的。

接下来,您需要将项目数存储在中,g:form以便您的控制器操作知道要查找多少个复选框参数,就像这样(在 g:each 之外但在g:form....

<g:hiddenField name="contactCount" value="${companyRoster.size()}" />

最后,您可以遍历params地图并处理选择或未选择的那些。

def doStuff = {
    def contactCount = params.contactCount.toInteger();

    for( i in 0..contactCount){


        def contact = Contact.get(params.contact."${i}".id);

        if (params.contact."${i}".checkBox){ //<-- this will return 'on' or ''
            println("you selected: ${contact}");
        }else{
            println("you DID NOT select: ${contact}");
        }

    }
}

有几种方法可以解决这个问题,但上述解决方案是我一直这样做的方式。代码没有经过语法测试,只是在我脑海中浮现,但希望你能明白。享受!

于 2012-06-05T17:46:35.350 回答