Grails params.max returns a map - grails

I am new to grails and have just started developing applications in work. The first thing that i wanted to do is create a gsp with two tables, each table having pagination functionality. With a research I found that there is a plugin called remotePagination that uses ajax to update a tables pagination. The problem that i am having is that the 'params.max' and 'params.offset' value is a map of two strings rather than just a string value. On opening the page the 'list' closure is called and with the correct values set for the max and offset, lets say 10. On the second call, when the ajax closure is called the max and offset values are each held within a map as follows:
params.max = [10,10]
params.offset = [10,10]
The code I am using is as follows:
Controller:
def list = {
params.max = Math.min(params.int('max') ?: 10, 100)
[bookInstanceList: Book.list(params), bookInstanceTotal: Book.count()]
}
def ajaxListBooks = {
params.max = Math.min(params.int('max') ?: 10, 100)
render(template: "bookList", model:[bookInstanceList: Book.list(params), bookInstanceTotal: Book.count()])
}
list.gsp
<%# page import="com.intelligrape.Book" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="layout" content="main"/>
<g:set var="entityName" value="${message(code: 'book.label', default: 'Book')}"/>
<title><g:message code="default.list.label" args="[entityName]"/></title>
<g:javascript library="prototype"/>
</head>
<body>
<div class="nav">
<span class="menuButton"><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a>
</span>
<span class="menuButton"><g:link class="create" action="create"><g:message code="default.new.label" args="[entityName]"/></g:link></span>
</div>
<div class="body">
<h1><g:message code="default.list.label" args="[entityName]"/></h1>
<g:if test="${flash.message}">
<div class="message">${flash.message}</div>
</g:if>
<div id="repoList">
<g:render template="bookList"/>
</div>
</div>
</body>
</html>
_listBooks.gsp
<%# page import="com.nmi.uk.sw.subzero.Book" %>
<div>
<table>
<thead>
<tr>
<util:remoteSortableColumn property="author" title="${message(code: 'book.author.label', default: 'Author')}" update="repoList" action="ajaxListBooks"/>
<util:remoteSortableColumn property="name" title="${message(code: 'book.name.label', default: 'Name')}" update="repoList" action="ajaxListBooks"/>
<util:remoteSortableColumn property="price" title="${message(code: 'book.price.label', default: 'Price')}" update="repoList" action="ajaxListBooks"/>
</tr>
</thead>
<tbody>
<g:each in="${bookInstanceList}" status="i" var="bookInstance">
<tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
<td><g:link action="show" id="${bookInstance.id}">${fieldValue(bean: bookInstance, field: "author")}</g:link></td>
<td>${fieldValue(bean: bookInstance, field: "name")}</td>
<td>${fieldValue(bean: bookInstance, field: "price")}</td>
</tr>
</g:each>
</tbody>
</table>
<div class="paginateButtons">
<util:remotePaginate total="${bookInstanceTotal}" update="repoList"
action="ajaxListBooks"
pageSizes="[10,20,30,40,50,60,70,80,90,100]" />
</div>
</div>
The above code is based on the sample application for the remotePagination tutorial. It isn't that different. I created it just to see if the plugin would work before I integrated it into my application.
I would like to know if any one else has come across this problem and if there is a solution to it. Many thanks.

In your _bookList.gsp, there is an error in <util:remotePaginate tag:
pageSizes="[10,20,30,40,50,60,70,80,90,100]"
pageSizes should be a map, not a list, like:
pageSizes="[10:'10 Per Page', 20: '20 Per Page', 50:'50 Per Page',100:'100 Per Page']"

Related

Grails- groovy sql each row to be displayed in gsp view

I want to display the result of sql each row from the service code to my gsp view.
My Service code is:
def health()
{
def schemaList = [:]
groovy.sql.Sql sql = new groovy.sql.Sql(dataSource);
sql.eachRow("SELECT SOURCE, count(1) as COUNT from fact group by SOURCE");
ArrayList returnResults = []
sqlStatement.eachRow(sqlString)
{
returnResults<<it.toRowResults()
}
sqlStatement.close()
return[returnMap:returnResults]
}
My Controller Code is:
def stats = {
def health = AccessLogService.heath()
render (template:'healthview', model:[health:health])
}
My gsp view is as follows:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="layout" content="admin" />
<title>Health</title>
</head>
<body>
<SCRIPT language="JavaScript">
</SCRIPT>
<br />
<br />
<font style='font-size:14px;font-weight:bold;'>Health Condition</font>
<div id='overall'>
<g:if test="${health.size() > 0}">
<table border="1">
<thead>
<tr>
<th>Source</th>
<th>Count</th>
</tr>
</thead>
<tbody>
<g:each in="${health}" status="i" var="thisRecord">
<tr>
<td>${thisRecord.SOURCE}</td>
<td>${thisRecord.COUNT}</td>
</tr>
</g:each>
</tbody>
</table>
</g:if>
</div>
</body>
</html>
I am not able to see the results of my query in gsp view? Where I am going wrong.
you are trying to get the wrong key of your model.
you service returns a hash [returnMap:returnResults] so your controller renders the model: [health:health] -> [health:[returnMap:returnResults]].
thus in your gsp you should refer to health.returnMap to see the list:
<g:if test="${health.returnMap}">
...
<g:each in="${health.returnMap}" status="i" var="thisRecord">
<tr>
<td>${thisRecord.SOURCE}</td>
<td>${thisRecord.COUNT}</td>
</tr>
</g:each>
...
</g:if>
UPDATE:
the code looks strange... this is how it should be:
ArrayList returnResults = []
sql.eachRow("SELECT SOURCE, count(1) as COUNT from fact group by SOURCE"){
returnResults << it.toRowResults()
}
Where is the sqlStatement variable declared in your service? I think that is the error.
And an advice you need to debug your program. for example, Test if the service returns result by:
running your app debug mode
using log.debug
using println
or if you are doing these and have seen any erorrs on your console post that here.

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 remote-pagination is refreshing entire page instead of div

I am having problems with the grails plugin remote-pagination. I have created a new project and copy pasted the sample code for the plugin. There is a book class that has a list view and a template for the table. The pagination is supposed to update only the data in the div 'filteredList', but instead the page is refreshed with the layout view, _filtered.gsp, only. Below is the code :
Controller:
class BookController {
def scaffold = true
def list = {
[bookInstanceList: Book.list(max:10,offset: 0), bookInstanceTotal: Book.count()]
}
def filter = {
params.max = Math.min(params.max ? params.int('max') : 10, 100)
render(template: 'filter', model: [bookInstanceList: Book.list(params), bookInstanceTotal: Book.count()])
}
}
list.gsp - view :
<%# page import="com.intelligrape.Book" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="layout" content="main"/>
<g:set var="entityName" value="${message(code: 'book.label', default: 'Book')}"/>
<title><g:message code="default.list.label" args="[entityName]"/></title>
</head>
<body>
<div class="nav">
<span class="menuButton"><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a>
</span>
<span class="menuButton"><g:link class="create" action="create"><g:message code="default.new.label" args="[entityName]"/></g:link></span>
</div>
<div class="body">
<h1><g:message code="default.list.label" args="[entityName]"/></h1>
<g:if test="${flash.message}">
<div class="message">${flash.message}</div>
</g:if>
<div id="filteredList">
<g:render template="filter"/>
</div>
</div>
</body>
</html>
_filtered.gsp - template for table of books
<div>
<div class="list">
<table>
<thead>
<tr>
<util:remoteSortableColumn property="id" title="${message(code: 'book.id.label', default: 'Id')}" update="filteredList" action="filter"/>
<util:remoteSortableColumn property="author" title="${message(code: 'book.author.label', default: 'Author')}" update="filteredList" action="filter"/>
<util:remoteSortableColumn property="name" title="${message(code: 'book.name.label', default: 'Name')}" update="filteredList" action="filter" max="5"/>
<util:remoteSortableColumn property="price" title="${message(code: 'book.price.label', default: 'Price')}" update="filteredList" action="filter"/>
</tr>
</thead>
<tbody>
<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: "name")}</td>
<td>${fieldValue(bean: bookInstance, field: "price")}</td>
</tr>
</g:each>
</tbody>
</table>
</div>
<div class="paginateButtons">
<util:remotePaginate total="${bookInstanceTotal}" update="filteredList" action="filter" pageSizes="[5: '5 on Page',10:'10 on Page',15:'15 on Page']" max="5" />
</div>
</div>
I just had to add the javascript library tag in the header section of the list.gsp page :
<g:javascript library="jquery" />

Display of image in show page

I have option of photo upload in my create.gsp page. I'm able to see the uploaded photo in my create page and able to upload my photo in database, but cannot see that photo in my show page.
This is create.gsp page
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="layout" content="billing-without-grid" />
<g:set var="entityName"
value="${message(code: 'skeletonBill.label', default: 'SkeletonBill')}" />
<title><g:message code="default.create.label"
args="[entityName]" /></title>
<script>
function showPhoto(imageFile) {
var fileReader = new FileReader();
var image = document.getElementById("uploadPhotoFile");
fileReader.onload = function(e) {
image.src = e.target.result;
}
fileReader.readAsDataURL(imageFile.files[0]);
}
</script>
</head>
<body>
<div class="body">
<div class="container-fluid">
<div class="row">
<div class="span6">
<img src="" name="uploadPhotoFile" id="uploadPhotoFile"
height="200" width="160" />
<table style="width: 25%">
<tbody>
<tr class="prop">
<td valign="top" class="name"><label for="uploadPhoto"><g:message code="uploadInformation.uploadPhoto.label" default="Upload Photo" /></label></td>
<td valign="top" class="value ${hasErrors(bean: uploadInformationInstance, field: 'uploadPhoto', 'errors')}">
<input type="file" id="uploadPhoto" name="uploadPhoto" onchange="showPhoto(this)" />
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</body>
</html> '
Domain class that I created
class UploadInformation {
static constraints = {
uploadPhoto(nullable:true, maxSize: 16384 /* 16K */)
}
byte[] uploadPhoto
static transients = ["uploadPhoto"]
static mapping = {
uploadPhoto column:"uploadPhoto",sqlType: "blob"
}
}
Anuj.
The problem I see after quick look at your code is:
static transients = ["uploadPhoto"]
UploadPhoto will not be saved to database because it's declarated as transient.
See more details transient properties

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