简体   繁体   English

在弹出窗口中填充来自 groovy 控制器的数据

[英]Populate data from groovy controller in pop up

I have a gsp page with a delete button for each row of a table.我有一个 gsp 页面,表格的每一行都有一个删除按钮。 On the button click I want a pop up which tells the consequences of the delete.在按钮上单击我想要一个弹出窗口,告诉删除的后果。 These consequences depends on the data present in the row and a few other constraints known to the grails service which is called from the grails controller associated to the gsp page.这些结果取决于行中存在的数据以及从与 gsp 页面关联的 grails 控制器调用的 grails 服务已知的一些其他约束。 If the user confirms these consequences the row should be deleted from the table, else the table remains unchanged.如果用户确认这些结果,则应从表中删除该行,否则该表保持不变。

How should i go about to achieve this behavior?我应该如何去实现这种行为?

Currently, I have in my gsp目前,我在我的 gsp

<tr>
    <td>name</td>
    <td>parentName</td>
    <td>data</td>
    <td>
        <g:link action="deleteRow" params="${[name: row.name, parentName: row.parentName]}">
            <button class="deleteSnapshot">Delete</button>
        </g:link>
    </td>
</tr>

and in my .js file在我的 .js 文件中

$(document).on('click', ':button', function (e) {
        var btn = $(e.target);
        btn.attr("disabled", "disabled"); // disable button
        alert('getting deletion details');
        //var deletionDetails -->not sure how to get these
        //var deletionDetails will get data from controller action:"getDetails"
        if (confirm('/*print deletion details and ask*/ Do you want to proceed?')) {
            alert('will delete')
            return true
        }
        else {
            btn.removeAttr("disabled"); // enable button
            return false
        }
    });

and in my controller在我的控制器中

class DeleteController{
    DeleteService deleteService
    def index() {
        [list:deleteService.getTableList()]
    }
    def getDeletionDetails(string name, String parentName){
        return deleteService.getDetails(name,parentName)
    }
    def deleteRow(String name, String parentName){
        service.deleteRow(name, parentName)
        redirect controller:"DeleteController", action:"index"
    }
}

I know the deletion works fine, because it works even with in the current state.我知道删除工作正常,因为即使在当前状态下也能正常工作。 Just that the confirmation box asks Do you want to proceed, without displaying the details.只是确认框询问您是否要继续,而不显示详细信息。

Any help on how i could achieve what I am looking for will be appreciated.任何关于我如何实现我正在寻找的帮助将不胜感激。

PS I am new to stackoverflow, so if i missed out on certain convention do let me know. PS 我是 stackoverflow 的新手,所以如果我错过了某些约定,请告诉我。

Thanks in advance.提前致谢。

I can think of two ways of doing it:我可以想到两种方法:

The first one is using ajax to both get deletion details and delete the row第一个是使用ajax来获取删除细节和删除行

Assuming that deleteService.getDetails(name, parentName) returns a String, first you need to change an getDeletionDetails action so it renders the response:假设deleteService.getDetails(name, parentName)返回一个字符串,首先您需要更改一个getDeletionDetails操作,以便它呈现响应:

def getDeletionDetails(String name, String parentName){
    render deleteService.getDetails(name, parentName)
}

and change g:link -s to buttons in gsp :并将g:link -s 更改为gsp 中的按钮:

<button data-name="${row.name}" data-parent-name="${row.parentName}">
     Delete
</button>

In your .js then put:在你的 .js 然后输入:

$(document).on('click', ':button', function (e) {
    var btn = $(e.target);

    btn.attr("disabled", "disabled"); // disable button

    var name = btn.data('name');
    var parentName = btn.data('parentName');

    $.ajax({
        url: "/delete/getDeletionDetails",
        data: {
            name: name,
            parentName: parentName
        },
        success: function (data) {
            if (confirm(data + '\nDo you want to proceed?')) {
                $.ajax({
                    url: '/delete/deleteRow',
                    data: {
                        name: name,
                        parentName: parentName
                    },
                    success: function (d) {
                        console.log("Success: " + d);
                    }
                });
            } else {
                btn.removeAttr("disabled"); // enable button
            }
        }
    });
});

What this code does is it sends an ajax call to /delete/getDeletionDetails , then uses its response (rendered by getDeletionDetails action in DeleteController ) to show a confirmation alert.此代码的作用是向/delete/getDeletionDetails发送 ajax 调用,然后使用其响应(由DeleteController 中getDeletionDetails操作呈现)显示确认警报。 If user confirms the question, another ajax call is sent - now to deleteRow action of DeleteController - with parameters taken from data attributes of clicked button.如果用户确认该问题,则会发送另一个 ajax 调用 - 现在发送到DeleteController 的deleteRow操作 - 参数取自点击按钮的数据属性。 If user cancels - nothing happens, except for reenabling a button.如果用户取消 - 除了重新启用按钮之外,什么都不会发生。

Your deleteRow should only change the return statement - it also must render the response:您的deleteRow应该只更改 return 语句 - 它还必须呈现响应:

def deleteRow(String name, String parentName){
    service.deleteRow(name, parentName)
    render "You deleted an item $name - $parentName."
}

You don't need redirect here, because - thanks to using ajax - user will never leave delete/index .您不需要在此处重定向,因为 - 由于使用了 ajax - 用户永远不会离开delete/index You can just display some kind of confirmation on page after successful ajax call.成功调用ajax后,您可以在页面上显示某种确认。

The second option is to put deletion details in hidden fields or data- attributes in each row and then just retrieve them in js :第二种选择是将删除细节放在每行的隐藏字段或数据属性中,然后在js 中检索它们:

You can create a method getDeletionDetails() in row 's domain class (presumably Row ) that returns the details (using services in domain classes is not perfect, but is should work ok if the service is not very complex).您可以在row的域类(大概是Row )中创建一个方法getDeletionDetails()返回详细信息(在域类中使用服务并不完美,但如果服务不是很复杂,应该可以正常工作)。 Then, in your .gsp place:然后,在您的.gsp位置:

<td>
    <g:link action="deleteRow" params="${[name: row.name, parentName: row.parentName]}">
        <button class="deleteSnapshot" data-details="${row.deletionDetails}">Delete</button>
    </g:link>
</td>

You should then be able to get details in .js like this:然后,您应该能够在.js 中获取详细信息,如下所示:

var deletionDetails = btn.data('details');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM