Posted on 2010-12-01 12:32 
asdtiang 阅读(912) 
评论(0)  编辑  收藏  
			 
			
		 
		
grails ajax分页标签实现--不带参数续传的
http://www.blogjava.net/asdtiang/archive/2010/11/26/339127.html
这次有了新的需求,比如说在结果中查询--查询结果用的是这个分页,那么参数不继续传的话,就会出错了
只是更改了上次的tagLib类:
代码如下:
 import org.springframework.web.servlet.support.RequestContextUtils as RCU;
import org.springframework.web.servlet.support.RequestContextUtils as RCU;
 import org.codehaus.groovy.grails.commons.ConfigurationHolder
import org.codehaus.groovy.grails.commons.ConfigurationHolder

 class AjaxPageTagLib
class AjaxPageTagLib  {
{

 def paginateAjax=
    def paginateAjax= {attrs ->
{attrs ->
 def writer = out
        def writer = out
 //获取其它的参数
        //获取其它的参数
 def params1=new HashMap()
        def params1=new HashMap()
 params1.putAll(params)
        params1.putAll(params)
 params1.remove("max")
        params1.remove("max")
 params1.remove("offset")
        params1.remove("offset")
 params1.remove("controller")
        params1.remove("controller")
 params1.remove("action")
        params1.remove("action")
 def otherParamsUrl=""
        def otherParamsUrl=""

 params1.each
        params1.each {
{
 otherParamsUrl=otherParamsUrl+it.toString()+"&"
            otherParamsUrl=otherParamsUrl+it.toString()+"&"
 
            
 }
            }        
 params.offset = Math.min(params.offset ? params.offset as int : 0, 100)
        params.offset = Math.min(params.offset ? params.offset as int : 0, 100)
 params.sort=params.sort==null?"dateCreated":params.sort
        params.sort=params.sort==null?"dateCreated":params.sort
 params.order=params.order==null?"desc":params.order
        params.order=params.order==null?"desc":params.order
 def messageSource = grailsAttributes.getApplicationContext().getBean("messageSource")
        def messageSource = grailsAttributes.getApplicationContext().getBean("messageSource")
 def locale = RCU.getLocale(request)
        def locale = RCU.getLocale(request)        
 def total = attrs.total.toInteger()
        def total = attrs.total.toInteger()
 def offset = params.offset?.toInteger()
        def offset = params.offset?.toInteger()
 def max = params.max?.toInteger()
        def max = params.max?.toInteger()
 int pageSize=Math.round(Math.ceil(total / max))
        int pageSize=Math.round(Math.ceil(total / max))    
 def path=request.getContextPath()
        def path=request.getContextPath()    
 def pageNow=offset/max+1
        def pageNow=offset/max+1
 def selectMax=attrs.selectMax?.toInteger()
        def selectMax=attrs.selectMax?.toInteger()

 if(!selectMax)
        if(!selectMax) {
{
 selectMax=20
            selectMax=20
 }
        }
 
        
 selectMax=selectMax>total?total:selectMax
        selectMax=selectMax>total?total:selectMax
 ////js输出
        ////js输出
 writer<<"""
        writer<<"""
 <div id="ajaxPage">
         <div id="ajaxPage">
 """
        """
 /////显示首页和上页
        /////显示首页和上页
 def firstText=messageSource.getMessage('paginate.first', null, messageSource.getMessage('default.paginate.first', null, 'First', locale), locale)
        def firstText=messageSource.getMessage('paginate.first', null, messageSource.getMessage('default.paginate.first', null, 'First', locale), locale)
 def prevText=messageSource.getMessage('paginate.prev', null, messageSource.getMessage('default.paginate.prev', null, 'Prev', locale), locale)
        def prevText=messageSource.getMessage('paginate.prev', null, messageSource.getMessage('default.paginate.prev', null, 'Prev', locale), locale)
 def nextText=messageSource.getMessage('paginate.next', null, messageSource.getMessage('default.paginate.next', null, 'Next', locale), locale)
        def nextText=messageSource.getMessage('paginate.next', null, messageSource.getMessage('default.paginate.next', null, 'Next', locale), locale)
 def lastText=messageSource.getMessage('paginate.last', null, messageSource.getMessage('default.paginate.last', null, 'Last', locale), locale)
        def lastText=messageSource.getMessage('paginate.last', null, messageSource.getMessage('default.paginate.last', null, 'Last', locale), locale)
 
        

 if(offset!=0)
        if(offset!=0) {
{
 def firstUrl="""${otherParamsUrl}offset=0&max=${params.max}"""
            def firstUrl="""${otherParamsUrl}offset=0&max=${params.max}"""
 writer<<"""
            writer<<"""
 <span id="firstShow" title="${firstText}" onclick="new Ajax.Updater('${attrs.update}',
            <span id="firstShow" title="${firstText}" onclick="new Ajax.Updater('${attrs.update}',

 '${path}/${params.controller}/${params.action}',
        '${path}/${params.controller}/${params.action}', {asynchronous:true,evalScripts:true,parameters:'${firstUrl}'});
{asynchronous:true,evalScripts:true,parameters:'${firstUrl}'});
 return false;" >
        return false;" >

 [$
            [$ {firstText}]
{firstText}]
 </span>
            </span>
 """
            """
 int prev=offset-max
            int prev=offset-max
 def prevUrl="""${otherParamsUrl}offset=${prev}&max=${params.max}"""
            def prevUrl="""${otherParamsUrl}offset=${prev}&max=${params.max}"""
 writer<<"""
            writer<<"""
 <span id="preShow" title="${prevText}" onclick="new Ajax.Updater('${attrs.update}',
                <span id="preShow" title="${prevText}" onclick="new Ajax.Updater('${attrs.update}',

 '${path}/${params.controller}/${params.action}',
            '${path}/${params.controller}/${params.action}', {asynchronous:true,evalScripts:true,parameters:'${prevUrl}'});
{asynchronous:true,evalScripts:true,parameters:'${prevUrl}'});
 return false;" >
            return false;" >

 [$
                [$ {prevText}]
{prevText}]
 </span>
                </span>
 """
                """
 }
        }

 else
        else {
{

 writer<<"""<span id="firstNoShow" title="$
            writer<<"""<span id="firstNoShow" title="$ {firstText}">
{firstText}">

 [$
                      [$ {firstText}]
{firstText}]
 </span>
                      </span>
 """
                """

 writer<<"""<span id="preNoShow" title="$
            writer<<"""<span id="preNoShow" title="$ {prevText}">
{prevText}">

 [$
                    [$ {prevText}]
{prevText}]
 </span>
                    </span>
 """
                    """
 }
        }
 ////数据信息输出
        ////数据信息输出
 writer<<"""<span id="ajaxPageInfo">当前${pageNow}/${pageSize}页(共${total})</span>"""
        writer<<"""<span id="ajaxPageInfo">当前${pageNow}/${pageSize}页(共${total})</span>"""
 
        
 //下一页和末页输出,
        //下一页和末页输出,

 if(offset!=max*(pageSize-1))
        if(offset!=max*(pageSize-1)) {
{
 int next1=offset+max
            int next1=offset+max
 def nextUrl="""${otherParamsUrl}offset=${next1}&max=${params.max}"""
            def nextUrl="""${otherParamsUrl}offset=${next1}&max=${params.max}"""
 
            
 writer<<"""
            writer<<"""
 <span id="nextShow"   title="${nextText}" onclick="new Ajax.Updater('${attrs.update}',
                <span id="nextShow"   title="${nextText}" onclick="new Ajax.Updater('${attrs.update}',

 '${path}/${params.controller}/${params.action}',
            '${path}/${params.controller}/${params.action}', {asynchronous:true,evalScripts:true,parameters:'${nextUrl}'});
{asynchronous:true,evalScripts:true,parameters:'${nextUrl}'});
 return false;" >
            return false;" >

 [$
                [$ {nextText}]
{nextText}]
 </span>
                </span>
 """
                """
 int last=max*(pageSize-1)
            int last=max*(pageSize-1)
 def lastUrl="""${otherParamsUrl}offset=${last}&max=${params.max}"""
            def lastUrl="""${otherParamsUrl}offset=${last}&max=${params.max}"""
 writer<<"""
            writer<<"""
 <span id="lastShow" title="${lastText}" onclick="new Ajax.Updater('${attrs.update}',
                <span id="lastShow" title="${lastText}" onclick="new Ajax.Updater('${attrs.update}',

 '${path}/${params.controller}/${params.action}',
            '${path}/${params.controller}/${params.action}', {asynchronous:true,evalScripts:true,parameters:'${lastUrl}'});
{asynchronous:true,evalScripts:true,parameters:'${lastUrl}'});
 return false;" >
            return false;" >

 [$
                [$ {lastText}]
{lastText}]
 </span>
                </span>
 """
                """
 }
        }

 else
        else {
{

 writer<<"""<span id="nextNoShow" title="$
            writer<<"""<span id="nextNoShow" title="$ {nextText}">
{nextText}">

 [$
                [$ {nextText}]
{nextText}]
 </span>
                </span>
 """
                """

 writer<<"""<span id="lastNoShow" title="$
            writer<<"""<span id="lastNoShow" title="$ {lastText}">
{lastText}">

 [$
                [$ {lastText}]
{lastText}]
 </span>
                </span>
 """
                """
 }
        }
 ///到指定页连接输出
        ///到指定页连接输出
 def selectUrl="&${otherParamsUrl}max=${params.max}"
        def selectUrl="&${otherParamsUrl}max=${params.max}"
 writer<<"""
        writer<<"""
 <span id="ajaxSelectText">转到<select id="ajaxSelect" onchange="myUpdate('${attrs.update}','${path}/${params.controller}/${params.action}'
        <span id="ajaxSelectText">转到<select id="ajaxSelect" onchange="myUpdate('${attrs.update}','${path}/${params.controller}/${params.action}'
 ,'${selectUrl}','${max}')">
        ,'${selectUrl}','${max}')">
 
        
 """
        """

 for(int j=1;j<=pageSize;j++)
        for(int j=1;j<=pageSize;j++) {
{

 if(pageNow!=j)
            if(pageNow!=j) {
{

 writer<<"""  <option value ="$
                writer<<"""  <option value ="$ {j}">${j}/${pageSize}</option>           """
{j}">${j}/${pageSize}</option>           """
 }
            }

 else
            else {
{

 writer<<"""  <option value ="$
                writer<<"""  <option value ="$ {j}"  selected="selected">${j}/${pageSize}</option>           """
{j}"  selected="selected">${j}/${pageSize}</option>           """
 }
            }
 }
        }
 writer<<"</select>页</span>"
        writer<<"</select>页</span>"
 
        
 ////每页显示几条数据
        ////每页显示几条数据
 def maxUrl="&${otherParamsUrl}offset=0"
        def maxUrl="&${otherParamsUrl}offset=0"
 writer<<"""
        writer<<"""
 <span id="ajaxMaxText"> 每页显示</span><select id="ajaxMax" onchange="myUpdate2('${attrs.update}','${path}/${params.controller}/${params.action}'
             <span id="ajaxMaxText"> 每页显示</span><select id="ajaxMax" onchange="myUpdate2('${attrs.update}','${path}/${params.controller}/${params.action}'
 ,'${maxUrl}')">
            ,'${maxUrl}')">
 """
            """

 for(int j=1;j<=selectMax;j++)
        for(int j=1;j<=selectMax;j++) {
{

 if(max!=j)
            if(max!=j) {
{

 writer<<"""<option value ="$
                writer<<"""<option value ="$ {j}">${j}</option>"""
{j}">${j}</option>"""
 }
            }

 else
            else {
{

 writer<<"""<option value ="$
                writer<<"""<option value ="$ {j}"  selected="selected">${j}</option>"""
{j}"  selected="selected">${j}</option>"""
 }
            }
 }
        }
 writer<<"</select> </div>"
        writer<<"</select> </div>"
 }
    }
 
    
 }
}





天苍苍,野茫茫,风吹草底见牛羊