Grails - Updating a TD located within a template - grails

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.

Related

Display Nested object

I use spring boot with thymeleaf, in a page try to display a form and it's nested object.
My object Factories has
id
name
List
When I display
<form id="factoriesForm" th:object="${factories}" >
...
...
<table id="machinesTable" class="table table-striped table-hover responsive">
<thead>
<tr>
<th th:text="#{name}">Name</th>
<th th:text="#{description}">Description</th>
<th></th>
</tr>
</thead>
<tbody>
<tr th:each="machine, stat : ${machines}">
<td>
<input type="hidden" th:id="${'machineId-'+stat.index}" th:field="*{machines[__${stat.index}__].id}" />
<input type="text" class="form-control" th:id="${'machineName-'+stat.index}" th:placeholder="#{name.placeholder}" placeholder="Name" th:field="*{machines[__${stat.index}__].name}" />
</td>
<td> <input type="text" class="form-control" th:id="${'machineDescription-'+stat.index}" th:placeholder="#{description.placeholder}" placeholder="Description" th:field="*{machines[__${stat.index}__].description}" /></td>
<td> <i class="fas fa-trash-alt"></i></td>
</tr>
</tbody>
</table>
</form>
My factories has many machines, but none is displayed
Any idea?
change
${factories}
for
*{factories}

show button based on a condition in Grails

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>

Delete a row using allowed method from grails link in grails

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

Calculate the sum of a particular table cell's value created by s:iterator tag in JSP from Action class of Struts2?

I want to calculate the sum of the particular property from action class which is displaying in JSP as HTML Table using <s:iterator> tag.
JSP code is:
<table border="1">
<tr>
<th>Date</th>
<th>Material</th>
<th>Quantity</th>
<th>Buyer</th>
<th>Total</th>
<th>Remarks</th>
<th colspan="2">Action</th>
</tr>
<s:set var="sumTotal" value="%{0}" />
<s:iterator value="eal" status="entries">
<tr>
<td><s:property value="date"/></td>
<td><s:property value="materialName"/></td>
<td><s:property value="quantity"/><s:property value="unitName"/></td>
<td><s:property value="buyer"/></td>
<td>₹<s:property value="total"/></td>
<s:set var="sumTotal" value="%{+#attr.total}" />
<td><s:property value="remarks"/></td>
<td>
Edit
</td>
<td>
Delete
</td>
</tr>
</s:iterator>
<tr>
<th colspan="4">Sum Total</th>
<td colspan="4"><s:property value="#sumTotal"/></td>
</tr>
</table>
I want to calculate the sum of table heading "Total" and display that below in last row with the heading "Sum Total".
Please help me..
Change
<s:set var="sumTotal" value="%{+#attr.total}" />
to
<s:set var="sumTotal" value="#sumTotal + total" />
And you do not need to use %{0} inside first <s:set> tag, simple 0 will work.
<s:set var="sumTotal" value="0" />

rendering multiple sortable tables per gsp page?

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

Resources