1. 程式人生 > >Bootstrap mergeCells合並單元格(多列)

Bootstrap mergeCells合並單元格(多列)

src prop com function process type str boot table

/**

  • 合並單元格
  • @param target 目標表格對象
  • @param data 原始數據(在服務端完成排序)
  • @param fieldName 合並參照的屬性名稱
  • @param fieldList 要合並的字段集合[不含fieldName]![]
  • @param colspan 合並開始列
    */
    function mergeCells(target, data, fieldName, fieldList, colspan) {
    // 聲明一個map計算相同屬性值在data對象出現的次數和
    var sortMap = {};
    var index = 0;
    var begini=0;
    var endi = 0;
    // 統計fieldName長度
    getCount(target, data, 0, data.length, fieldName, index, sortMap);
    for(var prop in sortMap){
    endi = index+sortMap[prop];
    if(sortMap[prop]>1){
    // console.log(fieldName + ":" + prop,sortMap[prop]);
    for(var i=0;i<fieldList.length;i++){
    getCount(target, data, begini, endi, fieldList[i], index, null);
    }
    }
    index = begini = endi;
    }

}

/**

  • 計算合並
    /
    function getCount(target, data, begini, endi, fieldName, index, sortMap) {
    // console.log(‘fieldName:‘ + fieldName);
    // console.log(begini,endi);
    if(sortMap == null){
    sortMap = {};
    }
    for(var i = begini ; i < endi ; i++){
    for(var prop in data[i]){
    if(prop == fieldName){
    var key = data[i][prop];
    if(sortMap.hasOwnProperty(key)){
    sortMap[key] = sortMap[key]
    1 + 1;
    } else {
    sortMap[key] = 1;
    }
    // console.log(fieldName + ":" + key, sortMap[key]);
    break;
    }
    }
    }
    for(var p in sortMap){
    var count = sortMap[p] * 1;
    // console.log(">>>>>" + ":" + p , count);
    $(target).bootstrapTable(‘mergeCells‘,{index:index, field:fieldName, colspan: 1, rowspan: count});
    index += count;
    }

}

使用:
var data1 = $(‘#table‘).bootstrapTable(‘getData‘, true);
mergeCells($(‘#table‘), data1, "proname", ["promonth", "pkno", "zb"], 1);

技術分享圖片

Bootstrap mergeCells合並單元格(多列)