如何打开模态窗体,在控制器中执行操作,并在Grails中呈现结果

时间:2013-10-30 20:31:49

标签: javascript jquery ajax grails modal-dialog

我的gsp中有一个表,每行都有一个要编辑的按钮。此按钮打开一个模式面板,其中包含用于编辑此行的表单。 当我打开这个模态时,js函数运行并对控制器的动作进行ajax调用,传递行的id。 在控制器中,我搜索实体,然后将其返回到视图。但问题是在视图中我看不到这个对象..

我的代码是:

GSP

<a href="#modal-form" data-id="${cancha?.id}" role="button" class="open-EditCanchaModal btn btn-xs btn-info" data-toggle="modal" /> 

然后,这个模态有一个g:formRemote,里面有4个textFields来编辑属性

JS

$(document).on("click", ".open-EditCanchaModal", function () {
    var canchaId = $(this).data('id');
    var newData = $.post("${createLink(controller: 'cancha', action: 'selectToEdit')}/"+canchaId);
    alert("dat: "+newData); // it print dat: [object Object]
    alert("can: "+newData.cancha); // it print undefined
});

GRAILS

def selectToEdit = {
    Cancha cancha = Cancha.get(params.id)
    println cancha // this found the correct "Cancha"
    [cancha:cancha]
}

所以,我想在调用方法selectToEdit之后在JS中获取Cancha,以在模式面板的textFields中呈现属性。

2 个答案:

答案 0 :(得分:0)

我也是Grails的新手。 如果我错了,请纠正我。

返回的newData将是对象的字符串格式。 (不是真实的对象,认为它是toString()的输出)

所以,要实现你想要做的事情。 您可以使用JSON编码/解码作为JS / GRAILS之间的桥梁。

答案 1 :(得分:0)

Creating a Grail Jquery Modal Window and Form posting with ajax?

一些摘录:

"Attach Comment": function() {
        //do form posting here
        $.ajax({ 
        context: $(this),
        url:"${resource()}"+"/issue/attachComment",
        type:"POST",
        data:{"comment":$('#commentArea').val(),"id":$("#selectedIssueInstanceId").val()},
        success:function(data){
        if(data.success)
        {
        var tobeAppendeID = $("#selectedIssueInstanceId").val();
        $('#'+'comment_'+tobeAppendeID).val(data.newComment);
        $( this ).dialog( "close" );
        }
        else
        {
        $('#error-message').addClass('ui-state-error ui-corner-all');
        $("#error-message").html(data.message).show()
        $(this).dialog("close");
        }
        $( this ).dialog( "close" );
        }