2

我有一个 grails 项目,我需要选择要删除的字段,当我单击删除时,我需要一个函数来删除所有选定的项目:

html代码:

<form name="bookForm" action="list" method="post">
    ....
    <a onclick="deleteBooks();">Delete</a>
    ....
    ....
    <g:checkBox id="select_all" name="select_all" value="" onclick="selectAll();" />
    ....
    <g:each in="${bookList}" status="i" var="bookInstance">
        <tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
            <td><g:checkBox id="${bookInstance.id}" name="delete_checkbox" value="" /></td>
        </tr>
    </g:each>
    ....
</form>

javascript代码:

<script type="text/javascript">
    function selectAll(){
        var select = document.getElementById("select_all");
        var checkboxes = document.forms['bookForm'].elements['delete_checkbox'];
        if (select.checked){
            for (i = 0; i < checkboxes.length; i++) checkboxes[i].checked = true;
        }else{
            for (i = 0; i < checkboxes.length; i++) checkboxes[i].checked = false;
        }
    }

    function deleteBooks(){
        var checkboxes = document.forms['bookForm'].elements['delete_checkbox'];
        var counter = 0;
            for (i = 0; i < checkboxes.length; i++){
                if(checkboxes[i].checked){
                    counter ++;
                    ${g.remoteFunction(action:'delete', controller:'book', id:checkboxes[i].id) }
                }
            }
            if (counter == 0) alert("select books to delete");
        }
    </script>

selectAll 函数工作正常,但 deleteBooks 函数在我添加时会导致此错误 ${g.remoteFunction(action:'delete', controller:'book', id:checkboxes[i].id) }

例外:

Error 500: Error evaluating expression [g.remoteFunction(action:&#39;delete&#39;, controller:&#39;book&#39;, id: checkboxes[i].id)] on line [26]: Cannot get property &#39;null&#39; on null object
Servlet: grails
URI: /myProject/grails/book/list.dispatch
Exception Message: Cannot get property &#39;null&#39; on null object 
Caused by: Error evaluating expression [g.remoteFunction(action:&#39;delete&#39;, controller:&#39;book&#39;, id: checkboxes[i].id)] on line [26]: Cannot get property &#39;null&#39; on null object 
Class: list.gsp 
At Line: [26] 
Code Snippet:

如果我用${g.remoteFunction(action:'delete', controller:'book') }

例外:

Error 500: Error evaluating expression [g.remoteFunction(action:&#39;delete&#39;, controller:&#39;book&#39;)] on line [27]: No javascript provider is configured
Servlet: grails
URI: /myProject/grails/book/list.dispatch
Exception Message: No javascript provider is configured 
Caused by: Error evaluating expression [g.remoteFunction(action:&#39;delete&#39;, controller:&#39;book&#39;)] on line [27]: No javascript provider is configured 
Class: list.gsp 
At Line: [27] 
Code Snippet:

如何从 javascript 函数或 jquery 调用控制器操作?

4

4 回答 4

3

GSP 在服务器端工作,JavaScript 在客户端工作。你不能混合它,使用ajax:

$.ajax({
   type: 'POST',
   url: "${createLink(action:'delete', controller:'book')}/" + checkboxes[i].id
});
于 2012-08-08T15:15:02.763 回答
3
function deleteBooks(){
    var checkboxes = document.forms['bookForm'].elements['delete_checkbox'];
    var counter = 0;
    for (i = 0; i < checkboxes.length; i++){
        if(checkboxes[i].checked){
              counter ++;
              var bookId = checkboxes[i].id;
              <g:remoteFunction controller="book" action="delete" id="${bookId}" /> 
        }
    }
    if (counter == 0) alert("select books to delete");
}

如果有任何错误请通知我

于 2012-08-09T13:47:08.407 回答
0

remoteFunction很好,但是您需要有一个 javascript 库。查看http://grails.org/plugin/jquery以及如何安装它。

于 2012-08-08T15:19:02.897 回答
0

如果您编写 javascript 内联,您可以这样做。但是,如果 javascript 在您包含的文件中 - 它将不起作用。你也不需要“g”。在远程函数中。

于 2012-08-08T15:22:56.610 回答