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;
}
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 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" />
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']"
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