我试图创建一个ajax分页标记库,但我正在努力处理必须从我的taglib发回的html / js / ajax。在这里(我认为)我的taglib的一部分给出了问题:
out << """<button onclick="\$.post('${createLink(controller: controller, action: action, params: [max: max, offset: (offset + max), searchStr: searchStr])}', function(data){\$(#${updateDiv}).html(data);})" >Next</button>"""
这给出了chrome中的jquery错误:
Uncaught SyntaxError: Invalid or unexpected token
这是我的控制人员:
def activeUsers(){
def clubId = params.clubId
if (params.searchStr) {
def searchStr = params.searchStr
def users = []
def ret = []
def map = [:]
def max = (params.max)?:10
def offset = (params.offset)?:0
map["max"] = max
map["offset"] = offset
def usersPre = User.createCriteria().list(map) {
or {
ilike("firstName","%${searchStr}%")
ilike("surname","%${searchStr}%")
ilike("username","%${searchStr}%")
}
and {
profile {
eq("club", clubId)
}
}
order("firstName","asc")
}
println(usersPre)
println(usersPre.totalCount)
render(template: 'tblActives', model: [users: usersPre, clubId: clubId, searchStr: searchStr, total: usersPre.totalCount])
} else {
[clubId: clubId]
}
}
这是我的整个标签:
def paginateAjax = { attrs, body ->
def updateDiv = attrs.divId
def controller = attrs.controller
def action = attrs.action
def max = (attrs.max.toInteger())?:10
def offset = (attrs.offset.toInteger())?:0
def searchStr = attrs.model
def total = attrs.total.toInteger()
def pages = total/max
if (total > max) {
out << "<span>"
if (offset > 0) {
// Don't mind this next line, I haven't even attempted anything here yet
out << """<button onclick="previous()">Previous</button>"""
}
(1..pages).each { num ->
// I also haven't tried anything here yet either
out << """<button onclick="pageNum(${num})" >${num}</button>"""
}
if (offset != (pages * max)) {
out << """<button onclick="\$.post('${createLink(controller: controller, action: action, params: [max: max, offset: (offset + max), searchStr: searchStr])}', function(data){\$(#${updateDiv}).html(data);})" >Next</button>"""
}
out << "</span>"
out << body()
}
}
这是我的html(此操作呈现的模板):
<div class="row-fluid">
<div class="span12 hero-unit">
<table class="table table-hover" id="results">
<thead>
<tr>
<td>ID</td>
<td>Name</td>
<td>Surname</td>
<td>Email</td>
<td>Cell</td>
<td>Activated</td>
<td>Voucher</td>
<td>Details</td>
</tr>
</thead>
<tbody>
<g:each in="${users}" var="u">
<tr>
<td>${u.id}</td>
<td>${u.firstName}</td>
<td>${u.surname}</td>
<td>${u.username}</td>
<td><g:cellphone user="${u.id}"/></td>
<g:if test="${!u.accountExpired}">
<td>
<button class="btn btn-primary btn-mini" style="cursor: default; font-size: 1.5em; width: 100%; color: whitesmoke;" disabled="disabled">
✓
</button>
</td>
</g:if>
<g:else>
<td>
<button class="gen-btn btn-red" onclick="activate('${u.id}')" style="font-size: 1.5em; width: 100%;">
×
</button>
</td>
</g:else>
<td><button class="gen-btn" style="width: 100%" onclick="viewVouch('${u.id}')">View</button></td>
<td><button class="gen-btn" style="width: 100%;" onclick="edit('${u.id}', '${u.firstName}', '${u.surname}', '${u.username}', '<g:cellphone user="${u.id}" />')">Edit</button></td>
</tr>
</g:each>
</tbody>
</table>
%{--<g:paginateLink action="activeUsers" controller="adminUser" max="10" model="${searchStr}" offset="0" total="${total}"/>--}%
<g:paginateAjax total="${total}" offset="0" model="${searchStr}" max="10" action="activeUsers" controller="adminUser" divId="tablesArea" />
</div>