I have a grails project where I need to select the fields that I want to delete and when I click 'delete' I need a function to delete all selected items:
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 the action to check all checkboxes in the list gsp, and if they are checked, take their ids and delete the record by id.
Can i do that either by groovy or javascript?
The problem with your code is that you set value for checkbox = "". That's incorrect, because value is what get submitted to server.
You need to change it as the following:
<td><g:checkBox id="${bookInstance.id}" name="delete_checkbox" value="${bookInstance.id}"" /></td>