zoukankan      html  css  js  c++  java
  • bootstrap table + spring + springmvc + mybatis 实现从前端到后端的表格分页

    1.使用准备

    前台需要的资源文件,主要有Bootstrap3相关css、js以及bootstrap Table相关css、js:

    [javascript] view plain copy
     
    1. <-- 样式 -->  
    2. <link rel="stylesheet" href="bootstrap.min.css">  
    3. <link rel="stylesheet" href="bootstrap-table.css">  
    4.   
    5. <script src="jquery.min.js"></script>  
    6. <script src="bootstrap.min.js"></script>  
    7. <script src="bootstrap-table.js"></script>  
    8. <-- 表格汉化js -->  
    9. <script src="bootstrap-table-zh-CN.js"></script>  
    10.    

    以上这些插件可以到这里下载http://bootstrap-table.wenzhixin.NET.cn/zh-cn/getting-started/ (官方文档地址)。

    2.使用方法

    对于bootstrap table 可以通过data 属性或者JavaScript 来启用bootstrap table 插件,显示丰富的功能。

    这里推荐使用javascript来启用bootstrap table插件来使用bootstrap table,可以实现js和html的分离,代码可以重用,下面我介绍的时候也只介绍这种方法。

    3.前端代码

    页面代码:

    [html] view plain copy
     
    1. <-- 以下是一些需要的css、js -->  
    2. <-- 样式 -->  
    3. <link rel="stylesheet" href="bootstrap.min.css">  
    4. <link rel="stylesheet" href="bootstrap-table.css">  
    5.   
    6. <script src="jquery.min.js"></script>  
    7. <script src="bootstrap.min.js"></script>  
    8. <script src="bootstrap-table.js"></script>  
    9. <-- 表格汉化js -->  
    10. <script src="bootstrap-table-zh-CN.js"></script>  
    11.   
    12. <-- 这是html主体代码,只需要这一个就可以了 -->  
    13. <div ><table id="tableList" class="table table-striped"></table</div>  
    
    

    以上代码省略了部分html标签,只粘贴了主要部分。

    js代码:

    [javascript] view plain copy
     
    1. //通过bootstrap Table方法refresh重新加载数据  
    2. function showData() {  
    3.     $('#tableList').bootstrapTable('refresh');  
    4. }  
    5.   
    6. //官方使用方法的语法:<code>$('#table').bootstrapTable('method', parameter)</code>  
    7. $('#tableList').bootstrapTable({  
    8.     columns: [{  
    9.         field: 'id',  
    10.         title: '序号',  
    11.     }, {  
    12.         field: 'year',  
    13.         title: '年度',  
    14.     }, {  
    15.         field: 'month',  
    16.         title: '月份',  
    17.     },{  
    18.         field: 'creDate',  
    19.         title: '日期',  
    20.     },{  
    21.         field: 'merBasicId',  
    22.         title: '商户id',  
    23.     },{  
    24.         field: 'merName',  
    25.         title: '商户名称',  
    26.     },{  
    27.         field: 'categoryTypeName',  
    28.         title: '商户类型',  
    29.     },{  
    30.         field: 'city',  
    31.         title: '城市',  
    32.     },{  
    33.         field: 'area',  
    34.         title: '区域',  
    35.     },{  
    36.         field: 'tradeAreaName',  
    37.         title: '商圈',  
    38.     }],//页面需要展示的列,后端交互对象的属性  
    39.     pagination: true,  //开启分页  
    40.     sidePagination: 'server',//服务器端分页  
    41.     pageNumber: 1,//默认加载页  
    42.     pageSize: 20,//每页数据  
    43.     pageList: [20, 50, 100, 500],//可选的每页数据  
    44.     queryParams: function (params) {  
    45.         return {  
    46.         startDate: $("#txtStartDate").val(),  
    47.         endDate: $("#txtEndDate").val(),  
    48.         merName: $("#txtMerName").val(),  
    49.             pageSize: params.limit,  
    50.             offset: params.offset  
    51.         }  
    52.     },//请求服务器数据时的参数  
    53.     url: rootURL+'/console/finance/channelDivideDetails/data' //服务器数据的加载地址  
    54. });  

    对于parameter更多的描述,具体可以参考前面发的官方文档的链接。

    4.后端代码

    [java] view plain copy
     
    1. //根据传入的pageSize,offset参数决定查哪一页,根据其他参数决定查询哪些数据   
    2. @RequestMapping( value = "/data", method = RequestMethod.POST, produces = "application/json;charset=UTF-8" )  
    3.   @ResponseBody  
    4.   public Object channelDivideDetailsData( HttpServletRequest request, @RequestBody JSONObject jsonObj ) {  
    5.     String html = "[]";  
    6.     Map<String, Object> map = new HashMap<String, Object>();  
    7.     String startDateStr = jsonObj.getString("startDate");  
    8.     String endDateStr = jsonObj.getString("endDate");  
    9.     String merName = jsonObj.getString("merName");  
    10.     int pageSize = jsonObj.getIntValue("pageSize");  
    11.     int offset = jsonObj.getIntValue("offset");  
    12.     try {  
    13.       map.put("startDate", startDateStr);  
    14.       map.put("endDate", endDateStr);  
    15.       if(merName != null && merName != "") {  
    16.         map.put("merName", merName);  
    17.       }  
    18.       PageBounds pageBounds = JSPUtil.getPagerBoundsByParameter(pageSize, offset);  
    19.       List<FChannelDivideDetails> list = channelDivideDetailsService.getChannelDivideDetails(map, pageBounds);  
    20.       if(list != null && list.size() > 0) {  
    21.         Map<String, Object> retMap =  
    22.             (Map<String, Object>) JSPUtil.pagelistToJSONMapNew((PageList<FChannelDivideDetails>) list);  
    23.         html = JSON.toJSONStringWithDateFormat(retMap, DATE_FORMATE_STR);  
    24.       }  
    25.     }  
    26.     catch(Exception e) {  
    27.       logger.error("系统异常e:{}", e);  
    28.       this.buildResponse(ErrorCode.system_1000);  
    29.     }  
    30.     return html;  
    31.   }  

    4.1这里要注意的是前端传过来的参数是json格式的,所以用@RequestBody注解后我们就能将前端传过来的参数取出来。

    4.2代码里用到了mybatis的一个分页插件mybatis-paginator,我们只需要包装出一个PageBounds,参数传入service层,插件会自动帮我们代理实现分页,就不需要我们自己再写分页代码了, mybatis-paginator的具体使用教程搜索关键字查看相关文章即可。

    包装PageBounds的代码:

    [java] view plain copy
     
    1. /** 
    2.    * 取得分页对象 
    3.    *  
    4.    * @param pageSize 
    5.    * @param offset 
    6.    * @return 
    7.    */  
    8.   @SuppressWarnings( "unused" )  
    9.   public static PageBounds getPagerBoundsByParameter( int pageSize, int offset ) {  
    10.     if(pageSize == 0) {  
    11.       return null;  
    12.     }  
    13.   
    14.     PageBounds pageBounds = new PageBounds(offset / pageSize + 1, pageSize);  
    15.     return pageBounds;  
    16.   }  

    4.3最后返回前端的json数据包括total、rows两个对象,total表示数据总数,rows表示需要显示的数据。必须按照这种格式返回才行。

    包装返回数据的代码:

    [java] view plain copy
     
    1. @SuppressWarnings( { "rawtypes", "unchecked" } )  
    2.  public static Object pagelistToJSONMapNew( PageList list ) {  
    3.    Map<String, Object> map = new HashMap<String, Object>();  
    4.    if(list != null) {  
    5.      Paginator paginator = list.getPaginator();  
    6.      map.put("total", paginator.getTotalCount());  
    7.      map.put("rows", new ArrayList(list));  
    8.    }  
    9.    return map;  
    10.  }  

    以上就实现了从前端到后端的表格分页查询。

    目前主要使用到了查询分页,具体其他的操作可以参考查询相关代码。

  • 相关阅读:
    Java提高学习之Object(5)
    cmd命令。
    CacheView。
    快速界面:QML。
    抓包工具。
    打包安装程序。
    AS:加载新版本的SWF文件。
    as自定义菜单。
    as [Frame]元标签
    转载:Flash AS3.0 加载外部资源(图片,MP3,SWF)的两种方式
  • 原文地址:https://www.cnblogs.com/onetwo/p/6390264.html
Copyright © 2011-2022 走看看