在layui 中表格单元格中插入下拉框

这个实现采用了layui 社区一位大神的代码,在此感谢大神的分享,我自己做个记录,也给使用layui 的小伙伴们做一个分享。
思路是这样的

可以使用自定义模板返回select节点然后在done回调中form.render(‘select’);然后还有一个需要注意的是默认td 超出是不显示的,所以你会发现form渲染之后select出来了,但是点击下拉的菜单会看不到,需要处理一下。

代码也贴出来

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="js/layui/src/css/layui.css" media="all">
    <!-- 注意:如果你直接复制所有代码到本地,上述css路径需要改成你本地的 -->
</head>
<body>
 
<button class="layui-btn
  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 16
    评论
可以通过在表格单元格内添下拉框来实现该功能,具体步骤如下: 1. 在表格指定单元格内添一个 `div` 元素,该元素用于容纳下拉框。 ```html <table class="layui-table"> <thead> <tr> <th>姓名</th> <th>年龄</th> <th>性别</th> </tr> </thead> <tbody> <tr> <td>张三</td> <td>18</td> <td> <div class="dropdown"> <select> <option value="男">男</option> <option value="女">女</option> </select> </div> </td> </tr> <!-- 其他行省略 --> </tbody> </table> ``` 2. 使用 `layui.form.render()` 方法对表单进行渲染,以便 `select` 元素能够正确显示。 ```html <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/layui/2.5.6/layui.min.js"></script> <script> layui.use(['form'], function () { var form = layui.form; form.render(); }); </script> ``` 3. 使用 jQuery 监听单元格的点击事件,并在点击事件显示或隐藏下拉框。 ```html <script> $(function () { $('.dropdown').click(function (event) { event.stopPropagation(); // 防止单击事件冒泡到表格行 $('.dropdown').not(this).removeClass('open'); $(this).toggleClass('open'); }); $('html').click(function () { $('.dropdown').removeClass('open'); }); }); </script> ``` 4. 样式表下拉框的样式。 ```html <style> .dropdown { position: relative; display: inline-block; } .dropdown select { width: 100%; padding: 6px 12px; border: 1px solid #d2d6de; border-radius: 2px; background-color: #fff; background-image: none; -webkit-appearance: none; -moz-appearance: none; appearance: none; cursor: pointer; position: absolute; top: 0; right: 0; bottom: 0; left: 0; z-index: 1; } .dropdown.open select { z-index: 2; } </style> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值