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

How to open a modal-form, execute a action in the controller, and render the result in Grails

本文关键字:Grails 并在 结果 操作 执行 何打开 窗体 控制器 模态      更新时间:2023-09-26

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

我拥有的代码是:

普惠制

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

然后,这个模式有一个g:formRemote,里面有4个文本字段来编辑属性

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
});

圣杯

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

所以,我想在调用方法selectToEdit之后,在JS中获取Cancha,以在模态面板的文本字段中呈现属性。

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

返回的 newData 将是对象的字符串格式。(不是真正的对象,把它想象成toString()的输出)

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

使用 ajax 创建 Grail Jquery 模态窗口和表单发布?

部分摘录:

"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" );
        }