layui合并 表头合并 列合并

最近遇到一个需求,需要将layui框架渲染出的table表格的表头指定列进行合并,网上查了不少资料都是有两行表头甚至更多行表头的一些列合并,和我的需求不符,我的需求是只有一行表头行,然后需要将该表头的指定两列合并。

1. 附上代码和效果图

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>layui表头合并demo</title>
    <link href="//unpkg.com/layui@2.9.16/dist/css/layui.css" rel="stylesheet" />
  </head>
  <body class="layui-padding-3">
    <table class="layui-hide" id="ID-table-demo-templet"></table>
    <script src="//unpkg.com/layui@2.9.16/dist/layui.js"></script>
    <script>
      layui.use(['table'], function () {
        var table = layui.table;
        var form = layui.form;
        table.render({
          elem: '#ID-table-demo-templet',
          url: 'https://unpkg.com/outeres@0.1.3/json/2/table/user.json',
          page: true,
          height: '315px',
          cols: [
            [
              { field: 'id', fixed: 'left', width: 80, title: 'ID' },
              {
                field: 'username',
                width: 80,
                title: '用户',
                templet: '#ID-table-demo-templet-user',
              },
              {
                field: 'sex',
                width: 60,
                title: '性别',
                templet: function (d) {
                  if (d.sex === '男') {
                    return '<span style="color: blue">♂</span>';
                  } else {
                    return '<span style="color: pink">♀</span>';
                  }
                },
              },
              {
                field: 'city',
                width: 115,
                title: '城市',
                templet:
                  '<div><i class="layui-icon layui-icon-location"></i> {{= d.city }}</div>',
              },
            ],
          ],
          done: function (res, cur, count) {
            console.log('res', res);
          },
        });
      });
    </script>
  </body>
</html>

2. 需求是将用户和性别列表头进行合并,数据列不合并

按f12打开开发调试者工具查看页面元素发现该table所在的dom位置

3. 代码改造如下 table.render中增加回调函数done

        done: function (res, cur, count) {
            var parentElement = document.querySelector(
              '[lay-table-id="ID-table-demo-templet"]'
            );
            var childElementBoxs =
              parentElement.querySelector('.layui-table-box');
            var childElementHeaders = childElementBoxs.querySelector(
              '.layui-table-header'
            );
            var childElementHeaderThs = childElementBoxs.querySelectorAll('th');
            // 指定列合并
            childElementHeaderThs[1].colSpan = 2;
            childElementHeaderThs[1].textContent = '';
            var div = document.createElement('div');
            div.className = 'layui-table-cell laytable-cell-my';
            var span = document.createElement('span');
            span.textContent = '用户性别';
            div.appendChild(span);
            childElementHeaderThs[1].appendChild(div);
            childElementHeaderThs[2].style.display = 'none';
            console.log('res', childElementHeaderThs);
          }

4. 完整代码改造如下

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>layui表头合并demo</title>
    <link href="//unpkg.com/layui@2.9.16/dist/css/layui.css" rel="stylesheet" />
    <style>
      .laytable-cell-my{ width: 141px; text-align:center; }
    </style>
  </head>
  <body class="layui-padding-3">
    <table class="layui-hide" id="ID-table-demo-templet"></table>
    <script src="//unpkg.com/layui@2.9.16/dist/layui.js"></script>
    <script>
      layui.use(['table'], function () {
        var table = layui.table;
        var form = layui.form;
        table.render({
          elem: '#ID-table-demo-templet',
          url: 'https://unpkg.com/outeres@0.1.3/json/2/table/user.json',
          page: true,
          height: '315px',
          cols: [
            [
              { field: 'id', fixed: 'left', width: 80, title: 'ID' },
              {
                field: 'username',
                width: 80,
                title: '用户',
                templet: '#ID-table-demo-templet-user',
              },
              {
                field: 'sex',
                width: 60,
                title: '性别',
                templet: function (d) {
                  if (d.sex === '男') {
                    return '<span style="color: blue">♂</span>';
                  } else {
                    return '<span style="color: pink">♀</span>';
                  }
                },
              },
              {
                field: 'city',
                width: 115,
                title: '城市',
                templet:
                  '<div><i class="layui-icon layui-icon-location"></i> {{= d.city }}</div>',
              },
            ],
          ],
          done: function (res, cur, count) {
            var parentElement = document.querySelector(
              '[lay-table-id="ID-table-demo-templet"]'
            );
            var childElementBoxs =
              parentElement.querySelector('.layui-table-box');
            var childElementHeaders = childElementBoxs.querySelector(
              '.layui-table-header'
            );
            var childElementHeaderThs = childElementBoxs.querySelectorAll('th');
            // 指定列合并
            childElementHeaderThs[1].colSpan = 2;
            childElementHeaderThs[1].textContent = '';
            var div = document.createElement('div');
            div.className = 'layui-table-cell laytable-cell-my';
            var span = document.createElement('span');
            span.textContent = '用户性别';
            div.appendChild(span);
            childElementHeaderThs[1].appendChild(div);
            childElementHeaderThs[2].style.display = 'none';
            console.log('res', childElementHeaderThs);
          },
        });
      });
    </script>
  </body>
</html>

效果如下

更多layui使用方法请参考官网说明

Layui 开发使用文档 - 入门指南icon-default.png?t=O83Ahttps://layui.dev/2.7/docs/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值