tableMerge.js 3.9 KB
Newer Older
H
hjdhnx 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
/**
 *
 * @name:  子表格扩展
 * @author: yelog
 * @link: https://github.com/yelog/layui-soul-table
 * @license: MIT
 * @version: v1.6.4
 */
layui.define(['table'], function (exports) {

  var $ = layui.jquery;

  // 封装方法
  var mod = {
    /**
     * 渲染入口
     * @param myTable
     */
    render: function (myTable) {
      var tableBox = $(myTable.elem).next().children('.layui-table-box'),
        $main = $(tableBox.children('.layui-table-body').children('table').children('tbody').children('tr').toArray().reverse()),
        $fixLeft = $(tableBox.children('.layui-table-fixed-l').children('.layui-table-body').children('table').children('tbody').children('tr').toArray().reverse()),
        $fixRight = $(tableBox.children('.layui-table-fixed-r').children('.layui-table-body').children('table').children('tbody').children('tr').toArray().reverse()),
        mergeRecord = {};

      layui.each(myTable.cols, function (i1, item1) {
        layui.each(item1, function (i2, item2) {
          if (item2.merge && item2.field) {
            var mergeField = [item2.field];
            if (item2.merge !== true) {
              if (typeof item2.merge === 'string') {
                mergeField = [item2.merge]
              } else {
                mergeField = item2.merge
              }
            }
            mergeRecord[myTable.index + '-' + i1 + '-' + i2] = {mergeField: mergeField, rowspan: 1}
          }
        })
      })

      $main.each(function (i) {

        for (var item in mergeRecord) {
          if (i === $main.length - 1 || isMaster(i, item)) {
            var tdHeight = $(this).children('[data-key="' + item + '"]').outerHeight(), patchHeight = 0; // 获取td高度
            if ($main.eq(i).data('index') === 0) {
              patchHeight = 1
            }
            $(this).children('[data-key="' + item + '"]').attr('rowspan', mergeRecord[item].rowspan).css({
              'position': 'static',
              'height': tdHeight * mergeRecord[item].rowspan + patchHeight
            }).children().css({
              height: 'auto',
              'white-space': 'normal',
              'max-height': tdHeight * mergeRecord[item].rowspan + patchHeight - 10
            });
            $fixLeft.eq(i).children('[data-key="' + item + '"]').attr('rowspan', mergeRecord[item].rowspan).css({
              'position': 'static',
              'height': tdHeight * mergeRecord[item].rowspan + patchHeight
            }).children().css({
              height: 'auto',
              'white-space': 'normal',
              'max-height': tdHeight * mergeRecord[item].rowspan + patchHeight - 10
            });
            $fixRight.eq(i).children('[data-key="' + item + '"]').attr('rowspan', mergeRecord[item].rowspan).css({
              'position': 'static',
              'height': tdHeight * mergeRecord[item].rowspan + patchHeight
            }).children().css({
              height: 'auto',
              'white-space': 'normal',
              'max-height': tdHeight * mergeRecord[item].rowspan + patchHeight - 10
            });
            mergeRecord[item].rowspan = 1;
          } else {
            $(this).children('[data-key="' + item + '"]').remove();
            $fixLeft.eq(i).children('[data-key="' + item + '"]').remove();
            $fixRight.eq(i).children('[data-key="' + item + '"]').remove();
            mergeRecord[item].rowspan += 1;
          }
        }
      })

      function isMaster(index, item) {
        var mergeField = mergeRecord[item].mergeField;
        var dataLength = layui.table.cache[myTable.id].length;
        for (var i = 0; i < mergeField.length; i++) {

          if (layui.table.cache[myTable.id][dataLength - 2 - index][mergeField[i]]
            !== layui.table.cache[myTable.id][dataLength - 1 - index][mergeField[i]]) {
            return true;
          }
        }
        return false;
      }

    }
  };

  // 输出
  exports('tableMerge', mod);
});