rendering multiple sortable tables per gsp page? - grails

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

Related

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

Dropdown list in grails

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.

Grails - Updating a TD located within a template

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.

Grails - Refreshing object list using checkbox?

So I'm diving into Grails for the first time and am trying to accomplish what I think would be an easy task, so I hope this is trivial. Time spent on it is making me feel otherwise :)
So I have a list of Contacts in a database that are tied to a boolean called isActive. I want to have a check box in my list gsp that determines whether to show inactive members or not.
I've tried using a Javascript function (which I could successfully call, but wasn't sure how to handle the passing after the call). I've also tried to add a g:if to check to see if the box's checked property was enabled, but this results in a null object (which I suspected would happen).
I've also tried attaching a remoteFunction call on the onclick of the checkbox, but I never get a response back unfortunately.
Any advice? Thanks - I appreciate it. The challenges of teaching yourself a web language for the first time :)
<html>
<head>
<meta name="layout" content="main">
<g:set var="entityName" value="${message(code: 'contact.label', default: 'Contact')}" />
<title><g:message code="default.list.label" args="[entityName]" /></title>
<g:javascript>
function updateThisPage()
{
}
</g:javascript>
</head>
<body>
<g:message code="default.link.skip.label" default="Skip to content…"/>
<div class="nav" role="navigation">
<ul>
<li><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a></li>
<li><g:link class="create" action="create"><g:message code="default.new.label" args="[entityName]" /></g:link></li>
<li><g:checkBox name="showInactives" value="${false}" onclick="....." /></li>
</ul>
</div>
<div id="list-contact" 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:if test="${isActive?.checked}">
<g:sortableColumn property="firstName" title="${message(code: 'contact.firstName.label', default: 'First Name')}" />
<g:sortableColumn property="lastName" title="${message(code: 'contact.lastName.label', default: 'Last Name')}" />
<g:sortableColumn property="phone" title="${message(code: 'contact.phone.label', default: 'Phone')}" />
<g:sortableColumn property="email" title="${message(code: 'contact.email.label', default: 'Email')}" />
<g:sortableColumn property="title" title="${message(code: 'contact.title.label', default: 'Title')}" />
<g:sortableColumn property="jobFunc" title="${message(code: 'contact.jobFunc.label', default: 'Job Func')}" />
</g:if>
</tr>
</thead>
<tbody>
<g:each in="${contactInstanceList}" status="i" var="contactInstance">
<g:if test="${contactInstance.isActive}">
<tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
<td><g:link action="show" id="${contactInstance.id}">${fieldValue(bean: contactInstance, field: "firstName")}</g:link></td>
<td>${fieldValue(bean: contactInstance, field: "lastName")}</td>
<td>${fieldValue(bean: contactInstance, field: "phone")}</td>
<td>${fieldValue(bean: contactInstance, field: "email")}</td>
<td>${fieldValue(bean: contactInstance, field: "title")}</td>
<td>${fieldValue(bean: contactInstance, field: "jobFunc")}</td>
</tr>
</g:if>
</g:each>
</tbody>
</table>
<div class="pagination">
<g:paginate total="${contactInstanceTotal}" />
</div>
</div>
</body>
Why don't you try JQuery & CSS combination? For example, with every field you have, add a class "Active" or "Inactive" according to their record in Database. Then when you click the button, simply add the class "Hidden" for all the elements that have "Inactive" class.
jQuery("#hideInactiveContact").click(function(){
jQuery(".Inactive").addClass("Hidden");
});
jQuery("#showInactiveContact").click(function(){
jQuery(".Inactive").removeClass("Hidden");
});
In CSS, you can do the following and all the elements of "Hidden" class will be hid:
.Hidden {
display: none;
}

Resources