如何访问gsp文件中在<g:each>标记中生成的各个值?</g:each>

时间:2014-05-16 14:41:24

标签: grails gsp

我在gsp文件中有以下代码,用于编辑现有列表的单个值(patientPts)

<g:each var="points" in="${patientPts}">
            <b>Term: ${points.term} - <g:if test="${points.type.equals('C')}">Calculus<br>
                </g:if> <g:else>Perio<br>
                </g:else></b>
            <label for="${points.id}01">Class 0/1:</label>
            <input type="text" name="class_01" value="${points.class_01}"
                id="${points.id}01">
            <br>
            <label for="${points.id}2">Class 2:&nbsp;&nbsp;&nbsp;</label>
            <input type="number" name="class_2" value="${points.class_2}"
                id="${points.id}2">
            <br>
            <label for="${points.id}3">Class 3:&nbsp;&nbsp;&nbsp;</label>
            <input type="number" name="class_3" value="${points.class_3}"
                id="${points.id}3">
            <br>
            <label for="${points.id}4">Class 4:&nbsp;&nbsp;&nbsp;</label>
            <input type="number" name="class_4" value="${points.class_4}"
                id="${points.id}4">
            <br>
        </g:each>
        <br>
        <div class="buttons">
            <g:link value="Save" action="save"
                params="${[patientPts: patientPts]}">Save</g:link>
            <g:actionSubmit value="Cancel" action="index" />
        </div>

patientPts是具有以下属性的PatientPoints对象列表

String courseKey; //Stores the course key of the course for the patient points object
String term; // Stores the term of the patient points object
String type; // Stores the type of the patient points object
Double class_01; //Stores the points for the class 0 and class 1
Double class_2;  //Stores the points for the class 2
Double class_3; //Stores the points for the class 3
Double class_4; //Stores the points for the class 4

显示此内容 Grade Adjustments edit

我的控制器中有一个保存操作,我需要能够获取所有这些对象并保存对它们所做的更改(如果有的话)。我无法做到这一点,因为当它们全部由标签生成时,我无法弄清楚如何分别引用每个文本字段。谁能帮我吗?

2 个答案:

答案 0 :(得分:2)

听起来您想在GSP中使用数组或列表绑定语法。您可以将PatientPts对象放在命令对象内的Set中,例如:

class PatientCommand {
    Set<PatientPts> patientPts
}

在您的GSP中,您希望使用索引表示法进行绑定:

<g:each var="points" in="${commandInstance.patientPts}" status="i">
    ...
    <input type="text" name="patientPts[${i}]class_01" value="${points.class_01}" 
           id="${points.id}01">

当表单被提交并绑定到PatientCommand时,数据绑定器将创建不同的PatientPts对象并将它们绑定到Set中(自动创建和增长它)。

答案 1 :(得分:1)

我猜PatientPoints是一个域类。您可以使用command object包装PatientPoints

的集合
class PatientPointsUpdate {
    List<PatientPoints> patientPoints
}

您必须相应地修改视图(为id添加缺少的输入并更新现有的名称):

<g:each var="points" in="${patientPts}" status="i">
    <g:hiddenField name="patientPointsUpdate[i].id" value="points.id" />
    <b>Term: ${points.term} - <g:if test="${points.type.equals('C')}">Calculus<br>
    </g:if> <g:else>Perio<br>
    </g:else></b>
    <label for="${points.id}01">Class 0/1:</label>
    <input type="text" name="patientPointsUpdate[i].class_01" value="${points.class_01}"
           id="${points.id}01">
    <br>
    <label for="${points.id}2">Class 2:&nbsp;&nbsp;&nbsp;</label>
    <input type="number" name="patientPointsUpdate[i].class_2" value="${points.class_2}"
           id="${points.id}2">
    <br>
    <label for="${points.id}3">Class 3:&nbsp;&nbsp;&nbsp;</label>
    <input type="number" name="patientPointsUpdate[i].class_3" value="${points.class_3}"
           id="${points.id}3">
    <br>
    <label for="${points.id}4">Class 4:&nbsp;&nbsp;&nbsp;</label>
    <input type="number" name="patientPointsUpdate[i].class_4" value="${points.class_4}"
           id="${points.id}4">
    <br>
</g:each>

如果在save操作中一切正常,则可以从命令对象中包含的数据中检索和更新数据库中的PatientPoints个实例:

def save(PatientPointsUpdate patientPointsUpdate) {
    //...
}