BootStrap Table 合併單元格

    爲了更直觀展現表格的一大堆亂七八糟的數據,合併單元格就派上用場:javascript

    效果:css

    代碼:html

    

<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3個meta標籤*必須*放在最前面,任何其餘內容都*必須*跟隨其後! -->
    <title>BootstrapTable合併單元格</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/bootstrap-table.min.css" rel="stylesheet">

    <!-- HTML5 shim 和 Respond.js 是爲了讓 IE8 支持 HTML5 元素和媒體查詢(media queries)功能 -->
    <!-- 警告:經過 file:// 協議(就是直接將 html 頁面拖拽到瀏覽器中)訪問頁面時 Respond.js 不起做用 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
        <table id="bootstrap-table" class="table table-hover"></table>
        <!-- jQuery (Bootstrap 的全部 JavaScript 插件都依賴 jQuery,因此必須放在前邊) -->
        <script src="js/jquery.min.js"></script>
        <!-- 加載 Bootstrap 的全部 JavaScript 插件。你也能夠根據須要只加載單個插件。 -->
        <script src="js/bootstrap.min.js"></script>
        <!-- 加載 Bootstrap-Table 插件 -->
        <script src="js/bootstrap-table.min.js"></script>
        <!-- 漢化插件 -->
        <script src="js/bootstrap-table-zh-CN.min.js"></script>
        <script type="application/javascript">
            $('#bootstrap-table').bootstrapTable({
                url: 'json/data.json',
                sidePagination: "true",
                pageSize: "10",
                pagination: true, // 是否分頁
                columns: [
                    {
                        field: 'city',
                        title: ''
                    },
                    {
                        field: 'area',
                        title: ''
                    },
                    {
                        field: 'gdp',
                        title: 'GDP'
                    }
                ],
                onLoadSuccess: function () {//當全部數據被加載時觸發處理函數
                    var data = $('#bootstrap-table').bootstrapTable('getData', true);//獲取當前頁數據
                    mergeCells(data,'city',1,$('#bootstrap-table'));
                },
                onPageChange: function (){//當頁面更改頁碼或頁面大小時觸發
                    var data = $('#bootstrap-table').bootstrapTable('getData', true);
                    mergeCells(data,'city',1,$('#bootstrap-table'));
                },
            });
            function mergeCells(data,fieldName,colspan,target){
                //聲明一個map計算相同屬性值在data對象出現的次數和
                var sortMap = {};
                for(var i = 0 ; i < data.length ; i++){
                    for(var prop in data[i]){
                        if(prop == fieldName){
                            var key = data[i][prop]     //fieldName的value
                            if(sortMap.hasOwnProperty(key)){
                                sortMap[key] = sortMap[key] * 1 + 1;
                            } else {
                                sortMap[key] = 1;
                            }
                            break;
                        }
                    }
                }
                /*for(var prop in sortMap){
                    console.log(prop,sortMap[prop])
                }*/
                //合併單元格
                var index = 0;
                for(var prop in sortMap){
                    var count = sortMap[prop] * 1;
//注意:合併前Java後臺Sql要對field字段排序(order by)
//index:合併起始位置,field:合併列名,colspan:合併成一列,rowspan:合併行數 $(target).bootstrapTable(
'mergeCells',{index:index, field:fieldName, colspan: colspan, rowspan: count}); index += count; } } </script> </body> </html>
相關文章
相關標籤/搜索