分享一个grails与jqGrid整合的小代码

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

//服务
class JqGridService {
    
    /**
    * @params params 相关参数(jqGrid向服务器端发的参数)
    * @param queryBlock 取数据的闭包逻辑
    * @param dataFormat 取出数据后具体需要组装成json的数据
    */
    def createForJson = { params,queryBlock,dataFormat ->
        def sortIndex = params.sidx ?: 'id'
        def sortOrder  = params.sord ?: 'desc'
        def maxRows = Integer.valueOf(params.rows ?: 10)
        def currentPage = Integer.valueOf(params.page ?: 1) ?: 1
        def rowOffset = currentPage == 1 ? 0 : (currentPage - 1) * maxRows
        params.max = maxRows
        params.offset = rowOffset
        def dataRows = queryBlock.call(params)
        def totalRows = dataRows.totalCount
        def numberOfPages = Math.ceil(totalRows / maxRows)
        def results = dataRows?.collect {
            dataFormat(it)
        }
        [rows: results, currpage: currentPage, totalrecords: totalRows, totalpages: numberOfPages]
    }
}

//控制器调用示例
class MyController{

def jqGridService

       def listJSON = {
        //取数据的闭包逻辑
        def block = { params ->
            Article.createCriteria().list(max: params.max, offset: params.offset) {
            order(params.sidx, params.sord).ignoreCase()
            }
        }
        //取出数据后具体需要组装成json的数据
        def format = { row ->
            [
                    'id':row.id,
                    'nickname':row.nickname,
                    'realname':row.realname,
                    'username':row.username,
                    'type':row.type?.name,
                    'online':row.online ? '是' : '否',
                    'dateCreated':row.dateCreated?.format('yyyy-MM-dd HH:mm:ss'),
                    'loginTime':row.loginTime?.format('yyyy-MM-dd HH:mm:ss')
            ]
        }        
        render jqGridService.createForJson(params,block,format) as JSON
    }
}

//页面调用
<script type="text/javascript">
          $(document).ready(function(){
              $('#list').jqGrid(
              {
                jsonReader : {
                root:"rows",
                page: "currpage",
                total: "totalpages",
                records: "totalrecords",
                repeatitems: false,
                id: "0"
                },
                url: '${resource()}/admin/user/listJSON',
                colNames: ['ID', '登录名','昵称','真实姓名','用户类型','注册时间','登录时间','是否在线'],
                colModel:
                  [
                  {name:'id', editable: false},
                  {name:'username', editable: true},
                  {name:'nickname', editable: true},
                  {name:'realname', editable: true},
                  {name:'type', editable: true},
                  {name:'dateCreated', editable: true},
                  {name:'loginTime', editable: true},
                  {name:'online', editable: true}
                  ],
                sortname: 'id',
                caption: '用户列表',
                height: 300,
                rowNum:10,
                autowidth: true,
                scrollOffset: 0,
                viewrecords: true,
                pager: '#pager',
                datatype: 'json'
              }
            );