Chris Lacy's Software Engineering Blog
Monday Nov 23, 2009
Grails Makes AJAX Too Easy
package net.chrislacy.grails
class AjaxController {
static Set names = new TreeSet()
static {
names << 'Al'
names << 'Barry'
names << 'Carol'
names << 'Chris'
names << 'Conan'
names << 'Olivia'
names << 'Sandy'
names << 'Sam'
}
def index = {
def model =[:]
model.names = names
model
}
def searchAJAX = {
Set foundNames = names.collect(new TreeSet()) {
it.startsWith(params.query) ? it : ''
}
foundNames.remove ''
render(contentType: "text/xml") {
results() {
foundNames.each { n ->
result() {
name(n)
}
}
}
}
}
def add = {
names << params.name
render names
}
}
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Ajax Example</title>
<resource:autoComplete skin="default"/>
<g:javascript library="scriptaculous" />
</head>
<body>
<g:formRemote name="add" url="[controller:'ajax', action:'add']" update="update">
<richui:autoComplete name="name" action="${createLinkTo('dir': 'ajax/searchAJAX')}"/>
</g:formRemote>
<div id="update">${names}</div>
</body>
</html>
Live: http://chrislacy.net/AjaxExample/
Posted at 06:42PM Nov 23, 2009 by chris in General |
Comments: