java - jquery+grails,如何将输入数据从对话框传递到 Controller

标签 java javascript html grails groovy

我是 grails 和 jquery 的新手。

我创建了一个 jquery 对话框,如下例所示:http://jqueryui.com/dialog/#modal-form

打开一个对话框,输入数据,并将数据显示在主页的表格中。

将所有条目添加到表中后,该表是动态的 我想创建一个保存按钮,它将所有数据保存到数据库中, 我的问题是如何将表中的所有数据传递到 Controller 中以便 Controller 可以持久化数据?

这是我的 gsp:

        <fieldset class="form">
            <h2>ReportInstance</h2>
            <table id="reportInstances" class="ui-widget ui-widget-content">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Template</th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
            <button id="create_new_reportInstance">Create new Report
                Instance</button>
        </fieldset>

        <fieldset class="buttons">
            <g:submitToRemote controller="NewReport" action="saveReport"
                update="page-body" value="save" />
        </fieldset>
    </g:form>


 javascript:

           $("#dialog-form").dialog(
               {
                autoOpen : false,
                    height : 300,
                    width : 350,
                    modal : true,
                buttons : {
                 "Create an account" : function() {
                          var bValid = true;
                           allFields.removeClass("ui-state-error");
                                if (bValid) {
                                    $("#reportInstances    tbody").append(
                                    "<tr>" + "/<td>" + name.val() + "/</td>"
                                            + "/<td>" + template.val()
                                            + "/</td>" + "/<td>" + "/</tr>");

                            $(this).dialog("close");
                        }
                    },
                    Cancel : function() {
                        $(this).dialog("close");
                    }
                },
                close : function() {
                    allFields.val("").removeClass("ui-state-error");
                }
            });

    $("#create_new_reportInstance").button().click(function() {
        $("#dialog-form").dialog("open");
    });

我只想将 name.val() 和 template.val() 传递到 Controller

最佳答案

我根本不会使用submitToRemote。由于您已经在使用 jQuery,因此您可以通过 Ajax 发布此内容:

if(bValid) {
    $.ajax({
        url: "/yourApp/newReport/saveReport",
        type: "POST",
        data: {"name": name.val(), "template": template.val()},
        success: function(data) {
            $("#reportInstances tbody").append( "" + "/" + name.val() + "/" + "/" + template.val() + "/" + "/" + "/");
            $(this).dialog("close");
        },
        error: function(xhr, textStatus, error){
           //whatever you need to do here - output errors etc.
           console.log(xhr.statusText);
           console.log(textStatus);
           console.log(error);
        }

    });
}

在您的 Controller 中,您将获得一个 params.name 和一个 params.template

另请注意 Controller 名称上的小写首字母 - 在 Grails 中,NewReportController 是作为 newReport

访问的

关于java - jquery+grails,如何将输入数据从对话框传递到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13421595/

相关文章:

java - Spring 集成: Multiple Application Integration using Spring Integration

javascript - HTML5 文件 api,读取 xml/文本文件并将其显示在页面上?

javascript - 将 HTML 内容分解为轮播元素

javascript - 使用Javascript更改背景颜色并在刷新页面后保留它

html - 当我向下滚动页面时,如何让我的导航栏向下滚动

Div 容器内的 HTML + CSS 样式/文本格式

Java 8 Lambda,过滤器 HashMap,无法解析方法

java - 如何解决 Java 方法引用歧义

java - 如何重置Java中的当前对象?

html - 为什么我的控件没有分成每行 3 个控件?