my searchable on grails
Posted: December 27, 2008 Filed under: code, grails, programming 1 CommentWithin grails, Book class-domain is very common used as sample. And here is my simple implementation of searchable plugin on Book class-domain:
Book.groovy
class Book {
String bookName Date release String description static Searchable = true }
BookController.groovy
def search = {
def bookInstanceList def searchResult if (params.query) { searchResult = Book.search('*'+params.query+'*') bookInstanceList = searchResult.results } else bookInstanceList = Book.list() render(view:'list', model:[bookInstanceList:bookInstanceList, searchResult:searchResult, query:params.query]) }
List view
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <meta name="layout" content="main" /> <title>Book List</title> </head> <body> <div class="nav"> <span class="menuButton"><a class="home" href="${createLinkTo(dir:'')}">Home</a></span> <span class="menuButton"><g:link class="create" action="create">New Book</g:link></span> </div> <div class="body"> <h1>Book List</h1> <g:if test="${flash.message}"> <div class="message">${flash.message}</div> </g:if> <div class="list"> <div class="nav"> <g:form name="search" action="search"> search: <input type="text" name="query" value="${query}"/> <input type="submit" value="search"> </g:form> </div> <table> <thead> <tr> <g:sortableColumn property="id" title="Id" /> <g:sortableColumn property="bookName" title="Book Name" /> <g:sortableColumn property="description" title="Description" /> <g:sortableColumn property="release" title="Release" /> </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:'bookName')}</td> <td>${fieldValue(bean:bookInstance, field:'description')}</td> <td>${fieldValue(bean:bookInstance, field:'release')}</td> </tr> </g:each> </tbody> </table> </div> <div class="paginateButtons"> <g:if test="${searchResult}"> <g:paginate total="${searchResult.total}" params="[ query:query]"/> </g:if> <g:else> <g:paginate total="${Book.count()}" /> </g:else> </div> </div> </body> </html>
and here is the shoot:
The search criteria above is based on word match, for more details about more search criteria or all features on grails searchable please see the documentation on grails plugins site
Hi, this is a comment.
To delete a comment, just log in, and view the posts’ comments, there you will have the option to edit or delete them.