layui table自定义模板

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <!-- 引入 layui.css -->
  <link rel="stylesheet" href="http://unpkg.com/layui@2.6.8/dist/css/layui.css">
  <!-- 引入 layui.js -->
  <script src="http://unpkg.com/layui@2.6.8/dist/layui.js"></script>

  <style>
    .layui-form-checkbox {
      padding-right: 0 !important;
    }

    .layui-table-view .layui-form-checkbox i {
      height: 18px !important;
    }


    .layui-form-checkbox i {
      width: 18px !important;
      font-size: 18px !important;
      border-width: 1px !important;
      border-style: solid !important;
      border-color: rgb(210, 210, 210) !important;
    }

    .layui-form-checked i {
      border-color: #5FB878 !important;
      background-color: #5FB878 !important;
      color: #fff !important;
    }

    .laytable-cell-1-0-3 {
      height: 60px !important;
      line-height: 25px !important;
      padding: 0 15px !important;
      position: relative !important;
      box-sizing: border-box !important;
    }

    .layui-table tr {
      height: 60px;
      /* line-height: 60px; */
    }

    .layui-table-view .layui-table th[data-field='action'] .laytable-cell-1-0-3 {
      line-height: 60px !important;
    }
  </style>

</head>

<body>
  <!-- 测试列表 -->
  <table class="test-table" id="test-table" lay-size="lg" lay-filter="testFilter"></table>
  <button id="getBtn">查看选中数据</button>

  <!-- 自定义cell 指定id -->
  <script type="text/html" id="test-table-action">
    {{#  if(d.hobby1==1){ }}
    <input type="checkbox" name="hobby1" id={{d.id}} checked value={{ d.hobby1 }} lay-event="hobby1"
      lay-filter="hobby1"><span style="margin-left: 10px;">篮球</span><br />
  
    {{# } else { }}
    <input type="checkbox" name="hobby1" id={{d.id}} value={{ d.hobby1 }} lay-event="hobby1"
    lay-filter="hobby1"><span style="margin-left: 10px;">篮球</span><br />
    {{# } }}
  
    {{# if(d.hobby2==1){ }}
    <input type="checkbox" name="hobby2" id={{d.id}}  checked value={{ d.hobby2 }} lay-event="hobby2"
    lay-filter="hobby2"><span style="margin-left: 10px;">背带裤</span><br />
    {{# } else { }}
    <input type="checkbox" name="hobby2" id={{d.id}} value={{ d.hobby2 }} lay-event="hobby2"
    lay-filter="hobby2"><span style="margin-left: 10px;">背带裤</span><br />
    {{# } }}
  </script>

  <script>
    window.onload = function () {
      const tableData = [
        { "id": 1, "name": '鸡哥', "age": '22', "hobby1": 0, "hobby2": 0 },
        { "id": 2, "name": '狗哥', "age": '30', "hobby1": 0, "hobby2": 0 },
        { "id": 3, "name": '猫弟', "age": '12', "hobby1": 0, "hobby2": 0 },
        { "id": 4, "name": '喜羊羊', "age": '55', "hobby1": 0, "hobby2": 0 },
      ]
      layui.use(['table', 'form'], function () {
        var table = layui.table;
        var form = layui.form;
        table.render({
          id: 'testTable',
          elem: '#test-table',
          cols: [[
            { type: 'checkbox' },
            { field: 'name', width: 92, title: '姓名', align: 'center' },
            { field: 'age', width: 92, title: '年龄', align: 'center' },
            { field: 'action', width: 120, title: '爱好', align: 'center', templet: '#test-table-action' }
          ]],
          data: tableData,
          page: false
        });

        //监听表格复选框选择
        table.on('checkbox(testFilter)', function (obj) {
          console.log(obj)
        });

        form.on('checkbox(hobby1)', function (data) {
          var status = data.elem.checked ? 1 : 0;
          console.log(">>>>>>>>>>>>>>", status);
          console.log(data.elem.id)
          const currentId = data.elem.id
          tableData.forEach(element => {
            if (element.id == currentId) {
              element.hobby1 = status
              return
            }
          });
        });

        form.on('checkbox(hobby2)', function (data) {
          var status = data.elem.checked ? 1 : 0;
          console.log(">>>>>>>>>>>>>>", status);
          console.log(data.elem.id)
          const currentId = data.elem.id
          tableData.forEach(element => {
            if (element.id == currentId) {
              element.hobby2 = status
              return
            }
          });
        });

        var btn = document.getElementById('getBtn');
        btn.addEventListener('click', function () {
          console.log('选中的数据>>>')
          var checkStatus = table.checkStatus('testTable')
          const data = checkStatus.data;
          console.log(JSON.stringify(data))
        }, false)

      });

    }
  </script>


</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zsd_666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值