layui table 自定义表头

devtools/2024/10/15 5:24:44/

自定义表头-查询

js/css静态文件引用

<!-- 引入 layui.css -->
<link href="//unpkg.com/layui@2.9.16/dist/css/layui.css" rel="stylesheet">
<!-- 引入 layui.js -->
<script src="//unpkg.com/layui@2.9.16/dist/layui.js"></script>

html

<script type="text/html" id="sex">{{#  if(d.sex == 10){ }}男{{# }else if(d.sex == 20) { }}女{{# }else{ }}未知{{# } }}
</script>
<table class="layui-hide" id="tableDemo"></table>

javascript">javascript

<script>layui.use(function () {var filters = {};  // 保存每个输入框的值var form = layui.form;var table = layui.table;var laydate = layui.laydate;var dropdown = layui.dropdown;table.render({elem: '#tableDemo',method: "post",//url: '', //如果要访问后台,改成你的api地址即可//where: { p1: p1, p2: p2, p3: p3},//切换成你的请求实体对象cols: [[{ field: 'id', title: 'ID', width: 80, sort: true, fixed: true },{ field: 'username', title: '用户', width: 120, fixed: true },{ field: 'sign', title: '签名', width: 160 },{ field: 'sex', title: '性别', width: 80, templet: '#sex' },{ field: 'city', title: '城市', width: 100 },{ field: 'experience', title: '积分', width: 80, sort: true }]],data: [{ // 赋值已知数据"id": "10001","username": "张三1","sex": "10","city": "浙江杭州","sign": "人生恰似一场修行","experience": "116"}, {"id": "10002","username": "张三2","sex": "","city": "浙江杭州","sign": "人生恰似一场修行","experience": "12",}, {"id": "10003","username": "张三3","sex": "10","city": "浙江杭州","sign": "人生恰似一场修行","experience": "65"}, {"id": "10004","username": "张三4","sex": "20","city": "浙江杭州","sign": "人生恰似一场修行","experience": "777"}, {"id": "10005","username": "张三5","sex": "20","city": "浙江杭州","sign": "人生恰似一场修行","experience": "86"}, {"id": "10006","username": "张三6","sex": "10","city": "浙江杭州","sign": "人生恰似一场修行","experience": "12"}, {"id": "10007","username": "张三7","sex": "20","city": "浙江杭州","sign": "人生恰似一场修行","experience": "16"}, {"id": "10008","username": "张三8","sex": "10","city": "浙江杭州","sign": "人生恰似一场修行","experience": "106"}],scrollX: true,  // 启用横向滚动fixed: true,page: false,done: function (res, curr, count) {loadCustomSearch(res);}});///初始化查询条件function loadCustomSearch(response) {//这个务必保留 由于使用了flexd layui原理是两个相同的,所以如果不加这个,flexd的列会出现错行var fixedHeader = document.querySelectorAll('.layui-table-fixed thead th');fixedHeader.forEach(function (item, index) {var width = item.offsetWidth - 2 + 'px';if (item.querySelector('input') === null) {//用户if (index == 1) {generateSearchByText("", width, "username", item);}//签名if (index == 2) {generateSearchByText("", width, "sign", item);}}});var th = document.querySelectorAll('thead th');th.forEach(function (item, index) {var width = item.offsetWidth - 2 + 'px';if (item.querySelector('input') === null) {//用户if (index == 1) {generateSearchByText("", width, "username", item);}//签名if (index == 2) {generateSearchByText("", width, "sign", item);}// if (index == 4) {//     generateSearchByDateRange("", "98px", "Create_Date", "Create_DateStart", "Create_DateEnd", item, "date");// }}if (item.querySelector('select') === null) {//性别if (index == 3) {generateSearchBySelect("", width, "sex", item, 0);}}});// 重新计算布局table.resize('tableDemo');}///生成文本框的查询条件function generateSearchByText(placeHolder, width, name, item) {var input = document.createElement('input');input.name = name;input.type = 'text';input.placeholder = placeHolder;input.style.width = width;input.style.fontWeight = "normal";// 如果之前保存了该列的输入内容,填充到输入框中if (filters[name]) {input.value = filters[name];}item.appendChild(input);// 绑定事件input.addEventListener('change', function (e) {var searchValue = e.target.value;// 保存当前输入框的值filters[name] = searchValue;searchChange();});}///生成时间区间查询条件function generateSearchByDateRange(placeHolder, width, dvName, startName, endName, item, type) {let newWidth = width - 6;// 创建时间区间控件的容器var container = document.createElement('div');container.id = dvName;container.style.display = 'flex';container.style.alignItems = 'center';// 创建开始日期输入框var startInput = document.createElement('input');startInput.id = startName;startInput.placeholder = '开始日期';startInput.style.width = width;startInput.style.fontWeight = "normal";// 创建结束日期输入框var endInput = document.createElement('input');endInput.id = endName;endInput.placeholder = '结束日期';endInput.style.width = width;endInput.style.fontWeight = "normal";if (filters[startName]) {startInput.value = filters[startName];}if (filters[endName]) {endInput.value = filters[endName];}container.appendChild(startInput);container.appendChild(document.createTextNode(' - ')); // 添加一个分隔符container.appendChild(endInput);item.appendChild(container);// 使用 laydate.render 绑定时间区间控件laydate.render({elem: '#' + dvName,type: type,fullPanel: true, // 2.8+range: ['#' + startName, '#' + endName],rangeLinked: true,min: minDate,max: maxDate,done: function (value, date, endDate) {// 确保值不为空且包含 " - "if (value && value.includes(' - ')) {var dates = value.split(' - ');  // 分割成开始日期和结束日期var startDate = dates[0];  // 获取开始日期var endDate = dates[1];  // 获取结束日期filters[startName] = startDate;filters[endName] = endDate;document.getElementById(startName).value = startDate;document.getElementById(endName).value = endDate;}searchChange();}});}///生成下拉框的查询条件function generateSearchBySelect(placeHolder, width, name, item, type) {// 创建 select 下拉菜单var select = document.createElement('select');select.id = name;select.name = name;select.style.width = width;select.style.display = "block";select.style.fontWeight = "normal";//禁用layui的select组件样式渲染//不适用此属性的时候,页面会出现先展示layui的select,后展示原生的select组件//此属性开启,则默认不适用layui的select样式select.setAttribute('lay-ignore', true);select.add(new Option("", ""));select.add(new Option("男", "10"));select.add(new Option("女", "20"));if (filters[name]) {select.value = filters[name];}item.appendChild(select);$(select).on('change', function () {var selectedValue = $(this).val();// 保存当前输入框的值filters[name] = selectedValue;searchChange();});}//列头查询事件function searchChange() {table.reload('tableDemo', {where: {  // 添加查询条件dto: sureEntity},page: false,done: function (res, curr, count) {loadCustomSearch(res);}});}});
</script>

表头弹tip信息

javascript-1">javascript

        table.render({elem: '#tableDemo',method: "post",//url: '', //如果要访问后台,改成你的api地址即可//where: { p1: p1, p2: p2, p3: p3},//切换成你的请求实体对象cols: [[{ field: 'id', title: 'ID', width: 80, sort: true, fixed: true },{ field: 'username', title: '用户', width: 120, fixed: true },{ field: 'sign', title: '签名', width: 160 },{ field: 'sex', title: '性别', width: 80, templet: '#sex' },{ field: 'city', title: '城市', width: 100 },{ field: 'experience', title: '积分', width: 80, sort: true }]],data: [{ // 赋值已知数据"id": "10001","username": "张三1","sex": "10","city": "浙江杭州","sign": "人生恰似一场修行","experience": "116"}, {"id": "10002","username": "张三2","sex": "","city": "浙江杭州","sign": "人生恰似一场修行","experience": "12",}, {"id": "10003","username": "张三3","sex": "10","city": "浙江杭州","sign": "人生恰似一场修行","experience": "65"}, {"id": "10004","username": "张三4","sex": "20","city": "浙江杭州","sign": "人生恰似一场修行","experience": "777"}, {"id": "10005","username": "张三5","sex": "20","city": "浙江杭州","sign": "人生恰似一场修行","experience": "86"}, {"id": "10006","username": "张三6","sex": "10","city": "浙江杭州","sign": "人生恰似一场修行","experience": "12"}, {"id": "10007","username": "张三7","sex": "20","city": "浙江杭州","sign": "人生恰似一场修行","experience": "16"}, {"id": "10008","username": "张三8","sex": "10","city": "浙江杭州","sign": "人生恰似一场修行","experience": "106"}],scrollX: true,  // 启用横向滚动fixed: true,page: false,done: function (res, curr, count) {var tipContent = "我是自定义Tip信息";$('.layui-table tr').each(function (index, item) {var nameCell = $(item).find('th').eq(1);let nIndex;nameCell.on('mouseenter', function () {nIndex = layer.tips(tipContent, this, {tips: [1, "#3A3D49"],});});nameCell.on('mouseleave', function () {layer.close(nIndex);});});}});


http://www.ppmy.cn/devtools/125996.html

相关文章

Flask接口通过WSGI服务器部署到后台

1、flask开发服务器警告问题 &#xff08;1&#xff09;flask直接部署警告问题 flask接口直接部署会出现下列警告&#xff1a;WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. 例如&#xff1a; 这条…

基于单片机的教室灯光自动控制系统设计(243)

文章目录 一、前言1.1 项目介绍【1】项目开发背景【2】设计实现的功能【3】项目硬件模块组成1.2 设计思路【1】 系统架构规划【2】 硬件选型与电路设计【3】 软件设计【4】 照明控制策略【5】 显示屏与按键模块设计【6】 系统调试与优化【7】 系统集成与应用1.3 项目开发背景【…

Qt C++设计模式->备忘录模式

备忘录模式&#xff08;Memento Pattern&#xff09;是一种行为型设计模式&#xff0c;用于在不破坏封装性的前提下&#xff0c;捕获并保存对象的内部状态&#xff0c;以便在将来的某个时刻可以恢复到之前的状态。备忘录模式的核心是状态的保存和恢复&#xff0c;常用于实现撤销…

AOT漫谈专题(第三篇): 如何获取C#程序的CPU利用率

一&#xff1a;背景 1. 讲故事 上篇聊到了如何对AOT程序进行轻量级的APM监控&#xff0c;有朋友问我如何获取AOT程序的CPU利用率&#xff0c;本来我觉得这是一个挺简单的问题&#xff0c;但一研究不是这么一回事&#xff0c;这篇我们简单的聊一聊。 二&#xff1a;如何获取C…

Java Python 开发效率利器:IDEA、PyCharm 与 通义灵码深度融合

随着软件开发行业的快速发展&#xff0c;提高开发效率成为每个程序员追求的目标。在众多开发工具中&#xff0c;IntelliJ IDEA 和 PyCharm 分别作为 Java 和 Python 开发者的首选集成开发环境&#xff08;IDE&#xff09;&#xff0c;因其强大的功能和良好的用户体验而备受青睐…

1-laravel 搭建与路由基础

文章目录 laravel 环境搭建安装工程的命令 基于laravel 开发访问默认欢迎页面第一路由 laravel 环境搭建 借助 phpstudy 搭建环境 安装工程的命令 C:\phpstudy_pro\WWW>composer create-project --prefer-dist laravel/laravel la-3 安装位置 安装…

R语言统计分析——马赛克图

参考资料&#xff1a;R语言实战【第2版】 当变量时类别型变量时&#xff0c;若直观察单个类别型变量&#xff0c;可以使用柱状图或饼图&#xff1b;若存在两个类别型变量或更多时&#xff0c;我们可以使用马赛克图。 在马赛克图中&#xff0c;嵌套矩形面积正比于单元格频率&…

成都睿明智科技有限公司正规吗怎么样?

在数字经济的浪潮中&#xff0c;抖音电商以其独特的内容生态和庞大的用户基础&#xff0c;正逐步成为商家们竞相布局的新蓝海。而在这场电商变革的浪潮中&#xff0c;成都睿明智科技有限公司以其专业的服务和敏锐的市场洞察力&#xff0c;成为了众多商家信赖的合作伙伴&#xf…