Get flag value from grails controller

I have a grails project where I need to select the fields that I want to delete, and when I press "delete", I need a function to delete all selected elements:

Html code:

<form name="bookForm" action="list" method="post"> .... <g:link controller="book" action="delete">Delete</g:link> .... .... <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 code:

 <script type="text/javascript"> function selectAll(){//this function is used to check or uncheck all checkboxes 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; } }//this function works fine </script> 

Problem:

I need an action to check all the checkboxes in the gsp list, and if they are checked, take their identifiers and delete the entry by id.

Can I do this using groovy or javascript?

+4
source share
2 answers

Rewrite your checkbox:

 <g:checkBox id="delete_checkbox" name="delete_checkbox" value="${bookInstance.id}" /> 

So, when you submit, you will get an array of identifiers _delete_checkbox or delete_checkbox inside your params map. To find out what you are getting, you can try to print your parameters inside the action.

 println params 

You can get the desired array using params['nameOfTheAttribute'] . When you have an array of identifiers, iterate over it and delete everything.

0
source

The problem with your code is that you are setting the value for checkbox = "". This is not true because the value is what is passed to the server.

You need to change it as follows:

  <td><g:checkBox id="${bookInstance.id}" name="delete_checkbox" value="${bookInstance.id}"" /></td> 
+1
source

All Articles