I am using grails 2.4.2. I have a form from where I can add , edit or delete any data or row. But from index I want to delete a single row on demand. For that I have added a link in my table cell for each row. But it is giving error no 405 which is The specified HTTP method is not allowed for the requested resource. Can anyone please help me on this ? Here are my index page below :
My index.gsp >>>
<table>
<thead>
<tr>
<g:sortableColumn property="address" title="${message(code: 'userInfo.address.label', default: 'Address')}" />
<g:sortableColumn property="name" title="${message(code: 'userInfo.name.label', default: 'Name')}" />
<g:sortableColumn property="name" title="${message(code: 'userInfo.name.label', default: 'Action')}" />
</tr>
</thead>
<tbody>
<g:each in="${userInfoInstanceList}" status="i" var="userInfoInstance">
<tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
<td><g:link action="show" id="${userInfoInstance.id}">${fieldValue(bean: userInfoInstance, field: "address")}</g:link></td>
<td>${fieldValue(bean: userInfoInstance, field: "name")}</td>
<td><g:link action="delete" id="${userInfoInstance.id}">Delete</g:link></td>
</tr>
</g:each>
</tbody>
</table>
I think that you are using "allowedMethods" inside your controller , like this
static allowedMethods = [delete: "POST"]
http://grails.github.io/grails-doc/2.4.2/ref/Controllers/allowedMethods.html
but when you use the link taglib you are making a GET request instead of POST
Related
in the Grails application i need to add Extend button when a condition is true ,
Here is my GSP :
<table class="table table-hover table-bordered table-striped">
<thead>
<tr>
<g:sortableColumn property="adTitle"
title="${message(code: 'ads.adTitle.label', default: 'Ad Title')}" />
<g:sortableColumn property="adDetails"
title="${message(code: 'ads.adDetails.label', default: 'Ad Details')}" />
<g:sortableColumn property="duration"
title="${message(code: 'ads.duration.label', default: 'Duration')}" />
<g:sortableColumn property="dateCreated"
title="${message(code: 'ads.dateCreated.label', default: 'Creation Date')}" />
<g:sortableColumn property="durationDate"
title="${message(code: 'ads.durationDate.label', default: 'End Date')}" />
</tr>
</thead>
<tbody>
<g:each in="${adsInstanceList}" status="i" var="adsInstance">
<tr class="${(i % 2) == 0 ? 'even' : 'odd'}" >
<td><g:link action="show" id="${adsInstance.id}">
${fieldValue(bean: adsInstance, field: "adTitle")}
</g:link></td>
<td>
${fieldValue(bean: adsInstance, field: "adDetails")}
</td>
<td>
${fieldValue(bean: adsInstance, field: "duration")}
</td>
<td><g:formatDate date="${adsInstance.dateCreated}" formatName='date.humanView' /></td>
<td><g:formatDate date="${adsInstance.durationDate}" formatName='date.humanView' /></td>
<td id="test"> </td>
</tr>
<g:if
test="${formatDate(formatName:'date.humanView',date:adsInstance.durationDate) == formatDate(formatName:'date.humanView',date:new Date()+1)}">
<script type="text/javascript">
$(document).ready(
function() {
var add = $('<g:submitButton name="extendAd" value="Extend" />')
$('#test').append(add)
-
});
</script>
</g:if>
</g:each>
</tbody>
</table>
</div>
as the below screenshot shows when the Ad End date is 12-9-2016 the Extend button should appears , but as you can see it is showing 3 times in the first row only although it supposed to be shown in first,second and third rows.
Am i missing something in my GSP code?
You should do this without javascript:
<td id="test">
<g:if test="${formatDate(formatName:'date.humanView',date:adsInstance.durationDate) == formatDate(formatName:'date.humanView',date:new Date()+1)}">
<g:submitButton name="extendAd" value="Extend" />
</g:if>
</td>
If you want to do this with javascript, you cannot use an id (because id has to be unique) but a class:
<td class="test"></td>
...
<script type="text/javascript">
$(document).ready(
function() {
var add = $('<g:submitButton name="extendAd" value="Extend" />')
$('.test').append(add)
});
</script>
In my program, which is a project tracker, I have a table with rows corresponding to the projects, and columns corresponding to the different information about the project (name, due date, status etc.). The last column should be 'More' column, that should display a dropdown list of additional attributes of the project whenever you press it. How do I do that in Grails?
Below is my list.gsp:
<calendar:resources lang="en"/>
<!doctype html>
<html>
<head>
<meta name="layout" content="layoutMain"/>
<g:set var="entityName" value="${message(code: 'project.label', default: 'Project')}" />
<title><g:message code="default.list.label" args="[entityName]" /></title>
</head>
<body>
<div class="nav" role="navigation">
<ul>
<li><g:link class="create" action="create"><button>New Project</button></g:link></li>
</ul>
</div>
<div id="list-project" class="content scaffold-list" role="main">
<h1><g:message code="default.list.label" args="[entityName]" /></h1>
<g:if test="${flash.message}">
<div class="message" role="status">${flash.message}</div>
</g:if>
<table>
<thead>
<tr>
<g:sortableColumn property="name" title="${message(code: 'project.name.label', default: 'Name')}" />
<g:sortableColumn property="dueDate" title="${message(code: 'project.dueDate.label', default: 'Due Date')}" />
<g:sortableColumn property="startDate" title="${message(code: 'project.startDate.label', default: 'Start Date')}" />
<g:sortableColumn property="status" title="${message(code: 'project.name.label', default: 'Status')}" />
<g:sortableColumn property="requirements" title="${message(code: 'project.name.label', default: 'Requirements')}" />
<g:sortableColumn property="design" title="${message(code: 'project.name.label', default: 'Design')}" />
<g:sortableColumn property="development" title="${message(code: 'project.name.label', default: 'Development')}" />
<g:sortableColumn property="qa" title="${message(code: 'project.name.label', default: 'QA')}" />
<g:sortableColumn property="ua" title="${message(code: 'project.name.label', default: 'UA')}" />
<g:sortableColumn property="delivery" title="${message(code: 'project.name.label', default: 'Delivery')}" />
<g:sortableColumn property="more" title="${message(code: 'project.name.label', default: 'More')}" />
</tr>
</thead>
<tbody>
<g:each in="${projectInstanceList}" status="i" var="projectInstance">
<tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
<td><g:link action="show" id="${projectInstance.id}">${fieldValue(bean: projectInstance, field: "name")}</g:link></td>
<td><g:formatDate date="${projectInstance.dueDate}" /></td>
<td><g:formatDate date="${projectInstance.startDate}" /></td>
<td><g:link action="show" id="${projectInstance.id}">${fieldValue(bean: projectInstance, field: "status.name")}</g:link></td>
<td><g:link action="show" id="${projectInstance.id}">${fieldValue(bean: projectInstance, field: "requirements.name")}</g:link></td>
<td><g:link action="show" id="${projectInstance.id}">${fieldValue(bean: projectInstance, field: "design.name")}</g:link></td>
<td><g:link action="show" id="${projectInstance.id}">${fieldValue(bean: projectInstance, field: "development.name")}</g:link></td>
<td><g:link action="show" id="${projectInstance.id}">${fieldValue(bean: projectInstance, field: "qa.name")}</g:link></td>
<td><g:link action="show" id="${projectInstance.id}">${fieldValue(bean: projectInstance, field: "ua.name")}</g:link></td>
<td><g:link action="show" id="${projectInstance.id}">${fieldValue(bean: projectInstance, field: "delivery.name")}</g:link></td>
<td><g:link action="show" id="${projectInstance.id}">${fieldValue(bean: projectInstance, field: "delivery.name")}</g:link></td>
</tr>
</g:each>
</tbody>
</table>
<div class="pagination">
<g:paginate total="${projectInstanceTotal}" />
</div>
</div>
</body>
</html>
If you want to use standard Grails you can create a dropdown in Grails using a g:select tag. A prettier solution might be to use jQuery (or similar) to show hide a block of HTML.
If your project domain looks something like:
class Project {
String name
...
// more info
String attr1
int attr2
boolean isAttr3
}
You can add a convenience method to your domain to aggregate the fields into a list (or possibly another object) that can be used for a drop down box. E.g.
// utility getter to aggregate the fields into an array
def getMoreInfo() {
[attr1, attr2, isAttr3]
}
Then you can use the following in your GSP:
<td><g:select name="more" from="${projectInstance.moreInfo}" /></td>
On the click of more column you can send an Ajax request to some action which can bring you JSON of two arrays:
{extraColumns:{c1,c2}, columnValues:{{v11,v12}, {v21,v22}}}
In the response of Ajax you can parse the JSON and create additional columns for headers and additional column values for rows.
I have a table that has columns in a template:
<div id="petDiv">
<table>
<thead>
<tr>
<g:sortableColumn property="breed" title="${message(code: 'contact.breed.label', default: 'Breed')}" />
<g:sortableColumn property="age" title="${message(code: 'contact.age.label', default: 'Age')}" />
</tr>
</thead>
<tbody>
<g:each in="${availablePets}" status="i" var="pet">
<tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
<td><g:link action="show" id="${pet.id}">${fieldValue(bean: pet, field: "Breed")}</g:link></td>
<td>${fieldValue(bean: pet, field: "Age")}</g:link></td>
<td>
<g:form>
<fieldset class="pDivB">
<g:remoteLink action="updatePet" update="another template here??" params: [] >Update</g:remoteLink>
</fieldset>
</g:form>
</td>
</tr>
</g:each>
</tbody>
</table>
</div>
I want:
Hit link
Go to updatePet method in Controller with the ID of that pet
Update my <td> tags for breed and age for that row only
I tried using a jQuery/Javascript combination I read online, but it updates the first row only.
I have some sample code that does something similar - https://github.com/aldrinm/grails-sample-code
Hope that helps.
I have a a view that lists several contractor objects. The last column is from addresses which is a sorted set in the contractor object. I am trying to sort those columns and I get an exception that the property addresses is not defined in class Contractors. Do I need to handle the sorted set differently in the controller?
<table width="100%">
<thead>
<tr>
<th></th>
<g:sortableColumn property="status" title="${message(code: 'contractor.status.label', default: 'Status')}" params="[statuses:statuses,name:name,nameOperator:nameOperator ?: 'CONTAINS']"/>
<g:sortableColumn property="contractorName" title="${message(code: 'contractor.contractorName.label', default: 'Name')}" params="[statuses:statuses,name:name,nameOperator:nameOperator ?: 'CONTAINS']"/>
<g:sortableColumn property="addresses.address" title='Address' params="[statuses:statuses,name:name,nameOperator:nameOperator ?: 'CONTAINS']"/>
</tr>
</thead>
<tbody>
<g:each in="${contractorList}" status="i" var="contractor">
<tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
<td>
<g:link action="show" id="${contractor.id}">show</g:link>
|
<g:link action="edit" id="${contractor.id}">edit</g:link>
</td>
<td>${contractor.status}</td>
<td>${contractor.contractorName}</td>
<td>${contractor?.addresses?.address}</td>
</tr>
</g:each>
</tbody>
</table>
The logic in the controller:
order(params.sort, params.order)
I am very new to groovy/grails any help would be appreciated, thanks!
I am not sure if there is anything availabel out of the box for this but here is how I solved my similar issue. Basically I had to interecept sort param and do my own sorting.:
if (params.sort && params.sort == "state") {
tasks = Task.activeOnly.open.unassigned.list() //<--my query
tasks = sortByState(tasks,params) //<--custom sort
}
--- where sortByState is this:
private List sortByState(tasks, params){
tasks.sort{Task.STATES.indexOf(it.state)} //<-- this is my custom sorter
if (params.order == "DESC" ) {
tasks = tasks.reverse()
}
def begin = params.int('offset') //<-- next two lines are for paging offsets
def end = Math.min(begin+params.int('max'),tasks.size()-1)
return tasks[begin..end]
}
I want to create a gsp page that contains several tables.
Note that with the term "tables" I mean graphical tables ( tag) not database tables.
The tables shall have sortable headers
If a gsp page contains only one table it is easy to use the existing sortableColumn and paginate tags.
They insert relevant entries to the "params" map (e.g. "sort=column4711").
However if several graphical tables are involved on the page this does not work that easy.
The problem is that the associated controller does not know which table the params are associated with
(i.e. if there is a map entry "sort=column4711" the controller does not know to which table it belongs).
I am currently thinking of the following solution that I consider quite ugly:
<div class="list">
<table>
<thead>
<tr>
<g:sortableColumn property="id" title="${message(code: 'user.id.label', default: 'Id')}" />
<g:sortableColumn property="password" title="${message(code: 'user.password.label', default: 'Password')}" />
<g:sortableColumn property="userId" title="${message(code: 'user.userId.label', default: 'User Id')}" />
<g:sortableColumn property="userName" title="${message(code: 'user.userName.label', default: 'User Name')}" />
<g:sortableColumn property="id" title="${message(code: 'bookDetails.id.label', default: 'Id')}" />
<th><g:message code="bookDetails.bookId.label" default="Book Id" /></th>
<g:sortableColumn property="pages" title="${message(code: 'bookDetails.pages.label', default: 'Pages')}" />
<g:sortableColumn property="price" title="${message(code: 'bookDetails.price.label', default: 'Price')}" />
<g:sortableColumn property="id" title="${message(code: 'book.id.label', default: 'Id')}" />
<g:sortableColumn property="author" title="${message(code: 'book.author.label', default: 'Author')}" />
<g:sortableColumn property="bookId" title="${message(code: 'book.bookId.label', default: 'Book Id')}" />
<g:sortableColumn property="bookName" title="${message(code: 'book.bookName.label', default: 'Book Name')}" />
<g:sortableColumn property="category" title="${message(code: 'book.category.label', default: 'Category')}" />
</tr>
</thead>
<tbody>
<g:each in="${userInstanceList}" status="i" var="userInstance">
<tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
<td><g:link action="show" id="${userInstance.id}">${fieldValue(bean: userInstance, field: "id")}</g:link></td>
<td>${fieldValue(bean: userInstance, field: "password")}</td>
<td>${fieldValue(bean: userInstance, field: "userId")}</td>
<td>${fieldValue(bean: userInstance, field: "userName")}</td>
</tr>
</g:each>
<g:each in="${bookDetailsInstanceList}" status="i" var="bookDetailsInstance">
<tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
<td><g:link action="show" id="${bookDetailsInstance.id}">${fieldValue(bean: bookDetailsInstance, field: "id")}</g:link></td>
<td>${fieldValue(bean: bookDetailsInstance, field: "bookId")}</td>
<td>${fieldValue(bean: bookDetailsInstance, field: "pages")}</td>
<td>${fieldValue(bean: bookDetailsInstance, field: "price")}</td>
</tr>
</g:each>
<g:each in="${bookInstanceList}" status="i" var="bookInstance">
<tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
<td><g:link action="show" id="${bookInstance.id}">${fieldValue(bean: bookInstance, field: "id")}</g:link></td>
<td>${fieldValue(bean: bookInstance, field: "author")}</td>
<td>${fieldValue(bean: bookInstance, field: "bookId")}</td>
<td>${fieldValue(bean: bookInstance, field: "bookName")}</td>
<td>${fieldValue(bean: bookInstance, field: "category")}</td>
</tr>
</g:each>
</tbody>
</table>
</div>
</div>
<body>
so please guide us to solve the problem
There's a remote pagination plugin. It had some issues that I had to fix, but I don't have my modified version of it (and it's been a while since I used it). Basically it makes Ajax calls instead of rendering the entire page. You get tags for remoteSortableColumn and remotePaginate